PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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