|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Tool |
|
17 * @subpackage Framework |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: ActionMethod.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Tool_Project_Context_Interface |
|
25 */ |
|
26 require_once 'Zend/Tool/Project/Context/Interface.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_Reflection_File |
|
30 */ |
|
31 require_once 'Zend/Reflection/File.php'; |
|
32 |
|
33 /** |
|
34 * This class is the front most class for utilizing Zend_Tool_Project |
|
35 * |
|
36 * A profile is a hierarchical set of resources that keep track of |
|
37 * items within a specific project. |
|
38 * |
|
39 * @category Zend |
|
40 * @package Zend_Tool |
|
41 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
42 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
43 */ |
|
44 class Zend_Tool_Project_Context_Zf_ActionMethod implements Zend_Tool_Project_Context_Interface |
|
45 { |
|
46 |
|
47 /** |
|
48 * @var Zend_Tool_Project_Profile_Resource |
|
49 */ |
|
50 protected $_resource = null; |
|
51 |
|
52 /** |
|
53 * @var Zend_Tool_Project_Profile_Resource |
|
54 */ |
|
55 protected $_controllerResource = null; |
|
56 |
|
57 /** |
|
58 * @var string |
|
59 */ |
|
60 protected $_controllerPath = ''; |
|
61 |
|
62 /** |
|
63 * @var string |
|
64 */ |
|
65 protected $_actionName = null; |
|
66 |
|
67 /** |
|
68 * init() |
|
69 * |
|
70 * @return Zend_Tool_Project_Context_Zf_ActionMethod |
|
71 */ |
|
72 public function init() |
|
73 { |
|
74 $this->_actionName = $this->_resource->getAttribute('actionName'); |
|
75 |
|
76 $this->_resource->setAppendable(false); |
|
77 $this->_controllerResource = $this->_resource->getParentResource(); |
|
78 if (!$this->_controllerResource->getContext() instanceof Zend_Tool_Project_Context_Zf_ControllerFile) { |
|
79 require_once 'Zend/Tool/Project/Context/Exception.php'; |
|
80 throw new Zend_Tool_Project_Context_Exception('ActionMethod must be a sub resource of a ControllerFile'); |
|
81 } |
|
82 // make the ControllerFile node appendable so we can tack on the actionMethod. |
|
83 $this->_resource->getParentResource()->setAppendable(true); |
|
84 |
|
85 $this->_controllerPath = $this->_controllerResource->getContext()->getPath(); |
|
86 |
|
87 /* |
|
88 * This code block is now commented, its doing to much for init() |
|
89 * |
|
90 if ($this->_controllerPath != '' && self::hasActionMethod($this->_controllerPath, $this->_actionName)) { |
|
91 require_once 'Zend/Tool/Project/Context/Exception.php'; |
|
92 throw new Zend_Tool_Project_Context_Exception('An action named ' . $this->_actionName . 'Action already exists in this controller'); |
|
93 } |
|
94 */ |
|
95 |
|
96 return $this; |
|
97 } |
|
98 |
|
99 /** |
|
100 * getPersistentAttributes |
|
101 * |
|
102 * @return array |
|
103 */ |
|
104 public function getPersistentAttributes() |
|
105 { |
|
106 return array( |
|
107 'actionName' => $this->getActionName() |
|
108 ); |
|
109 } |
|
110 |
|
111 /** |
|
112 * getName() |
|
113 * |
|
114 * @return string |
|
115 */ |
|
116 public function getName() |
|
117 { |
|
118 return 'ActionMethod'; |
|
119 } |
|
120 |
|
121 /** |
|
122 * setResource() |
|
123 * |
|
124 * @param Zend_Tool_Project_Profile_Resource $resource |
|
125 * @return Zend_Tool_Project_Context_Zf_ActionMethod |
|
126 */ |
|
127 public function setResource(Zend_Tool_Project_Profile_Resource $resource) |
|
128 { |
|
129 $this->_resource = $resource; |
|
130 return $this; |
|
131 } |
|
132 |
|
133 /** |
|
134 * setActionName() |
|
135 * |
|
136 * @param string $actionName |
|
137 * @return Zend_Tool_Project_Context_Zf_ActionMethod |
|
138 */ |
|
139 public function setActionName($actionName) |
|
140 { |
|
141 $this->_actionName = $actionName; |
|
142 return $this; |
|
143 } |
|
144 |
|
145 /** |
|
146 * getActionName() |
|
147 * |
|
148 * @return string |
|
149 */ |
|
150 public function getActionName() |
|
151 { |
|
152 return $this->_actionName; |
|
153 } |
|
154 |
|
155 /** |
|
156 * create() |
|
157 * |
|
158 * @return Zend_Tool_Project_Context_Zf_ActionMethod |
|
159 */ |
|
160 public function create() |
|
161 { |
|
162 if (self::createActionMethod($this->_controllerPath, $this->_actionName) === false) { |
|
163 require_once 'Zend/Tool/Project/Context/Exception.php'; |
|
164 throw new Zend_Tool_Project_Context_Exception( |
|
165 'Could not create action within controller ' . $this->_controllerPath |
|
166 . ' with action name ' . $this->_actionName |
|
167 ); |
|
168 } |
|
169 return $this; |
|
170 } |
|
171 |
|
172 /** |
|
173 * delete() |
|
174 * |
|
175 * @return Zend_Tool_Project_Context_Zf_ActionMethod |
|
176 */ |
|
177 public function delete() |
|
178 { |
|
179 // @todo do this |
|
180 return $this; |
|
181 } |
|
182 |
|
183 /** |
|
184 * createAcionMethod() |
|
185 * |
|
186 * @param string $controllerPath |
|
187 * @param string $actionName |
|
188 * @param string $body |
|
189 * @return true |
|
190 */ |
|
191 public static function createActionMethod($controllerPath, $actionName, $body = ' // action body') |
|
192 { |
|
193 if (!file_exists($controllerPath)) { |
|
194 return false; |
|
195 } |
|
196 |
|
197 $controllerCodeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($controllerPath, true, true); |
|
198 $controllerCodeGenFile->getClass()->setMethod(array( |
|
199 'name' => $actionName . 'Action', |
|
200 'body' => $body |
|
201 )); |
|
202 |
|
203 file_put_contents($controllerPath, $controllerCodeGenFile->generate()); |
|
204 return true; |
|
205 } |
|
206 |
|
207 /** |
|
208 * hasActionMethod() |
|
209 * |
|
210 * @param string $controllerPath |
|
211 * @param string $actionName |
|
212 * @return bool |
|
213 */ |
|
214 public static function hasActionMethod($controllerPath, $actionName) |
|
215 { |
|
216 if (!file_exists($controllerPath)) { |
|
217 return false; |
|
218 } |
|
219 |
|
220 $controllerCodeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($controllerPath, true, true); |
|
221 return $controllerCodeGenFile->getClass()->hasMethod($actionName . 'Action'); |
|
222 } |
|
223 |
|
224 } |