PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Tool/Project/Context/Zf/ControllerFile.php

https://bitbucket.org/netglue/zf-1.12-release
PHP | 223 lines | 120 code | 24 blank | 79 comment | 9 complexity | 81d09de74b2adaf4660289841bb25fe3 MD5 | raw file
  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-2012 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. * This class is the front most class for utilizing Zend_Tool_Project
  24. *
  25. * A profile is a hierarchical set of resources that keep track of
  26. * items within a specific project.
  27. *
  28. * @category Zend
  29. * @package Zend_Tool
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Tool_Project_Context_Zf_ControllerFile extends Zend_Tool_Project_Context_Filesystem_File
  34. {
  35. /**
  36. * @var string
  37. */
  38. protected $_controllerName = 'index';
  39. /**
  40. * @var string
  41. */
  42. protected $_moduleName = null;
  43. /**
  44. * @var string
  45. */
  46. protected $_filesystemName = 'controllerName';
  47. /**
  48. * init()
  49. *
  50. */
  51. public function init()
  52. {
  53. $this->_controllerName = $this->_resource->getAttribute('controllerName');
  54. $this->_moduleName = $this->_resource->getAttribute('moduleName');
  55. $this->_filesystemName = ucfirst($this->_controllerName) . 'Controller.php';
  56. parent::init();
  57. }
  58. /**
  59. * getPersistentAttributes
  60. *
  61. * @return array
  62. */
  63. public function getPersistentAttributes()
  64. {
  65. return array(
  66. 'controllerName' => $this->getControllerName()
  67. );
  68. }
  69. /**
  70. * getName()
  71. *
  72. * @return string
  73. */
  74. public function getName()
  75. {
  76. return 'ControllerFile';
  77. }
  78. /**
  79. * getControllerName()
  80. *
  81. * @return string
  82. */
  83. public function getControllerName()
  84. {
  85. return $this->_controllerName;
  86. }
  87. /**
  88. * getContents()
  89. *
  90. * @return string
  91. */
  92. public function getContents()
  93. {
  94. $filter = new Zend_Filter_Word_DashToCamelCase();
  95. $className = ($this->_moduleName) ? $filter->filter(ucfirst($this->_moduleName)) . '_' : '';
  96. $className .= ucfirst($this->_controllerName) . 'Controller';
  97. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  98. 'fileName' => $this->getPath(),
  99. 'classes' => array(
  100. new Zend_CodeGenerator_Php_Class(array(
  101. 'name' => $className,
  102. 'extendedClass' => 'Zend_Controller_Action',
  103. 'methods' => array(
  104. new Zend_CodeGenerator_Php_Method(array(
  105. 'name' => 'init',
  106. 'body' => '/* Initialize action controller here */',
  107. ))
  108. )
  109. ))
  110. )
  111. ));
  112. if ($className == 'ErrorController') {
  113. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  114. 'fileName' => $this->getPath(),
  115. 'classes' => array(
  116. new Zend_CodeGenerator_Php_Class(array(
  117. 'name' => $className,
  118. 'extendedClass' => 'Zend_Controller_Action',
  119. 'methods' => array(
  120. new Zend_CodeGenerator_Php_Method(array(
  121. 'name' => 'errorAction',
  122. 'body' => <<<EOS
  123. \$errors = \$this->_getParam('error_handler');
  124. if (!\$errors || !\$errors instanceof ArrayObject) {
  125. \$this->view->message = 'You have reached the error page';
  126. return;
  127. }
  128. switch (\$errors->type) {
  129. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
  130. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  131. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  132. // 404 error -- controller or action not found
  133. \$this->getResponse()->setHttpResponseCode(404);
  134. \$priority = Zend_Log::NOTICE;
  135. \$this->view->message = 'Page not found';
  136. break;
  137. default:
  138. // application error
  139. \$this->getResponse()->setHttpResponseCode(500);
  140. \$priority = Zend_Log::CRIT;
  141. \$this->view->message = 'Application error';
  142. break;
  143. }
  144. // Log exception, if logger available
  145. if (\$log = \$this->getLog()) {
  146. \$log->log(\$this->view->message, \$priority, \$errors->exception);
  147. \$log->log('Request Parameters', \$priority, \$errors->request->getParams());
  148. }
  149. // conditionally display exceptions
  150. if (\$this->getInvokeArg('displayExceptions') == true) {
  151. \$this->view->exception = \$errors->exception;
  152. }
  153. \$this->view->request = \$errors->request;
  154. EOS
  155. )),
  156. new Zend_CodeGenerator_Php_Method(array(
  157. 'name' => 'getLog',
  158. 'body' => <<<EOS
  159. \$bootstrap = \$this->getInvokeArg('bootstrap');
  160. if (!\$bootstrap->hasResource('Log')) {
  161. return false;
  162. }
  163. \$log = \$bootstrap->getResource('Log');
  164. return \$log;
  165. EOS
  166. )),
  167. )
  168. ))
  169. )
  170. ));
  171. }
  172. // store the generator into the registry so that the addAction command can use the same object later
  173. Zend_CodeGenerator_Php_File::registerFileCodeGenerator($codeGenFile); // REQUIRES filename to be set
  174. return $codeGenFile->generate();
  175. }
  176. /**
  177. * addAction()
  178. *
  179. * @param string $actionName
  180. */
  181. public function addAction($actionName)
  182. {
  183. $classCodeGen = $this->getCodeGenerator();
  184. $classCodeGen->setMethod(array('name' => $actionName . 'Action', 'body' => ' // action body here'));
  185. file_put_contents($this->getPath(), $classCodeGen->generate());
  186. }
  187. /**
  188. * getCodeGenerator()
  189. *
  190. * @return Zend_CodeGenerator_Php_Class
  191. */
  192. public function getCodeGenerator()
  193. {
  194. $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($this->getPath());
  195. $codeGenFileClasses = $codeGenFile->getClasses();
  196. $class = array_shift($codeGenFileClasses);
  197. return $class;
  198. }
  199. }