Action.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_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-2009 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. return (($controllerFile->search(array('actionMethod' => array('actionName' => $actionName)))) instanceof Zend_Tool_Project_Profile_Resource);
  80. }
  81. /**
  82. * _getControllerFileResource()
  83. *
  84. * @param Zend_Tool_Project_Profile $profile
  85. * @param string $controllerName
  86. * @param string $moduleName
  87. * @return Zend_Tool_Project_Profile_Resource
  88. */
  89. protected static function _getControllerFileResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
  90. {
  91. $profileSearchParams = array();
  92. if ($moduleName != null && is_string($moduleName)) {
  93. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  94. }
  95. $profileSearchParams[] = 'controllersDirectory';
  96. $profileSearchParams['controllerFile'] = array('controllerName' => $controllerName);
  97. return $profile->search($profileSearchParams);
  98. }
  99. /**
  100. * create()
  101. *
  102. * @param string $name
  103. * @param string $controllerName
  104. * @param bool $viewIncluded
  105. */
  106. public function create($name, $controllerName = 'index', $viewIncluded = true, $module = null)
  107. {
  108. $this->_loadProfile();
  109. if (self::hasResource($this->_loadedProfile, $name, $controllerName, $module)) {
  110. throw new Zend_Tool_Project_Provider_Exception('This controller (' . $controllerName . ') already has an action named (' . $name . ')');
  111. }
  112. $actionMethod = self::createResource($this->_loadedProfile, $name, $controllerName, $module);
  113. if ($this->_registry->getRequest()->isPretend()) {
  114. $this->_registry->getResponse()->appendContent(
  115. 'Would create an action named ' . $name .
  116. ' inside controller at ' . $actionMethod->getParentResource()->getContext()->getPath()
  117. );
  118. } else {
  119. $this->_registry->getResponse()->appendContent(
  120. 'Creating an action named ' . $name .
  121. ' inside controller at ' . $actionMethod->getParentResource()->getContext()->getPath()
  122. );
  123. $actionMethod->create();
  124. $this->_storeProfile();
  125. }
  126. if ($viewIncluded) {
  127. $viewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, $name, $controllerName, $module);
  128. if ($this->_registry->getRequest()->isPretend()) {
  129. $this->_registry->getResponse()->appendContent(
  130. 'Would create a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath()
  131. );
  132. } else {
  133. $this->_registry->getResponse()->appendContent(
  134. 'Creating a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath()
  135. );
  136. $viewResource->create();
  137. $this->_storeProfile();
  138. }
  139. }
  140. }
  141. }