2
0

Controller.php 7.6 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. * @category Zend
  24. * @package Zend_Tool
  25. * @copyright Copyright (c) 2005-2009 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 Zend_Tool_Project_Profile_Resource
  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->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. // determine if testing is enabled in the project
  103. require_once 'Zend/Tool/Project/Provider/Test.php';
  104. $testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
  105. if (self::hasResource($this->_loadedProfile, $name, $module)) {
  106. throw new Zend_Tool_Project_Provider_Exception('This project already has a controller named ' . $name);
  107. }
  108. // Check that there is not a dash or underscore, return if doesnt match regex
  109. if (preg_match('#[_-]#', $name)) {
  110. throw new Zend_Tool_Project_Provider_Exception('Controller names should be camel cased.');
  111. }
  112. $name = ucwords($name);
  113. try {
  114. $controllerResource = self::createResource($this->_loadedProfile, $name, $module);
  115. if ($indexActionIncluded) {
  116. $indexActionResource = Zend_Tool_Project_Provider_Action::createResource($this->_loadedProfile, 'index', $name, $module);
  117. $indexActionViewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, 'index', $name, $module);
  118. }
  119. if ($testingEnabled) {
  120. $testControllerResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module);
  121. }
  122. } catch (Exception $e) {
  123. $response = $this->_registry->getResponse();
  124. $response->setException($e);
  125. return;
  126. }
  127. // do the creation
  128. if ($this->_registry->getRequest()->isPretend()) {
  129. $this->_registry->getResponse()->appendContent('Would create a controller at ' . $controllerResource->getContext()->getPath());
  130. if (isset($indexActionResource)) {
  131. $this->_registry->getResponse()->appendContent('Would create an index action method in controller ' . $name);
  132. $this->_registry->getResponse()->appendContent('Would create a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath());
  133. }
  134. if ($testControllerResource) {
  135. $this->_registry->getResponse()->appendContent('Would create a controller test file at ' . $testControllerResource->getContext()->getPath());
  136. }
  137. } else {
  138. $this->_registry->getResponse()->appendContent('Creating a controller at ' . $controllerResource->getContext()->getPath());
  139. $controllerResource->create();
  140. if (isset($indexActionResource)) {
  141. $this->_registry->getResponse()->appendContent('Creating an index action method in controller ' . $name);
  142. $indexActionResource->create();
  143. $this->_registry->getResponse()->appendContent('Creating a view script for the index action method at ' . $indexActionViewResource->getContext()->getPath());
  144. $indexActionViewResource->create();
  145. }
  146. if ($testControllerResource) {
  147. $this->_registry->getResponse()->appendContent('Creating a controller test file at ' . $testControllerResource->getContext()->getPath());
  148. $testControllerResource->create();
  149. }
  150. $this->_storeProfile();
  151. }
  152. }
  153. }