File indexing completed on 2024-06-23 05:55:50

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Tool
0017  * @subpackage Framework
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * This class is the front most class for utilizing Zend_Tool_Project
0025  *
0026  * A profile is a hierarchical set of resources that keep track of
0027  * items within a specific project.
0028  *
0029  * @category   Zend
0030  * @package    Zend_Tool
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_Tool_Project_Context_Zf_ControllerFile extends Zend_Tool_Project_Context_Filesystem_File
0035 {
0036 
0037     /**
0038      * @var string
0039      */
0040     protected $_controllerName = 'index';
0041 
0042     /**
0043      * @var string
0044      */
0045     protected $_moduleName = null;
0046 
0047     /**
0048      * @var string
0049      */
0050     protected $_filesystemName = 'controllerName';
0051 
0052     /**
0053      * init()
0054      *
0055      */
0056     public function init()
0057     {
0058         $this->_controllerName = $this->_resource->getAttribute('controllerName');
0059         $this->_moduleName = $this->_resource->getAttribute('moduleName');
0060         $this->_filesystemName = ucfirst($this->_controllerName) . 'Controller.php';
0061         parent::init();
0062     }
0063 
0064     /**
0065      * getPersistentAttributes
0066      *
0067      * @return array
0068      */
0069     public function getPersistentAttributes()
0070     {
0071         return array(
0072             'controllerName' => $this->getControllerName()
0073             );
0074     }
0075 
0076     /**
0077      * getName()
0078      *
0079      * @return string
0080      */
0081     public function getName()
0082     {
0083         return 'ControllerFile';
0084     }
0085 
0086     /**
0087      * getControllerName()
0088      *
0089      * @return string
0090      */
0091     public function getControllerName()
0092     {
0093         return $this->_controllerName;
0094     }
0095 
0096     /**
0097      * getContents()
0098      *
0099      * @return string
0100      */
0101     public function getContents()
0102     {
0103         $filter = new Zend_Filter_Word_DashToCamelCase();
0104         
0105         $className = ($this->_moduleName) ? $filter->filter(ucfirst($this->_moduleName)) . '_' : '';
0106         $className .= ucfirst($this->_controllerName) . 'Controller';
0107 
0108         $codeGenFile = new Zend_CodeGenerator_Php_File(array(
0109             'fileName' => $this->getPath(),
0110             'classes' => array(
0111                 new Zend_CodeGenerator_Php_Class(array(
0112                     'name' => $className,
0113                     'extendedClass' => 'Zend_Controller_Action',
0114                     'methods' => array(
0115                         new Zend_CodeGenerator_Php_Method(array(
0116                             'name' => 'init',
0117                             'body' => '/* Initialize action controller here */',
0118                             ))
0119                         )
0120                     ))
0121                 )
0122             ));
0123 
0124 
0125         if ($className == 'ErrorController') {
0126 
0127             $codeGenFile = new Zend_CodeGenerator_Php_File(array(
0128                 'fileName' => $this->getPath(),
0129                 'classes' => array(
0130                     new Zend_CodeGenerator_Php_Class(array(
0131                         'name' => $className,
0132                         'extendedClass' => 'Zend_Controller_Action',
0133                         'methods' => array(
0134                             new Zend_CodeGenerator_Php_Method(array(
0135                                 'name' => 'errorAction',
0136                                 'body' => <<<EOS
0137 \$errors = \$this->_getParam('error_handler');
0138 
0139 if (!\$errors || !\$errors instanceof ArrayObject) {
0140     \$this->view->message = 'You have reached the error page';
0141     return;
0142 }
0143 
0144 switch (\$errors->type) {
0145     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
0146     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
0147     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
0148         // 404 error -- controller or action not found
0149         \$this->getResponse()->setHttpResponseCode(404);
0150         \$priority = Zend_Log::NOTICE;
0151         \$this->view->message = 'Page not found';
0152         break;
0153     default:
0154         // application error
0155         \$this->getResponse()->setHttpResponseCode(500);
0156         \$priority = Zend_Log::CRIT;
0157         \$this->view->message = 'Application error';
0158         break;
0159 }
0160 
0161 // Log exception, if logger available
0162 if (\$log = \$this->getLog()) {
0163     \$log->log(\$this->view->message, \$priority, \$errors->exception);
0164     \$log->log('Request Parameters', \$priority, \$errors->request->getParams());
0165 }
0166 
0167 // conditionally display exceptions
0168 if (\$this->getInvokeArg('displayExceptions') == true) {
0169     \$this->view->exception = \$errors->exception;
0170 }
0171 
0172 \$this->view->request   = \$errors->request;
0173 EOS
0174                                 )),
0175                             new Zend_CodeGenerator_Php_Method(array(
0176                                 'name' => 'getLog',
0177                                 'body' => <<<EOS
0178 \$bootstrap = \$this->getInvokeArg('bootstrap');
0179 if (!\$bootstrap->hasResource('Log')) {
0180     return false;
0181 }
0182 \$log = \$bootstrap->getResource('Log');
0183 return \$log;
0184 EOS
0185                                 )),
0186                             )
0187                         ))
0188                     )
0189                 ));
0190 
0191         }
0192 
0193         // store the generator into the registry so that the addAction command can use the same object later
0194         Zend_CodeGenerator_Php_File::registerFileCodeGenerator($codeGenFile); // REQUIRES filename to be set
0195         return $codeGenFile->generate();
0196     }
0197 
0198     /**
0199      * addAction()
0200      *
0201      * @param string $actionName
0202      */
0203     public function addAction($actionName)
0204     {
0205         $classCodeGen = $this->getCodeGenerator();
0206         $classCodeGen->setMethod(array('name' => $actionName . 'Action', 'body' => '        // action body here'));
0207         file_put_contents($this->getPath(), $classCodeGen->generate());
0208     }
0209 
0210     /**
0211      * getCodeGenerator()
0212      *
0213      * @return Zend_CodeGenerator_Php_Class
0214      */
0215     public function getCodeGenerator()
0216     {
0217         $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($this->getPath());
0218         $codeGenFileClasses = $codeGenFile->getClasses();
0219         $class = array_shift($codeGenFileClasses);
0220         return $class;
0221     }
0222 
0223 }