Controller.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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('controllerFile', array('controllerName' => $controllerName));
  76. return $newController;
  77. }
  78. /**
  79. * hasResource()
  80. *
  81. * @param Zend_Tool_Project_Profile $profile
  82. * @param string $controllerName
  83. * @param string $moduleName
  84. * @return Zend_Tool_Project_Profile_Resource
  85. */
  86. public static function hasResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
  87. {
  88. if (!is_string($controllerName)) {
  89. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  90. }
  91. $controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName);
  92. return (($controllersDirectory->search(array('controllerFile' => array('controllerName' => $controllerName)))) instanceof Zend_Tool_Project_Profile_Resource);
  93. }
  94. /**
  95. * _getControllersDirectoryResource()
  96. *
  97. * @param Zend_Tool_Project_Profile $profile
  98. * @param string $moduleName
  99. * @return Zend_Tool_Project_Profile_Resource
  100. */
  101. protected static function _getControllersDirectoryResource(Zend_Tool_Project_Profile $profile, $moduleName = null)
  102. {
  103. $profileSearchParams = array();
  104. if ($moduleName != null && is_string($moduleName)) {
  105. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  106. }
  107. $profileSearchParams[] = 'controllersDirectory';
  108. return $profile->search($profileSearchParams);
  109. }
  110. /**
  111. * Enter description here...
  112. *
  113. * @param string $name The name of the controller to create.
  114. * @param bool $indexActionIncluded Whether or not to create the index action.
  115. */
  116. public function create($name, $indexActionIncluded = true, $module = null)
  117. {
  118. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  119. // determine if testing is enabled in the project
  120. require_once 'Zend/Tool/Project/Provider/Test.php';
  121. $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
  122. if (self::hasResource($this->_loadedProfile, $name, $module)) {
  123. throw new Zend_Tool_Project_Provider_Exception('This project already has a controller named ' . $name);
  124. }
  125. try {
  126. $controllerResource = self::createResource($this->_loadedProfile, $name, $module);
  127. if ($indexActionIncluded) {
  128. $indexActionResource = Zend_Tool_Project_Provider_Action::createResource($this->_loadedProfile, 'index', $name, $module);
  129. $indexActionViewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, 'index', $name, $module);
  130. }
  131. if ($testingEnabled) {
  132. $testControllerResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module);
  133. }
  134. } catch (Exception $e) {
  135. $response = $this->_registry->getResponse();
  136. $response->setException($e);
  137. return;
  138. }
  139. // do the creation
  140. if ($this->_registry->getRequest()->isPretend()) {
  141. $this->_registry->getResponse()->appendContent('Would create a controller at ' . $controllerResource->getContext()->getPath());
  142. if (isset($indexActionResource)) {
  143. $this->_registry->getResponse()->appendContent('Would create an index action method in controller ' . $name);
  144. $this->_registry->getResponse()->appendContent('Would create a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath());
  145. }
  146. if ($testControllerResource) {
  147. $this->_registry->getResponse()->appendContent('Would create a controller test file at ' . $testControllerResource->getContext()->getPath());
  148. }
  149. } else {
  150. $this->_registry->getResponse()->appendContent('Creating a controller at ' . $controllerResource->getContext()->getPath());
  151. $controllerResource->create();
  152. if (isset($indexActionResource)) {
  153. $this->_registry->getResponse()->appendContent('Creating an index action method in controller ' . $name);
  154. $indexActionResource->create();
  155. $this->_registry->getResponse()->appendContent('Creating a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath());
  156. $indexActionViewResource->create();
  157. }
  158. if ($testControllerResource) {
  159. $this->_registry->getResponse()->appendContent('Creating a controller test file at ' . $testControllerResource->getContext()->getPath());
  160. $testControllerResource->create();
  161. }
  162. $this->_storeProfile();
  163. }
  164. }
  165. }