Action.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. if (self::hasResource($this->_loadedProfile, $name, $controllerName, $module)) {
  114. throw new Zend_Tool_Project_Provider_Exception('This controller (' . $controllerName . ') already has an action named (' . $name . ')');
  115. }
  116. // Check that there is not a dash or underscore, return if doesnt match regex
  117. if (preg_match('#[_-]#', $name)) {
  118. throw new Zend_Tool_Project_Provider_Exception('Action names should be camel cased.');
  119. }
  120. // ensure it is camelCase (lower first letter)
  121. $name = strtolower(substr($name, 0, 1)) . substr($name, 1);
  122. $actionMethod = self::createResource($this->_loadedProfile, $name, $controllerName, $module);
  123. if ($this->_registry->getRequest()->isPretend()) {
  124. $this->_registry->getResponse()->appendContent(
  125. 'Would create an action named ' . $name .
  126. ' inside controller at ' . $actionMethod->getParentResource()->getContext()->getPath()
  127. );
  128. } else {
  129. $this->_registry->getResponse()->appendContent(
  130. 'Creating an action named ' . $name .
  131. ' inside controller at ' . $actionMethod->getParentResource()->getContext()->getPath()
  132. );
  133. $actionMethod->create();
  134. $this->_storeProfile();
  135. }
  136. if ($viewIncluded) {
  137. $viewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, $name, $controllerName, $module);
  138. if ($this->_registry->getRequest()->isPretend()) {
  139. $this->_registry->getResponse()->appendContent(
  140. 'Would create a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath()
  141. );
  142. } else {
  143. $this->_registry->getResponse()->appendContent(
  144. 'Creating a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath()
  145. );
  146. $viewResource->create();
  147. $this->_storeProfile();
  148. }
  149. }
  150. }
  151. }