PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Zend/Tool/Project/Context/Zf/ControllerFile.php

https://bitbucket.org/micromax/vox-fw
PHP | 221 lines | 119 code | 23 blank | 79 comment | 9 complexity | f1648290bf28e40299ae0d2d769eda4b 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: ControllerFile.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. $className = ($this->_moduleName) ? ucfirst($this->_moduleName) . '_' : '';
  95. $className .= ucfirst($this->_controllerName) . 'Controller';
  96. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  97. 'fileName' => $this->getPath(),
  98. 'classes' => array(
  99. new Zend_CodeGenerator_Php_Class(array(
  100. 'name' => $className,
  101. 'extendedClass' => 'Zend_Controller_Action',
  102. 'methods' => array(
  103. new Zend_CodeGenerator_Php_Method(array(
  104. 'name' => 'init',
  105. 'body' => '/* Initialize action controller here */',
  106. ))
  107. )
  108. ))
  109. )
  110. ));
  111. if ($className == 'ErrorController') {
  112. $codeGenFile = new Zend_CodeGenerator_Php_File(array(
  113. 'fileName' => $this->getPath(),
  114. 'classes' => array(
  115. new Zend_CodeGenerator_Php_Class(array(
  116. 'name' => $className,
  117. 'extendedClass' => 'Zend_Controller_Action',
  118. 'methods' => array(
  119. new Zend_CodeGenerator_Php_Method(array(
  120. 'name' => 'errorAction',
  121. 'body' => <<<EOS
  122. \$errors = \$this->_getParam('error_handler');
  123. if (!\$errors || !\$errors instanceof ArrayObject) {
  124. \$this->view->message = 'You have reached the error page';
  125. return;
  126. }
  127. switch (\$errors->type) {
  128. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
  129. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  130. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  131. // 404 error -- controller or action not found
  132. \$this->getResponse()->setHttpResponseCode(404);
  133. \$priority = Zend_Log::NOTICE;
  134. \$this->view->message = 'Page not found';
  135. break;
  136. default:
  137. // application error
  138. \$this->getResponse()->setHttpResponseCode(500);
  139. \$priority = Zend_Log::CRIT;
  140. \$this->view->message = 'Application error';
  141. break;
  142. }
  143. // Log exception, if logger available
  144. if (\$log = \$this->getLog()) {
  145. \$log->log(\$this->view->message, \$priority, \$errors->exception);
  146. \$log->log('Request Parameters', \$priority, \$errors->request->getParams());
  147. }
  148. // conditionally display exceptions
  149. if (\$this->getInvokeArg('displayExceptions') == true) {
  150. \$this->view->exception = \$errors->exception;
  151. }
  152. \$this->view->request = \$errors->request;
  153. EOS
  154. )),
  155. new Zend_CodeGenerator_Php_Method(array(
  156. 'name' => 'getLog',
  157. 'body' => <<<EOS
  158. \$bootstrap = \$this->getInvokeArg('bootstrap');
  159. if (!\$bootstrap->hasResource('Log')) {
  160. return false;
  161. }
  162. \$log = \$bootstrap->getResource('Log');
  163. return \$log;
  164. EOS
  165. )),
  166. )
  167. ))
  168. )
  169. ));
  170. }
  171. // store the generator into the registry so that the addAction command can use the same object later
  172. Zend_CodeGenerator_Php_File::registerFileCodeGenerator($codeGenFile); // REQUIRES filename to be set
  173. return $codeGenFile->generate();
  174. }
  175. /**
  176. * addAction()
  177. *
  178. * @param string $actionName
  179. */
  180. public function addAction($actionName)
  181. {
  182. $classCodeGen = $this->getCodeGenerator();
  183. $classCodeGen->setMethod(array('name' => $actionName . 'Action', 'body' => ' // action body here'));
  184. file_put_contents($this->getPath(), $classCodeGen->generate());
  185. }
  186. /**
  187. * getCodeGenerator()
  188. *
  189. * @return Zend_CodeGenerator_Php_Class
  190. */
  191. public function getCodeGenerator()
  192. {
  193. $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($this->getPath());
  194. $codeGenFileClasses = $codeGenFile->getClasses();
  195. $class = array_shift($codeGenFileClasses);
  196. return $class;
  197. }
  198. }