|
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: Action.php 20851 2010-02-02 21:45:51Z ralph $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Tool_Project_Provider_Abstract |
|
25 */ |
|
26 require_once 'Zend/Tool/Project/Provider/Abstract.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_Tool_Framework_Provider_Pretendable |
|
30 */ |
|
31 require_once 'Zend/Tool/Framework/Provider/Pretendable.php'; |
|
32 |
|
33 /** |
|
34 * @category Zend |
|
35 * @package Zend_Tool |
|
36 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
37 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
38 */ |
|
39 class Zend_Tool_Project_Provider_Action |
|
40 extends Zend_Tool_Project_Provider_Abstract |
|
41 implements Zend_Tool_Framework_Provider_Pretendable |
|
42 { |
|
43 |
|
44 /** |
|
45 * createResource() |
|
46 * |
|
47 * @param Zend_Tool_Project_Profile $profile |
|
48 * @param string $actionName |
|
49 * @param string $controllerName |
|
50 * @param string $moduleName |
|
51 * @return Zend_Tool_Project_Profile_Resource |
|
52 */ |
|
53 public static function createResource(Zend_Tool_Project_Profile $profile, $actionName, $controllerName, $moduleName = null) |
|
54 { |
|
55 |
|
56 if (!is_string($actionName)) { |
|
57 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"actionName\" is the name of a action resource to create.'); |
|
58 } |
|
59 |
|
60 if (!is_string($controllerName)) { |
|
61 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"controllerName\" is the name of a controller resource to create.'); |
|
62 } |
|
63 |
|
64 $controllerFile = self::_getControllerFileResource($profile, $controllerName, $moduleName); |
|
65 |
|
66 $actionMethod = $controllerFile->createResource('ActionMethod', array('actionName' => $actionName)); |
|
67 |
|
68 return $actionMethod; |
|
69 } |
|
70 |
|
71 /** |
|
72 * hasResource() |
|
73 * |
|
74 * @param Zend_Tool_Project_Profile $profile |
|
75 * @param string $actionName |
|
76 * @param string $controllerName |
|
77 * @param string $moduleName |
|
78 * @return Zend_Tool_Project_Profile_Resource |
|
79 */ |
|
80 public static function hasResource(Zend_Tool_Project_Profile $profile, $actionName, $controllerName, $moduleName = null) |
|
81 { |
|
82 if (!is_string($actionName)) { |
|
83 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"actionName\" is the name of a action resource to create.'); |
|
84 } |
|
85 |
|
86 if (!is_string($controllerName)) { |
|
87 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"controllerName\" is the name of a controller resource to create.'); |
|
88 } |
|
89 |
|
90 $controllerFile = self::_getControllerFileResource($profile, $controllerName, $moduleName); |
|
91 |
|
92 if ($controllerFile == null) { |
|
93 throw new Zend_Tool_Project_Provider_Exception('Controller ' . $controllerName . ' was not found.'); |
|
94 } |
|
95 |
|
96 return (($controllerFile->search(array('actionMethod' => array('actionName' => $actionName)))) instanceof Zend_Tool_Project_Profile_Resource); |
|
97 } |
|
98 |
|
99 /** |
|
100 * _getControllerFileResource() |
|
101 * |
|
102 * @param Zend_Tool_Project_Profile $profile |
|
103 * @param string $controllerName |
|
104 * @param string $moduleName |
|
105 * @return Zend_Tool_Project_Profile_Resource |
|
106 */ |
|
107 protected static function _getControllerFileResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null) |
|
108 { |
|
109 $profileSearchParams = array(); |
|
110 |
|
111 if ($moduleName != null && is_string($moduleName)) { |
|
112 $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName)); |
|
113 } |
|
114 |
|
115 $profileSearchParams[] = 'controllersDirectory'; |
|
116 $profileSearchParams['controllerFile'] = array('controllerName' => $controllerName); |
|
117 |
|
118 return $profile->search($profileSearchParams); |
|
119 } |
|
120 |
|
121 /** |
|
122 * create() |
|
123 * |
|
124 * @param string $name Action name for controller, in camelCase format. |
|
125 * @param string $controllerName Controller name action should be applied to. |
|
126 * @param bool $viewIncluded Whether the view should the view be included. |
|
127 * @param string $module Module name action should be applied to. |
|
128 */ |
|
129 public function create($name, $controllerName = 'Index', $viewIncluded = true, $module = null) |
|
130 { |
|
131 |
|
132 $this->_loadProfile(); |
|
133 |
|
134 // Check that there is not a dash or underscore, return if doesnt match regex |
|
135 if (preg_match('#[_-]#', $name)) { |
|
136 throw new Zend_Tool_Project_Provider_Exception('Action names should be camel cased.'); |
|
137 } |
|
138 |
|
139 $originalName = $name; |
|
140 $originalControllerName = $controllerName; |
|
141 |
|
142 // ensure it is camelCase (lower first letter) |
|
143 $name = strtolower(substr($name, 0, 1)) . substr($name, 1); |
|
144 |
|
145 // ensure controller is MixedCase |
|
146 $controllerName = ucfirst($controllerName); |
|
147 |
|
148 if (self::hasResource($this->_loadedProfile, $name, $controllerName, $module)) { |
|
149 throw new Zend_Tool_Project_Provider_Exception('This controller (' . $controllerName . ') already has an action named (' . $name . ')'); |
|
150 } |
|
151 |
|
152 $actionMethod = self::createResource($this->_loadedProfile, $name, $controllerName, $module); |
|
153 |
|
154 // get request/response object |
|
155 $request = $this->_registry->getRequest(); |
|
156 $response = $this->_registry->getResponse(); |
|
157 |
|
158 // alert the user about inline converted names |
|
159 $tense = (($request->isPretend()) ? 'would be' : 'is'); |
|
160 |
|
161 if ($name !== $originalName) { |
|
162 $response->appendContent( |
|
163 'Note: The canonical action name that ' . $tense |
|
164 . ' used with other providers is "' . $name . '";' |
|
165 . ' not "' . $originalName . '" as supplied', |
|
166 array('color' => array('yellow')) |
|
167 ); |
|
168 } |
|
169 |
|
170 if ($controllerName !== $originalControllerName) { |
|
171 $response->appendContent( |
|
172 'Note: The canonical controller name that ' . $tense |
|
173 . ' used with other providers is "' . $controllerName . '";' |
|
174 . ' not "' . $originalControllerName . '" as supplied', |
|
175 array('color' => array('yellow')) |
|
176 ); |
|
177 } |
|
178 |
|
179 unset($tense); |
|
180 |
|
181 if ($request->isPretend()) { |
|
182 $response->appendContent( |
|
183 'Would create an action named ' . $name . |
|
184 ' inside controller at ' . $actionMethod->getParentResource()->getContext()->getPath() |
|
185 ); |
|
186 } else { |
|
187 $response->appendContent( |
|
188 'Creating an action named ' . $name . |
|
189 ' inside controller at ' . $actionMethod->getParentResource()->getContext()->getPath() |
|
190 ); |
|
191 $actionMethod->create(); |
|
192 $this->_storeProfile(); |
|
193 } |
|
194 |
|
195 if ($viewIncluded) { |
|
196 $viewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, $name, $controllerName, $module); |
|
197 |
|
198 if ($this->_registry->getRequest()->isPretend()) { |
|
199 $response->appendContent( |
|
200 'Would create a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath() |
|
201 ); |
|
202 } else { |
|
203 $response->appendContent( |
|
204 'Creating a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath() |
|
205 ); |
|
206 $viewResource->create(); |
|
207 $this->_storeProfile(); |
|
208 } |
|
209 |
|
210 } |
|
211 |
|
212 } |
|
213 |
|
214 } |