ActionMethod.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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-2014 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_Context_Interface
  24. */
  25. require_once 'Zend/Tool/Project/Context/Interface.php';
  26. /**
  27. * @see Zend_Reflection_File
  28. */
  29. require_once 'Zend/Reflection/File.php';
  30. /**
  31. * This class is the front most class for utilizing Zend_Tool_Project
  32. *
  33. * A profile is a hierarchical set of resources that keep track of
  34. * items within a specific project.
  35. *
  36. * @category Zend
  37. * @package Zend_Tool
  38. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Tool_Project_Context_Zf_ActionMethod implements Zend_Tool_Project_Context_Interface
  42. {
  43. /**
  44. * @var Zend_Tool_Project_Profile_Resource
  45. */
  46. protected $_resource = null;
  47. /**
  48. * @var Zend_Tool_Project_Profile_Resource
  49. */
  50. protected $_controllerResource = null;
  51. /**
  52. * @var string
  53. */
  54. protected $_controllerPath = '';
  55. /**
  56. * @var string
  57. */
  58. protected $_actionName = null;
  59. /**
  60. * init()
  61. *
  62. * @return Zend_Tool_Project_Context_Zf_ActionMethod
  63. */
  64. public function init()
  65. {
  66. $this->_actionName = $this->_resource->getAttribute('actionName');
  67. $this->_resource->setAppendable(false);
  68. $this->_controllerResource = $this->_resource->getParentResource();
  69. if (!$this->_controllerResource->getContext() instanceof Zend_Tool_Project_Context_Zf_ControllerFile) {
  70. require_once 'Zend/Tool/Project/Context/Exception.php';
  71. throw new Zend_Tool_Project_Context_Exception('ActionMethod must be a sub resource of a ControllerFile');
  72. }
  73. // make the ControllerFile node appendable so we can tack on the actionMethod.
  74. $this->_resource->getParentResource()->setAppendable(true);
  75. $this->_controllerPath = $this->_controllerResource->getContext()->getPath();
  76. /*
  77. * This code block is now commented, its doing to much for init()
  78. *
  79. if ($this->_controllerPath != '' && self::hasActionMethod($this->_controllerPath, $this->_actionName)) {
  80. require_once 'Zend/Tool/Project/Context/Exception.php';
  81. throw new Zend_Tool_Project_Context_Exception('An action named ' . $this->_actionName . 'Action already exists in this controller');
  82. }
  83. */
  84. return $this;
  85. }
  86. /**
  87. * getPersistentAttributes
  88. *
  89. * @return array
  90. */
  91. public function getPersistentAttributes()
  92. {
  93. return array(
  94. 'actionName' => $this->getActionName()
  95. );
  96. }
  97. /**
  98. * getName()
  99. *
  100. * @return string
  101. */
  102. public function getName()
  103. {
  104. return 'ActionMethod';
  105. }
  106. /**
  107. * setResource()
  108. *
  109. * @param Zend_Tool_Project_Profile_Resource $resource
  110. * @return Zend_Tool_Project_Context_Zf_ActionMethod
  111. */
  112. public function setResource(Zend_Tool_Project_Profile_Resource $resource)
  113. {
  114. $this->_resource = $resource;
  115. return $this;
  116. }
  117. /**
  118. * setActionName()
  119. *
  120. * @param string $actionName
  121. * @return Zend_Tool_Project_Context_Zf_ActionMethod
  122. */
  123. public function setActionName($actionName)
  124. {
  125. $this->_actionName = $actionName;
  126. return $this;
  127. }
  128. /**
  129. * getActionName()
  130. *
  131. * @return string
  132. */
  133. public function getActionName()
  134. {
  135. return $this->_actionName;
  136. }
  137. /**
  138. * create()
  139. *
  140. * @return Zend_Tool_Project_Context_Zf_ActionMethod
  141. */
  142. public function create()
  143. {
  144. if (self::createActionMethod($this->_controllerPath, $this->_actionName) === false) {
  145. require_once 'Zend/Tool/Project/Context/Exception.php';
  146. throw new Zend_Tool_Project_Context_Exception(
  147. 'Could not create action within controller ' . $this->_controllerPath
  148. . ' with action name ' . $this->_actionName
  149. );
  150. }
  151. return $this;
  152. }
  153. /**
  154. * delete()
  155. *
  156. * @return Zend_Tool_Project_Context_Zf_ActionMethod
  157. */
  158. public function delete()
  159. {
  160. // @todo do this
  161. return $this;
  162. }
  163. /**
  164. * createAcionMethod()
  165. *
  166. * @param string $controllerPath
  167. * @param string $actionName
  168. * @param string $body
  169. * @return true
  170. */
  171. public static function createActionMethod($controllerPath, $actionName, $body = ' // action body')
  172. {
  173. if (!file_exists($controllerPath)) {
  174. return false;
  175. }
  176. $controllerCodeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($controllerPath, true, true);
  177. $controllerCodeGenFile->getClass()->setMethod(array(
  178. 'name' => $actionName . 'Action',
  179. 'body' => $body
  180. ));
  181. file_put_contents($controllerPath, $controllerCodeGenFile->generate());
  182. return true;
  183. }
  184. /**
  185. * hasActionMethod()
  186. *
  187. * @param string $controllerPath
  188. * @param string $actionName
  189. * @return bool
  190. */
  191. public static function hasActionMethod($controllerPath, $actionName)
  192. {
  193. if (!file_exists($controllerPath)) {
  194. return false;
  195. }
  196. $controllerCodeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($controllerPath, true, true);
  197. return $controllerCodeGenFile->getClass()->hasMethod($actionName . 'Action');
  198. }
  199. }