Controller.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * @category Zend
  40. * @package Zend_Tool
  41. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Tool_Project_Provider_Controller extends Zend_Tool_Project_Provider_Abstract
  45. {
  46. /**
  47. * createResource will create the controllerFile resource at the appropriate location in the
  48. * profile. NOTE: it is your job to execute the create() method on the resource, as well as
  49. * store the profile when done.
  50. *
  51. * @param Zend_Tool_Project_Profile $profile
  52. * @param string $controllerName
  53. * @param string $moduleName
  54. * @return Zend_Tool_Project_Profile_Resource
  55. */
  56. public static function createResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
  57. {
  58. if (!is_string($controllerName)) {
  59. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  60. }
  61. $controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName);
  62. $newController = $controllersDirectory->createResource('controllerFile', array('controllerName' => $controllerName));
  63. return $newController;
  64. }
  65. /**
  66. * hasResource()
  67. *
  68. * @param Zend_Tool_Project_Profile $profile
  69. * @param string $controllerName
  70. * @param string $moduleName
  71. * @return Zend_Tool_Project_Profile_Resource
  72. */
  73. public static function hasResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
  74. {
  75. if (!is_string($controllerName)) {
  76. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Controller::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  77. }
  78. $controllersDirectory = self::_getControllersDirectoryResource($profile, $moduleName);
  79. return (($controllersDirectory->search(array('controllerFile' => array('controllerName' => $controllerName)))) instanceof Zend_Tool_Project_Profile_Resource);
  80. }
  81. /**
  82. * _getControllersDirectoryResource()
  83. *
  84. * @param Zend_Tool_Project_Profile $profile
  85. * @param string $moduleName
  86. * @return Zend_Tool_Project_Profile_Resource
  87. */
  88. protected static function _getControllersDirectoryResource(Zend_Tool_Project_Profile $profile, $moduleName = null)
  89. {
  90. $profileSearchParams = array();
  91. if ($moduleName != null && is_string($moduleName)) {
  92. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => $moduleName);
  93. }
  94. $profileSearchParams[] = 'controllersDirectory';
  95. return $profile->search($profileSearchParams);
  96. }
  97. /**
  98. * Enter description here...
  99. *
  100. * @param string $name The name of the controller to create.
  101. * @param bool $indexActionIncluded Whether or not to create the index action.
  102. */
  103. public function create($name, $indexActionIncluded = true)
  104. {
  105. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  106. // determine if testing is enabled in the project
  107. require_once 'Zend/Tool/Project/Provider/Test.php';
  108. $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
  109. if (self::hasResource($this->_loadedProfile, $name)) {
  110. throw new Zend_Tool_Project_Provider_Exception('This project already has a controller named ' . $name);
  111. }
  112. try {
  113. $controllerResource = self::createResource($this->_loadedProfile, $name);
  114. if ($indexActionIncluded) {
  115. $indexActionResource = Zend_Tool_Project_Provider_Action::createResource($this->_loadedProfile, 'index', $name);
  116. $indexActionViewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, $name, 'index');
  117. }
  118. if ($testingEnabled) {
  119. $testControllerResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index');
  120. }
  121. } catch (Exception $e) {
  122. $response = $this->_registry->getResponse();
  123. $response->setException($e);
  124. return;
  125. }
  126. // do the creation
  127. if ($this->_registry->getRequest()->isPretend()) {
  128. $this->_registry->getResponse()->appendContent('Would create a controller at ' . $controllerResource->getContext()->getPath());
  129. if (isset($indexActionResource)) {
  130. $this->_registry->getResponse()->appendContent('Would create an index action method in controller ' . $name);
  131. $this->_registry->getResponse()->appendContent('Would create a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath());
  132. }
  133. if ($testControllerResource) {
  134. $this->_registry->getResponse()->appendContent('Would create a controller test file at ' . $testControllerResource->getContext()->getPath());
  135. }
  136. } else {
  137. $this->_registry->getResponse()->appendContent('Creating a controller at ' . $controllerResource->getContext()->getPath());
  138. $controllerResource->create();
  139. if (isset($indexActionResource)) {
  140. $this->_registry->getResponse()->appendContent('Creating an index action method in controller ' . $name);
  141. $indexActionResource->create();
  142. $this->_registry->getResponse()->appendContent('Creating a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath());
  143. $indexActionViewResource->create();
  144. }
  145. if ($testControllerResource) {
  146. $this->_registry->getResponse()->appendContent('Creating a controller test file at ' . $testControllerResource->getContext()->getPath());
  147. $testControllerResource->create();
  148. }
  149. $this->_storeProfile();
  150. }
  151. }
  152. }