PageRenderTime 25ms CodeModel.GetById 20ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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

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