コード例 #1
0
ファイル: InfoController.php プロジェクト: ralfeggert/zftool
 /**
  * Show actions for a controller in a module
  */
 public function actionsAction()
 {
     // check for help mode
     if ($this->requestOptions->getFlagHelp()) {
         return $this->actionsHelp();
     }
     // output header
     $this->consoleHeader('Fetching requested information');
     // get needed options to shorten code
     $path = $this->requestOptions->getPath();
     $moduleName = $this->requestOptions->getModuleName();
     $modulePath = $this->requestOptions->getModulePath();
     $controllerKey = $this->requestOptions->getControllerKey();
     $controllerName = $this->requestOptions->getControllerName();
     $controllerPath = $this->requestOptions->getControllerPath();
     $controllerFile = $this->requestOptions->getControllerFile();
     // check for module path and application config
     if (!file_exists($path . '/module') || !file_exists($path . '/config/application.config.php')) {
         return $this->sendError(array(array(Color::NORMAL => 'The path '), array(Color::RED => $path), array(Color::NORMAL => ' doesn\'t contain a ZF2 application.')));
     }
     // check if controller name provided
     if (!$controllerName) {
         return $this->sendError(array(array(Color::NORMAL => 'Please provide the controller name as parameter.')));
     }
     // check if module name provided
     if (!$moduleName) {
         return $this->sendError(array(array(Color::NORMAL => 'Please provide the module name as parameter.')));
     }
     // check if module exists
     if (!file_exists($modulePath)) {
         return $this->sendError(array(array(Color::NORMAL => 'The module '), array(Color::RED => $moduleName), array(Color::NORMAL => ' does not exist.')));
     }
     // check if controller exists already in module
     if (!file_exists($controllerPath . $controllerFile)) {
         return $this->sendError(array(array(Color::NORMAL => 'The controller '), array(Color::RED => $controllerName), array(Color::NORMAL => ' does not exist in module '), array(Color::RED => $moduleName), array(Color::NORMAL => '.')));
     }
     // get actions
     $actions = $this->getActionsForController($controllerPath . $controllerFile, $controllerKey);
     // check actions
     if (empty($actions)) {
         return $this->sendError(array(array(Color::NORMAL => 'No actions available for controller '), array(Color::RED => $controllerName), array(Color::NORMAL => ' in module '), array(Color::RED => $moduleName), array(Color::NORMAL => '.')));
     }
     // start output
     $this->console->write(' Done ', Color::NORMAL, Color::CYAN);
     $this->console->write(' ');
     $this->console->write('Actions available in controller ');
     $this->console->write($controllerName, Color::GREEN);
     $this->console->write(' in module ');
     $this->console->write($moduleName, Color::GREEN);
     $this->console->writeLine(PHP_EOL);
     // output actions
     foreach ($actions as $actionMethod) {
         $this->console->writeLine('       => ' . $actionMethod . '()', Color::GREEN);
     }
     // output footer
     $this->consoleFooter('requested info was successfully displayed');
 }
コード例 #2
0
 /**
  * Add configuration for a new controller factory
  *
  * @return bool|mixed
  */
 public function addControllerFactoryConfig()
 {
     // check for no config flag
     if ($this->flagNoConfig) {
         return false;
     }
     // get needed options to shorten code
     $modulePath = $this->requestOptions->getModulePath();
     $controllerKey = $this->requestOptions->getControllerKey();
     // Read module configuration
     $moduleConfigOld = (require $modulePath . '/config/module.config.php');
     $moduleConfigNew = $moduleConfigOld;
     // check for controllers configuration
     if (!isset($moduleConfigNew['controllers'])) {
         $moduleConfigNew['controllers'] = array();
     }
     // check for controllers invokables configuration
     if (!isset($moduleConfigNew['controllers']['invokables'])) {
         $moduleConfigNew['controllers']['invokables'] = array();
     }
     // check for controllers invokables configuration
     if (!isset($moduleConfigNew['controllers']['factories'])) {
         $moduleConfigNew['controllers']['factories'] = array();
     }
     // check if factory key is already there
     if (!in_array($controllerKey, $moduleConfigNew['controllers']['factories'])) {
         $moduleConfigNew['controllers']['factories'][$controllerKey] = $controllerKey . 'ControllerFactory';
     }
     // check if invokable key is there
     if (isset($moduleConfigNew['controllers']['invokables']) && isset($moduleConfigNew['controllers']['invokables'][$controllerKey])) {
         unset($moduleConfigNew['controllers']['invokables'][$controllerKey]);
     }
     // set config dir
     $configDir = realpath($modulePath . '/config');
     // reset constant compilation
     $moduleConfigNew = $this->resetConfigDirCompilation($moduleConfigNew, $configDir);
     // check for module config updates
     if ($moduleConfigNew !== $moduleConfigOld) {
         return $moduleConfigNew;
     } else {
         return false;
     }
 }
コード例 #3
0
ファイル: ModuleGenerator.php プロジェクト: ralfeggert/zftool
 /**
  * Update controller class with new action
  *
  * @return bool
  * @throws \Zend\Code\Generator\Exception
  */
 public function updateController()
 {
     // get needed options to shorten code
     $moduleName = $this->requestOptions->getModuleName();
     $controllerKey = $this->requestOptions->getControllerKey();
     $controllerPath = $this->requestOptions->getControllerPath();
     $controllerFile = $this->requestOptions->getControllerFile();
     $actionMethod = $this->requestOptions->getActionMethod();
     $controllerFilePath = $controllerPath . $controllerFile;
     // get file and class reflection
     $fileReflection = new FileReflection($controllerFilePath, true);
     $classReflection = $fileReflection->getClass($controllerKey . 'Controller');
     // setup class generator with reflected class
     $code = ClassGenerator::fromReflection($classReflection);
     // check for action method
     if ($code->hasMethod($actionMethod)) {
         throw new GeneratorException('New action already exists within controller');
     }
     // fix namespace usage
     $code->addUse('Zend\\Mvc\\Controller\\AbstractActionController');
     $code->addUse('Zend\\View\\Model\\ViewModel');
     $code->setExtendedClass('AbstractActionController');
     $code->addMethodFromGenerator($this->generateActionMethod($actionMethod));
     // create file with file generator
     $file = new FileGenerator();
     $file->setClass($code);
     // add optional doc block
     if ($this->flagCreateApiDocs) {
         $file->setDocBlock(new DocBlockGenerator('This file was generated by FrilleZFTool.', null, array($this->generatePackageTag($moduleName), $this->generateSeeTag())));
     }
     // write controller class
     if (!file_put_contents($controllerFilePath, $file->generate())) {
         return false;
     }
     return true;
 }