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