Exemple #1
0
    /**
     * getContents()
     *
     * @return string
     */
    public function getContents()
    {
        $className = $this->_moduleName ? ucfirst($this->_moduleName) . '\\' : '';
        $className .= ucfirst($this->_controllerName) . 'Controller';
        $codeGenFile = new Php\PhpFile(array('fileName' => $this->getPath(), 'classes' => array(new Php\PhpClass(array('name' => $className, 'extendedClass' => 'Zend\\Controller\\Action', 'methods' => array(new Php\PhpMethod(array('name' => 'init', 'body' => '/* Initialize action controller here */'))))))));
        if ($className == 'ErrorController') {
            $codeGenFile = new Php\PhpFile(array('fileName' => $this->getPath(), 'classes' => array(new Php\PhpClass(array('name' => $className, 'extendedClass' => 'Zend\\Controller\\Action', 'methods' => array(new Php\PhpMethod(array('name' => 'errorAction', 'body' => <<<'EOS'
$errors = $this->_getParam('error_handler');

switch ($errors->type) {
    case \Zend\Controller\Plugin\ErrorHandler::EXCEPTION_NO_ROUTE:
    case \Zend\Controller\Plugin\ErrorHandler::EXCEPTION_NO_CONTROLLER:
    case \Zend\Controller\Plugin\ErrorHandler::EXCEPTION_NO_ACTION:

        // 404 error -- controller or action not found
        $this->getResponse()->setHttpResponseCode(404);
        $this->view->message = 'Page not found';
        break;
    default:
        // application error
        $this->getResponse()->setHttpResponseCode(500);
        $this->view->message = 'Application error';
        break;
}

// Log exception, if logger available
if (($log = $this->getLog())) {
    $log->crit($this->view->message, $errors->exception);
}

// conditionally display exceptions
if ($this->getInvokeArg('displayExceptions') == true) {
    $this->view->exception = $errors->exception;
}

$this->view->request = $errors->request;
EOS
)), new Php\PhpMethod(array('name' => 'getLog', 'body' => <<<'EOS'
$bootstrap = $this->getInvokeArg('bootstrap');
if (!$bootstrap->hasPluginResource('Log')) {
    return false;
}
$log = $bootstrap->getResource('Log');
return $log;
EOS
))))))));
        }
        // store the generator into the registry so that the addAction command can use the same object later
        Php\PhpFile::registerFileCodeGenerator($codeGenFile);
        // REQUIRES filename to be set
        return $codeGenFile->generate();
    }