Action.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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-2010 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_Provider_Pretendable
  28. */
  29. require_once 'Zend/Tool/Framework/Provider/Pretendable.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Tool
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Tool_Project_Provider_Action
  37. extends Zend_Tool_Project_Provider_Abstract
  38. implements Zend_Tool_Framework_Provider_Pretendable
  39. {
  40. /**
  41. * createResource()
  42. *
  43. * @param Zend_Tool_Project_Profile $profile
  44. * @param string $actionName
  45. * @param string $controllerName
  46. * @param string $moduleName
  47. * @return Zend_Tool_Project_Profile_Resource
  48. */
  49. public static function createResource(Zend_Tool_Project_Profile $profile, $actionName, $controllerName, $moduleName = null)
  50. {
  51. if (!is_string($actionName)) {
  52. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"actionName\" is the name of a action resource to create.');
  53. }
  54. if (!is_string($controllerName)) {
  55. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  56. }
  57. $controllerFile = self::_getControllerFileResource($profile, $controllerName, $moduleName);
  58. $actionMethod = $controllerFile->createResource('ActionMethod', array('actionName' => $actionName));
  59. return $actionMethod;
  60. }
  61. /**
  62. * hasResource()
  63. *
  64. * @param Zend_Tool_Project_Profile $profile
  65. * @param string $actionName
  66. * @param string $controllerName
  67. * @param string $moduleName
  68. * @return Zend_Tool_Project_Profile_Resource
  69. */
  70. public static function hasResource(Zend_Tool_Project_Profile $profile, $actionName, $controllerName, $moduleName = null)
  71. {
  72. if (!is_string($actionName)) {
  73. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"actionName\" is the name of a action resource to create.');
  74. }
  75. if (!is_string($controllerName)) {
  76. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  77. }
  78. $controllerFile = self::_getControllerFileResource($profile, $controllerName, $moduleName);
  79. if ($controllerFile == null) {
  80. throw new Zend_Tool_Project_Provider_Exception('Controller ' . $controllerName . ' was not found.');
  81. }
  82. return (($controllerFile->search(array('actionMethod' => array('actionName' => $actionName)))) instanceof Zend_Tool_Project_Profile_Resource);
  83. }
  84. /**
  85. * _getControllerFileResource()
  86. *
  87. * @param Zend_Tool_Project_Profile $profile
  88. * @param string $controllerName
  89. * @param string $moduleName
  90. * @return Zend_Tool_Project_Profile_Resource
  91. */
  92. protected static function _getControllerFileResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
  93. {
  94. $profileSearchParams = array();
  95. if ($moduleName != null && is_string($moduleName)) {
  96. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  97. }
  98. $profileSearchParams[] = 'controllersDirectory';
  99. $profileSearchParams['controllerFile'] = array('controllerName' => $controllerName);
  100. return $profile->search($profileSearchParams);
  101. }
  102. /**
  103. * create()
  104. *
  105. * @param string $name Action name for controller, in camelCase format.
  106. * @param string $controllerName Controller name action should be applied to.
  107. * @param bool $viewIncluded Whether the view should the view be included.
  108. * @param string $module Module name action should be applied to.
  109. */
  110. public function create($name, $controllerName = 'Index', $viewIncluded = true, $module = null)
  111. {
  112. $this->_loadProfile();
  113. // Check that there is not a dash or underscore, return if doesnt match regex
  114. if (preg_match('#[_-]#', $name)) {
  115. throw new Zend_Tool_Project_Provider_Exception('Action names should be camel cased.');
  116. }
  117. $originalName = $name;
  118. $originalControllerName = $controllerName;
  119. // ensure it is camelCase (lower first letter)
  120. $name = strtolower(substr($name, 0, 1)) . substr($name, 1);
  121. // ensure controller is MixedCase
  122. $controllerName = ucfirst($controllerName);
  123. if (self::hasResource($this->_loadedProfile, $name, $controllerName, $module)) {
  124. throw new Zend_Tool_Project_Provider_Exception('This controller (' . $controllerName . ') already has an action named (' . $name . ')');
  125. }
  126. $actionMethod = self::createResource($this->_loadedProfile, $name, $controllerName, $module);
  127. // get request/response object
  128. $request = $this->_registry->getRequest();
  129. $response = $this->_registry->getResponse();
  130. // alert the user about inline converted names
  131. $tense = (($request->isPretend()) ? 'would be' : 'is');
  132. if ($name !== $originalName) {
  133. $response->appendContent(
  134. 'Note: The canonical action name that ' . $tense
  135. . ' used with other providers is "' . $name . '";'
  136. . ' not "' . $originalName . '" as supplied',
  137. array('color' => array('yellow'))
  138. );
  139. }
  140. if ($controllerName !== $originalControllerName) {
  141. $response->appendContent(
  142. 'Note: The canonical controller name that ' . $tense
  143. . ' used with other providers is "' . $controllerName . '";'
  144. . ' not "' . $originalControllerName . '" as supplied',
  145. array('color' => array('yellow'))
  146. );
  147. }
  148. unset($tense);
  149. if ($request->isPretend()) {
  150. $response->appendContent(
  151. 'Would create an action named ' . $name .
  152. ' inside controller at ' . $actionMethod->getParentResource()->getContext()->getPath()
  153. );
  154. } else {
  155. $response->appendContent(
  156. 'Creating an action named ' . $name .
  157. ' inside controller at ' . $actionMethod->getParentResource()->getContext()->getPath()
  158. );
  159. $actionMethod->create();
  160. $this->_storeProfile();
  161. }
  162. if ($viewIncluded) {
  163. $viewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, $name, $controllerName, $module);
  164. if ($this->_registry->getRequest()->isPretend()) {
  165. $response->appendContent(
  166. 'Would create a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath()
  167. );
  168. } else {
  169. $response->appendContent(
  170. 'Creating a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath()
  171. );
  172. $viewResource->create();
  173. $this->_storeProfile();
  174. }
  175. }
  176. }
  177. }