Action.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * @category Zend
  28. * @package Zend_Tool
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Tool_Project_Provider_Action extends Zend_Tool_Project_Provider_Abstract
  33. {
  34. /**
  35. * createResource()
  36. *
  37. * @param Zend_Tool_Project_Profile $profile
  38. * @param string $actionName
  39. * @param string $controllerName
  40. * @param string $moduleName
  41. * @return Zend_Tool_Project_Profile_Resource
  42. */
  43. public static function createResource(Zend_Tool_Project_Profile $profile, $actionName, $controllerName, $moduleName = null)
  44. {
  45. if (!is_string($actionName)) {
  46. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"actionName\" is the name of a action resource to create.');
  47. }
  48. if (!is_string($controllerName)) {
  49. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  50. }
  51. $controllerFile = self::_getControllerFileResource($profile, $controllerName, $moduleName);
  52. $actionMethod = $controllerFile->createResource('ActionMethod', array('actionName' => $actionName));
  53. return $actionMethod;
  54. }
  55. /**
  56. * hasResource()
  57. *
  58. * @param Zend_Tool_Project_Profile $profile
  59. * @param string $actionName
  60. * @param string $controllerName
  61. * @param string $moduleName
  62. * @return Zend_Tool_Project_Profile_Resource
  63. */
  64. public static function hasResource(Zend_Tool_Project_Profile $profile, $actionName, $controllerName, $moduleName = null)
  65. {
  66. if (!is_string($actionName)) {
  67. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"actionName\" is the name of a action resource to create.');
  68. }
  69. if (!is_string($controllerName)) {
  70. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Action::createResource() expects \"controllerName\" is the name of a controller resource to create.');
  71. }
  72. $controllerFile = self::_getControllerFileResource($profile, $controllerName, $moduleName);
  73. return (($controllerFile->search(array('actionMethod' => array('actionName' => $actionName)))) instanceof Zend_Tool_Project_Profile_Resource);
  74. }
  75. /**
  76. * _getControllerFileResource()
  77. *
  78. * @param Zend_Tool_Project_Profile $profile
  79. * @param string $controllerName
  80. * @param string $moduleName
  81. * @return Zend_Tool_Project_Profile_Resource
  82. */
  83. protected static function _getControllerFileResource(Zend_Tool_Project_Profile $profile, $controllerName, $moduleName = null)
  84. {
  85. $profileSearchParams = array();
  86. if ($moduleName != null && is_string($moduleName)) {
  87. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => $moduleName);
  88. }
  89. $profileSearchParams[] = 'controllersDirectory';
  90. $profileSearchParams['controllerFile'] = array('controllerName' => $controllerName);
  91. return $profile->search($profileSearchParams);
  92. }
  93. /**
  94. * create()
  95. *
  96. * @param string $name
  97. * @param string $controllerName
  98. * @param bool $viewIncluded
  99. */
  100. public function create($name, $controllerName = 'index', $viewIncluded = true)
  101. {
  102. $this->_loadProfile();
  103. if (self::hasResource($this->_loadedProfile, $name, $controllerName)) {
  104. throw new Zend_Tool_Project_Provider_Exception('This controller (' . $controllerName . ') already has an action named (' . $name . ')');
  105. }
  106. $actionMethod = self::createResource($this->_loadedProfile, $name, $controllerName);
  107. if ($this->_registry->getRequest()->isPretend()) {
  108. $this->_registry->getResponse()->appendContent(
  109. 'Would create an action named ' . $name .
  110. ' inside controller at ' . $actionMethod->getParentResource()->getContext()->getPath()
  111. );
  112. } else {
  113. $this->_registry->getResponse()->appendContent(
  114. 'Creating an action named ' . $name .
  115. ' inside controller at ' . $actionMethod->getParentResource()->getContext()->getPath()
  116. );
  117. $actionMethod->create();
  118. $this->_storeProfile();
  119. }
  120. if ($viewIncluded) {
  121. $viewResource = Zend_Tool_Project_Provider_View::createResource($this->_loadedProfile, $controllerName, $name);
  122. if ($this->_registry->getRequest()->isPretend()) {
  123. $this->_registry->getResponse()->appendContent(
  124. 'Would create a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath()
  125. );
  126. } else {
  127. $this->_registry->getResponse()->appendContent(
  128. 'Creating a view script for the ' . $name . ' action method at ' . $viewResource->getContext()->getPath()
  129. );
  130. $viewResource->create();
  131. }
  132. }
  133. }
  134. }