コード例 #1
0
ファイル: ClassGeneratorTest.php プロジェクト: rikaix/zf2
 /**
  * @group ZF-7361
  */
 public function testHasMethod()
 {
     $method = new MethodGenerator();
     $method->setName('methodOne');
     $classGenerator = new ClassGenerator();
     $classGenerator->setMethod($method);
     $this->assertTrue($classGenerator->hasMethod('methodOne'));
 }
コード例 #2
0
ファイル: ControllerFile.php プロジェクト: rafalwrzeszcz/zf2
    /**
     * getContents()
     *
     * @return string
     */
    public function getContents()
    {
        $className = $this->_moduleName ? ucfirst($this->_moduleName) . '\\' : '';
        $className .= ucfirst($this->_controllerName) . 'Controller';
        $codeGenFile = new FileGenerator();
        $codeGenFile->setFilename($this->getPath());
        $cg = new ClassGenerator($className);
        $cg->setMethod(new MethodGenerator('init', array(), null, '/* Initialize action controller here */'));
        $codeGenFile->setClass($cg);
        if ($className == 'ErrorController') {
            $codeGenFile = new FileGenerator();
            $codeGenFile->setFilename($this->getPath());
            $cg = new ClassGenerator($className);
            $cg->setMethods(array(new MethodGenerator('errorAction', array(), null, <<<'EOS'
$errors = $this->_getParam('error_handler');

if (!$errors || !$errors instanceof \ArrayObject) {
    $this->view->vars()->message = 'You have reached the error page';
    return;
}

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);
        $priority = \Zend\Log\Logger::NOTICE;
        $this->view->vars()->message = 'Page not found';
        break;
    default:
        // application error
        $this->getResponse()->setHttpResponseCode(500);
        $priority = \Zend\Log\Logger::CRIT;
        $this->view->vars()->message = 'Application error';
        break;
}

// Log exception, if logger available
if (($log = $this->getLog())) {
    $log->log($this->view->vars()->message, $priority, $errors->exception);
    $log->log('Request Parameters', $priority, $errors->request->getParams());
}

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

$this->view->vars()->request = $errors->request;
EOS
), new MethodGenerator('getLog', array(), null, <<<'EOS'
/* @var $bootstrap Zend\Application\Bootstrap */
$bootstrap = $this->getInvokeArg('bootstrap');
if (!$bootstrap->getBroker()->hasPlugin('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
        FileGeneratorRegistry::registerFileCodeGenerator($codeGenFile);
        // REQUIRES filename to be set
        return $codeGenFile->generate();
    }
コード例 #3
0
ファイル: Generator.php プロジェクト: ncud/sagalaya
 public function buildController($model)
 {
     $class = new ClassGenerator($model->config->name . 'sController');
     $class->setExtendedClass('\\lithium\\action\\Controller');
     $class->setNamespaceName('app\\controllers');
     $publicActions = new PropertyGenerator('publicActions', array(), PropertyGenerator::FLAG_PUBLIC);
     $class->setProperty($publicActions);
     $messageMethod = new MethodGenerator('message');
     $messageMethod->setParameter('value');
     $messageMethod->setBody("\\lithium\\storage\\Session::write('message', \$value);");
     $class->setMethod($messageMethod);
     foreach (array('index', 'create', 'edit', 'view', 'delete') as $action) {
         $method = new MethodGenerator($action);
         $single = strtolower("{$model->config->name}");
         $plurals = $single . "s";
         switch ($action) {
             case 'index':
                 $body = "\${$plurals} = {$model->config->name}::findAll();\n\n";
                 $body .= "return compact('{$plurals}');";
                 $method->setBody($body);
                 break;
             case 'create':
                 $body = "if (\$this->request->data) {\n\n";
                 $body .= "\t\${$single} = new {$model->config->name}(\$this->request->data);\n\n";
                 $body .= "\tif(\${$single}->save()) {\n";
                 $body .= "\t\t\$this->message('Successfully to create {$model->config->name}');\n";
                 $body .= "\t\t\$this->redirect('{$model->config->name}s::index');\n";
                 $body .= "\t} else {\n";
                 $body .= "\t\t\$this->message('Failed to create {$model->config->name}, please check the error');\n";
                 $body .= "\t\t\$errors = \${$single}->getErrors();\n";
                 $body .= "\t}\n\n";
                 $body .= "}\n\n";
                 $body .= "return compact('{$single}', 'errors');";
                 $method->setBody($body);
                 break;
             case 'edit':
                 $body = "if (\$this->request->id) {\n\n";
                 $body .= "\t\${$single} = {$model->config->name}::get(\$this->request->id);\n";
                 $body .= "\t\${$single}->properties = \$this->request->data;\n\n";
                 $body .= "\tif(\${$single}->save()) {\n";
                 $body .= "\t\t\$this->message('Successfully to update {$model->config->name}');\n";
                 $body .= "\t\t\$this->redirect('{$model->config->name}s::index');\n";
                 $body .= "\t} else {\n";
                 $body .= "\t\t\$this->message('Failed to update {$model->config->name}, please check the error');\n";
                 $body .= "\t\t\$errors = \${$single}->getErrors();\n";
                 $body .= "\t}\n\n";
                 $body .= "}\n\n";
                 $body .= "return compact('{$single}', 'errors');";
                 $method->setBody($body);
                 break;
             case 'view':
                 $body = "if (\$this->request->id) {\n";
                 $body .= "\t\${$single} = {$model->config->name}::get(\$this->request->id);\n";
                 $body .= "}\n\n";
                 $body .= "return compact('{$single}');";
                 $method->setBody($body);
                 break;
             case 'delete':
                 $body = "if (\$this->request->id) {\n";
                 $body .= "\t\${$single} = {$model->config->name}::get(\$this->request->id);\n";
                 $body .= "\t\${$single}->delete();\n";
                 $body .= "\t\$this->message('Success to delete {$model->config->name}');\n";
                 $body .= "\t\$this->redirect('{$model->config->name}s::index');\n";
                 $body .= "\treturn true;\n";
                 $body .= "}\n\n";
                 $body .= "\$this->message('{$model->config->name} id cannot be empty');\n";
                 $body .= "\$this->redirect(\$this->request->referer());\n";
                 $body .= "return false;";
                 $method->setBody($body);
                 break;
         }
         $class->setMethod($method);
     }
     return $class;
 }