ControllerFile.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_Context_Filesystem_File
  24. */
  25. require_once 'Zend/Tool/Project/Context/Filesystem/File.php';
  26. /**
  27. * @see Zend_CodeGenerator_Php_File
  28. */
  29. require_once 'Zend/CodeGenerator/Php/File.php';
  30. /**
  31. * @see Zend_Filter_Word_DashToCamelCase
  32. */
  33. require_once 'Zend/Filter/Word/DashToCamelCase.php';
  34. /**
  35. * This class is the front most class for utilizing Zend_Tool_Project
  36. *
  37. * A profile is a hierarchical set of resources that keep track of
  38. * items within a specific project.
  39. *
  40. * @category Zend
  41. * @package Zend_Tool
  42. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. */
  45. class Zend_Tool_Project_Context_Zf_ControllerFile extends Zend_Tool_Project_Context_Filesystem_File
  46. {
  47. /**
  48. * @var string
  49. */
  50. protected $_controllerName = 'index';
  51. /**
  52. * @var string
  53. */
  54. protected $_filesystemName = 'controllerName';
  55. /**
  56. * init()
  57. *
  58. * @return Zend_Tool_Project_Context_Zf_ControllerFile
  59. */
  60. public function init()
  61. {
  62. $this->_controllerName = $this->_resource->getAttribute('controllerName');
  63. $this->_filesystemName = ucfirst($this->_controllerName) . 'Controller.php';
  64. parent::init();
  65. return $this;
  66. }
  67. /**
  68. * getPersistentAttributes
  69. *
  70. * @return array
  71. */
  72. public function getPersistentAttributes()
  73. {
  74. return array(
  75. 'controllerName' => $this->getControllerName()
  76. );
  77. }
  78. /**
  79. * getName()
  80. *
  81. * @return string
  82. */
  83. public function getName()
  84. {
  85. return 'ControllerFile';
  86. }
  87. /**
  88. * getControllerName()
  89. *
  90. * @return string
  91. */
  92. public function getControllerName()
  93. {
  94. return $this->_controllerName;
  95. }
  96. /**
  97. * getContents()
  98. *
  99. * @return string
  100. */
  101. public function getContents()
  102. {
  103. $filter = new Zend_Filter_Word_DashToCamelCase();
  104. $className = $filter->filter($this->_controllerName) . 'Controller';
  105. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  106. 'fileName' => $this->getPath(),
  107. 'classes' => array(
  108. new Zend_CodeGenerator_Php_Class(array(
  109. 'name' => $className,
  110. 'extendedClass' => 'Zend_Controller_Action',
  111. 'methods' => array(
  112. new Zend_CodeGenerator_Php_Method(array(
  113. 'name' => 'init',
  114. 'body' => '/* Initialize action controller here */',
  115. ))
  116. )
  117. ))
  118. )
  119. ));
  120. if ($className == 'ErrorController') {
  121. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  122. 'fileName' => $this->getPath(),
  123. 'classes' => array(
  124. new Zend_CodeGenerator_Php_Class(array(
  125. 'name' => $className,
  126. 'extendedClass' => 'Zend_Controller_Action',
  127. 'methods' => array(
  128. new Zend_CodeGenerator_Php_Method(array(
  129. 'name' => 'errorAction',
  130. 'body' => <<<EOS
  131. \$errors = \$this->_getParam('error_handler');
  132. switch (\$errors->type) {
  133. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  134. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  135. // 404 error -- controller or action not found
  136. \$this->getResponse()->setHttpResponseCode(404);
  137. \$this->view->message = 'Page not found';
  138. break;
  139. default:
  140. // application error
  141. \$this->getResponse()->setHttpResponseCode(500);
  142. \$this->view->message = 'Application error';
  143. break;
  144. }
  145. \$this->view->exception = \$errors->exception;
  146. \$this->view->request = \$errors->request;
  147. EOS
  148. ))
  149. )
  150. ))
  151. )
  152. ));
  153. }
  154. // store the generator into the registry so that the addAction command can use the same object later
  155. Zend_CodeGenerator_Php_File::registerFileCodeGenerator($codeGenFile); // REQUIRES filename to be set
  156. return $codeGenFile->generate();
  157. }
  158. /**
  159. * addAction()
  160. *
  161. * @param string $actionName
  162. */
  163. public function addAction($actionName)
  164. {
  165. $class = $this->getCodeGenerator();
  166. $class->setMethod(array('name' => $actionName . 'Action', 'body' => ' // action body here'));
  167. file_put_contents($this->getPath(), $codeGenFile->generate());
  168. }
  169. /**
  170. * getCodeGenerator()
  171. *
  172. * @return Zend_CodeGenerator_Php_Class
  173. */
  174. public function getCodeGenerator()
  175. {
  176. $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($this->getPath());
  177. $codeGenFileClasses = $codeGenFile->getClasses();
  178. $class = array_shift($codeGenFileClasses);
  179. return $class;
  180. }
  181. }