Controller.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Tool
  25. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class Zend_Tool_Project_Provider_Controller
  29. extends Zend_Tool_Project_Provider_Abstract
  30. implements Zend_Tool_Framework_Provider_Pretendable
  31. {
  32. /**
  33. * createResource will create the controllerFile resource at the appropriate location in the
  34. * profile. NOTE: it is your job to execute the create() method on the resource, as well as
  35. * store the profile when done.
  36. *
  37. * @param Zend_Tool_Project_Profile $profile
  38. * @param string $controllerName
  39. * @param string $moduleName
  40. * @return Zend_Tool_Project_Profile_Resource
  41. */
  42. public static function createResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
  43. {
  44. if (!is_string($controllerName)) {
  45. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  46. }
  47. if (!($controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName))) {
  48. if ($moduleName) {
  49. $exceptionMessage = 'A controller directory for module "' . $moduleName . '" was not found.';
  50. } else {
  51. $exceptionMessage = 'A controller directory was not found.';
  52. }
  53. throw new Zend_Tool_Project_Provider_Exception($exceptionMessage);
  54. }
  55. $newController = $controllersDirectory->createResource(
  56. 'controllerFile',
  57. array('controllerName' => $controllerName, 'moduleName' => $moduleName)
  58. );
  59. return $newController;
  60. }
  61. /**
  62. * hasResource()
  63. *
  64. * @param Zend_Tool_Project_Profile $profile
  65. * @param string $controllerName
  66. * @param string $moduleName
  67. * @return boolean
  68. */
  69. public static function hasResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
  70. {
  71. if (!is_string($controllerName)) {
  72. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  73. }
  74. $controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName);
  75. return ($controllersDirectory &&($controllersDirectory->search(array('controllerFile' => array('controllerName' => $controllerName)))) instanceof Zend_Tool_Project_Profile_Resource);
  76. }
  77. /**
  78. * _getControllersDirectoryResource()
  79. *
  80. * @param Zend_Tool_Project_Profile $profile
  81. * @param string $moduleName
  82. * @return Zend_Tool_Project_Profile_Resource
  83. */
  84. protected static function _getControllersDirectoryResource(Zend_Tool_Project_Profile $profile, $moduleName = null)
  85. {
  86. $profileSearchParams = array();
  87. if ($moduleName != null && is_string($moduleName)) {
  88. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  89. }
  90. $profileSearchParams[] = 'controllersDirectory';
  91. return $profile->search($profileSearchParams);
  92. }
  93. /**
  94. * Create a new controller
  95. *
  96. * @param string $name The name of the controller to create, in camelCase.
  97. * @param bool $indexActionIncluded Whether or not to create the index action.
  98. */
  99. public function create($name, $indexActionIncluded = true, $module = null)
  100. {
  101. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  102. // get request & response
  103. $request = $this->_registry->getRequest();
  104. $response = $this->_registry->getResponse();
  105. // determine if testing is enabled in the project
  106. require_once 'Zend/Tool/Project/Provider/Test.php';
  107. $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
  108. if ($testingEnabled && !Zend_Tool_Project_Provider_Test::isPHPUnitAvailable()) {
  109. $testingEnabled = false;
  110. $response->appendContent(
  111. 'Note: PHPUnit is required in order to generate controller test stubs.',
  112. array('color' => array('yellow'))
  113. );
  114. }
  115. $originalName = $name;
  116. $name = ucfirst($name);
  117. if (self::hasResource($this->_loadedProfile, $name, $module)) {
  118. throw new Zend_Tool_Project_Provider_Exception('This project already has a controller named ' . $name);
  119. }
  120. // Check that there is not a dash or underscore, return if doesnt match regex
  121. if (preg_match('#[_-]#', $name)) {
  122. throw new Zend_Tool_Project_Provider_Exception('Controller names should be camel cased.');
  123. }
  124. try {
  125. $controllerResource = self::createResource($this->_loadedProfile, $name, $module);
  126. if ($indexActionIncluded) {
  127. $indexActionResource = Zend_Tool_Project_Provider_Action::createResource($this->_loadedProfile, 'index', $name, $module);
  128. $indexActionViewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, 'index', $name, $module);
  129. }
  130. if ($testingEnabled) {
  131. $testActionResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module);
  132. }
  133. } catch (Exception $e) {
  134. $response->setException($e);
  135. return;
  136. }
  137. // determime if we need to note to the user about the name
  138. if (($name !== $originalName)) {
  139. $tense = (($request->isPretend()) ? 'would be' : 'is');
  140. $response->appendContent(
  141. 'Note: The canonical controller name that ' . $tense
  142. . ' used with other providers is "' . $name . '";'
  143. . ' not "' . $originalName . '" as supplied',
  144. array('color' => array('yellow'))
  145. );
  146. unset($tense);
  147. }
  148. // do the creation
  149. if ($request->isPretend()) {
  150. $response->appendContent('Would create a controller at ' . $controllerResource->getContext()->getPath());
  151. if (isset($indexActionResource)) {
  152. $response->appendContent('Would create an index action method in controller ' . $name);
  153. $response->appendContent('Would create a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath());
  154. }
  155. if ($testingEnabled) {
  156. $response->appendContent('Would create a controller test file at ' . $testActionResource->getParentResource()->getContext()->getPath());
  157. }
  158. } else {
  159. $response->appendContent('Creating a controller at ' . $controllerResource->getContext()->getPath());
  160. $controllerResource->create();
  161. if (isset($indexActionResource)) {
  162. $response->appendContent('Creating an index action method in controller ' . $name);
  163. $indexActionResource->create();
  164. $response->appendContent('Creating a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath());
  165. $indexActionViewResource->create();
  166. }
  167. if ($testingEnabled) {
  168. $response->appendContent('Creating a controller test file at ' . $testActionResource->getParentResource()->getContext()->getPath());
  169. $testActionResource->getParentResource()->create();
  170. $testActionResource->create();
  171. }
  172. $this->_storeProfile();
  173. }
  174. }
  175. }