13 * to license@zend.com so we can send you a copy immediately. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_Tool |
16 * @package Zend_Tool |
17 * @subpackage Framework |
17 * @subpackage Framework |
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
18 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
20 * @version $Id: Controller.php 20851 2010-02-02 21:45:51Z ralph $ |
20 * @version $Id: Controller.php 24593 2012-01-05 20:35:02Z matthew $ |
21 */ |
21 */ |
22 |
22 |
23 /** |
23 /** |
24 * @category Zend |
24 * @category Zend |
25 * @package Zend_Tool |
25 * @package Zend_Tool |
26 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
26 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
27 * @license http://framework.zend.com/license/new-bsd New BSD License |
27 * @license http://framework.zend.com/license/new-bsd New BSD License |
28 */ |
28 */ |
29 class Zend_Tool_Project_Provider_Controller |
29 class Zend_Tool_Project_Provider_Controller |
30 extends Zend_Tool_Project_Provider_Abstract |
30 extends Zend_Tool_Project_Provider_Abstract |
31 implements Zend_Tool_Framework_Provider_Pretendable |
31 implements Zend_Tool_Framework_Provider_Pretendable |
68 * hasResource() |
68 * hasResource() |
69 * |
69 * |
70 * @param Zend_Tool_Project_Profile $profile |
70 * @param Zend_Tool_Project_Profile $profile |
71 * @param string $controllerName |
71 * @param string $controllerName |
72 * @param string $moduleName |
72 * @param string $moduleName |
73 * @return Zend_Tool_Project_Profile_Resource |
73 * @return boolean |
74 */ |
74 */ |
75 public static function hasResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null) |
75 public static function hasResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null) |
76 { |
76 { |
77 if (!is_string($controllerName)) { |
77 if (!is_string($controllerName)) { |
78 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.'); |
78 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.'); |
79 } |
79 } |
80 |
80 |
81 $controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName); |
81 $controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName); |
82 return (($controllersDirectory->search(array('controllerFile' => array('controllerName' => $controllerName)))) instanceof Zend_Tool_Project_Profile_Resource); |
82 return ($controllersDirectory &&($controllersDirectory->search(array('controllerFile' => array('controllerName' => $controllerName)))) instanceof Zend_Tool_Project_Profile_Resource); |
83 } |
83 } |
84 |
84 |
85 /** |
85 /** |
86 * _getControllersDirectoryResource() |
86 * _getControllersDirectoryResource() |
87 * |
87 * |
110 */ |
110 */ |
111 public function create($name, $indexActionIncluded = true, $module = null) |
111 public function create($name, $indexActionIncluded = true, $module = null) |
112 { |
112 { |
113 $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); |
113 $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); |
114 |
114 |
|
115 // get request & response |
|
116 $request = $this->_registry->getRequest(); |
|
117 $response = $this->_registry->getResponse(); |
|
118 |
115 // determine if testing is enabled in the project |
119 // determine if testing is enabled in the project |
116 require_once 'Zend/Tool/Project/Provider/Test.php'; |
120 require_once 'Zend/Tool/Project/Provider/Test.php'; |
117 $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile); |
121 $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile); |
118 |
122 |
119 if (self::hasResource($this->_loadedProfile, $name, $module)) { |
123 if ($testingEnabled && !Zend_Tool_Project_Provider_Test::isPHPUnitAvailable()) { |
120 throw new Zend_Tool_Project_Provider_Exception('This project already has a controller named ' . $name); |
124 $testingEnabled = false; |
121 } |
125 $response->appendContent( |
122 |
126 'Note: PHPUnit is required in order to generate controller test stubs.', |
123 // Check that there is not a dash or underscore, return if doesnt match regex |
127 array('color' => array('yellow')) |
124 if (preg_match('#[_-]#', $name)) { |
128 ); |
125 throw new Zend_Tool_Project_Provider_Exception('Controller names should be camel cased.'); |
|
126 } |
129 } |
127 |
130 |
128 $originalName = $name; |
131 $originalName = $name; |
129 $name = ucfirst($name); |
132 $name = ucfirst($name); |
130 |
133 |
131 // get request & response |
134 if (self::hasResource($this->_loadedProfile, $name, $module)) { |
132 $request = $this->_registry->getRequest(); |
135 throw new Zend_Tool_Project_Provider_Exception('This project already has a controller named ' . $name); |
133 $response = $this->_registry->getResponse(); |
136 } |
134 |
137 |
|
138 // Check that there is not a dash or underscore, return if doesnt match regex |
|
139 if (preg_match('#[_-]#', $name)) { |
|
140 throw new Zend_Tool_Project_Provider_Exception('Controller names should be camel cased.'); |
|
141 } |
|
142 |
135 try { |
143 try { |
136 $controllerResource = self::createResource($this->_loadedProfile, $name, $module); |
144 $controllerResource = self::createResource($this->_loadedProfile, $name, $module); |
137 if ($indexActionIncluded) { |
145 if ($indexActionIncluded) { |
138 $indexActionResource = Zend_Tool_Project_Provider_Action::createResource($this->_loadedProfile, 'index', $name, $module); |
146 $indexActionResource = Zend_Tool_Project_Provider_Action::createResource($this->_loadedProfile, 'index', $name, $module); |
139 $indexActionViewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, 'index', $name, $module); |
147 $indexActionViewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, 'index', $name, $module); |
140 } |
148 } |
141 if ($testingEnabled) { |
149 if ($testingEnabled) { |
142 $testControllerResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module); |
150 $testActionResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module); |
143 } |
151 } |
144 |
152 |
145 } catch (Exception $e) { |
153 } catch (Exception $e) { |
146 $response->setException($e); |
154 $response->setException($e); |
147 return; |
155 return; |
156 . ' not "' . $originalName . '" as supplied', |
164 . ' not "' . $originalName . '" as supplied', |
157 array('color' => array('yellow')) |
165 array('color' => array('yellow')) |
158 ); |
166 ); |
159 unset($tense); |
167 unset($tense); |
160 } |
168 } |
161 |
169 |
162 // do the creation |
170 // do the creation |
163 if ($request->isPretend()) { |
171 if ($request->isPretend()) { |
164 |
172 |
165 $response->appendContent('Would create a controller at ' . $controllerResource->getContext()->getPath()); |
173 $response->appendContent('Would create a controller at ' . $controllerResource->getContext()->getPath()); |
166 |
174 |
167 if (isset($indexActionResource)) { |
175 if (isset($indexActionResource)) { |
168 $response->appendContent('Would create an index action method in controller ' . $name); |
176 $response->appendContent('Would create an index action method in controller ' . $name); |
169 $response->appendContent('Would create a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath()); |
177 $response->appendContent('Would create a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath()); |
170 } |
178 } |
171 |
179 |
172 if ($testControllerResource) { |
180 if ($testingEnabled) { |
173 $response->appendContent('Would create a controller test file at ' . $testControllerResource->getContext()->getPath()); |
181 $response->appendContent('Would create a controller test file at ' . $testActionResource->getParentResource()->getContext()->getPath()); |
174 } |
182 } |
175 |
183 |
176 } else { |
184 } else { |
177 |
185 |
178 $response->appendContent('Creating a controller at ' . $controllerResource->getContext()->getPath()); |
186 $response->appendContent('Creating a controller at ' . $controllerResource->getContext()->getPath()); |
183 $indexActionResource->create(); |
191 $indexActionResource->create(); |
184 $response->appendContent('Creating a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath()); |
192 $response->appendContent('Creating a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath()); |
185 $indexActionViewResource->create(); |
193 $indexActionViewResource->create(); |
186 } |
194 } |
187 |
195 |
188 if ($testControllerResource) { |
196 if ($testingEnabled) { |
189 $response->appendContent('Creating a controller test file at ' . $testControllerResource->getContext()->getPath()); |
197 $response->appendContent('Creating a controller test file at ' . $testActionResource->getParentResource()->getContext()->getPath()); |
190 $testControllerResource->create(); |
198 $testActionResource->getParentResource()->create(); |
|
199 $testActionResource->create(); |
191 } |
200 } |
192 |
201 |
193 $this->_storeProfile(); |
202 $this->_storeProfile(); |
194 } |
203 } |
195 |
204 |