/** * Add configuration for a new module * * @return bool|mixed */ public function addModuleConfig() { // get needed options to shorten code $path = $this->requestOptions->getPath(); $moduleName = $this->requestOptions->getModuleName(); // set file name $configFile = $path . '/config/application.config.php'; // read application configuration $applicationConfigOld = (require $path . '/config/application.config.php'); $applicationConfigNew = $applicationConfigOld; // Add the module in application.config.php if (!in_array($moduleName, $applicationConfigNew['modules'])) { $applicationConfigNew['modules'][] = $moduleName; } // set config dir $configDir = realpath($path . '/config'); // reset constant compilation $applicationConfigNew = $this->resetConfigDirCompilation($applicationConfigNew, $configDir); // check for application config updates if ($applicationConfigNew !== $applicationConfigOld) { return $applicationConfigNew; } else { return false; } }
/** * Get path to ZF2 * * @return bool|string */ protected function getZF2Path() { // check for ZF2 path if (getenv('ZF2_PATH')) { return getenv('ZF2_PATH'); } elseif (get_cfg_var('zf2_path')) { return get_cfg_var('zf2_path'); } elseif (is_dir('vendor/ZF2/library')) { return 'vendor/ZF2/library'; } elseif (is_dir('vendor/zendframework/zendframework/library')) { return 'vendor/zendframework/zendframework/library'; } elseif (is_dir('vendor/zendframework/zend-version')) { return 'vendor/zendframework/zend-version'; } elseif (is_dir($this->requestOptions->getPath() . '/vendor/zendframework/zendframework/library')) { return $this->requestOptions->getPath() . '/vendor/zendframework/zendframework/library'; } return false; }
/** * Update module class with class map autoloading * * @return bool * @throws \Zend\Code\Generator\Exception */ public function updateModuleWithClassmapAutoloader() { // get needed options to shorten code $path = realpath($this->requestOptions->getPath()); $directory = $this->requestOptions->getDirectory(); $destination = $this->requestOptions->getDestination(); $moduleFile = $directory . '/Module.php'; $moduleClass = str_replace($path . '/module/', '', $directory) . '\\Module'; $moduleName = str_replace($path . '/module/', '', $directory); // check for module file if (!file_exists($moduleFile)) { return false; } // get file and class reflection $fileReflection = new FileReflection($moduleFile, true); $classReflection = $fileReflection->getClass($moduleClass); // setup class generator with reflected class $code = ClassGenerator::fromReflection($classReflection); // check for action method if ($code->hasMethod('getAutoloaderConfig')) { $code->removeMethod('getAutoloaderConfig'); } // add getAutoloaderConfig method with class map $code->addMethodFromGenerator($this->generateGetAutoloaderConfigMethod($destination, $moduleName)); // 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()))); } // 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 module class if (!file_put_contents($moduleFile, $file->generate())) { return false; } return true; }
/** * Set configuration by key */ public function setAction() { // check for help mode if ($this->requestOptions->getFlagHelp()) { return $this->setHelp(); } // output header $this->consoleHeader('Setting requested configuration'); // get needed options to shorten code $path = realpath($this->requestOptions->getPath()); $configName = $this->requestOptions->getConfigName(); $configValue = $this->requestOptions->getConfigValue(); $configFile = $path . '/config/autoload/local.php'; // check for config name if (!$configName) { return $this->sendError('config get <configName> was not provided'); } // start output $this->console->write(' => Reading configuration file '); $this->console->writeLine($configFile, Color::GREEN); $this->console->write(' => Changing configuration data for key '); $this->console->write($configName, Color::GREEN); $this->console->write(' to value '); $this->console->writeLine($configValue, Color::GREEN); $this->console->write(' => Writing configuration file '); $this->console->writeLine($configFile, Color::GREEN); $this->console->writeLine(); // check for value if ($configValue === 'null') { $configValue = null; } // write local config file $configData = new ModuleConfig($configFile); $configData->write($configName, $configValue); // continue output $this->console->write(' Done ', Color::NORMAL, Color::CYAN); $this->console->write(' '); $this->console->writeLine('Configuration data was changed.'); // output footer $this->consoleFooter('requested configuration was successfully changed'); }
/** * @return mixed */ public function zfAction() { // check for help mode if ($this->requestOptions->getFlagHelp()) { return $this->zfHelp(); } // output header $this->consoleHeader('Installing Zend Framework 2 library'); // check for zip extension if (!extension_loaded('zip')) { return $this->sendError(array(array(Color::NORMAL => 'You need to install the ZIP extension of PHP.'))); } // get needed options to shorten code $path = $this->requestOptions->getPath(); $tmpDir = $this->requestOptions->getTmpDir(); $version = $this->requestOptions->getVersion(); // check if path provided if ($path == '.') { return $this->sendError(array(array(Color::NORMAL => 'Please provide the path to install the ZF2 library in.'))); } // check if path exists if (file_exists($path)) { return $this->sendError(array(array(Color::NORMAL => 'The directory '), array(Color::RED => $path), array(Color::NORMAL => ' already exists. '), array(Color::NORMAL => 'You cannot install the ZF2 library here.'))); } // check version if (empty($version)) { $version = Zf::getLastVersion(); if (false === $version) { return $this->sendError(array(array(Color::NORMAL => 'I cannot connect to the Zend Framework website.'))); } } else { if (!Zf::checkVersion($version)) { return $this->sendError(array(array(Color::NORMAL => 'The specified ZF version, '), array(Color::RED => $version), array(Color::NORMAL => ' does not exist.'))); } } // get tmp file and check it $tmpFile = ZF::getTmpFileName($tmpDir, $version); if (!file_exists($tmpFile)) { if (!Zf::downloadZip($tmpFile, $version)) { return $this->sendError(array(array(Color::NORMAL => 'I cannot download the ZF2 library from GitHub.'))); } } // unzip archive $zip = new \ZipArchive(); if ($zip->open($tmpFile)) { $zipFolders = $zip->statIndex(0); $zipFolder = $tmpDir . '/' . rtrim($zipFolders['name'], "/"); if (!$zip->extractTo($tmpDir)) { return $this->sendError(array(array(Color::NORMAL => 'Error during the unzip of '), array(Color::RED => $tmpFile))); } $result = Utility::copyFiles($zipFolder, $path); if (file_exists($zipFolder)) { Utility::deleteFolder($zipFolder); } $zip->close(); if (false === $result) { return $this->sendError(array(array(Color::NORMAL => 'Error during the copy of the files in '), array(Color::RED => $path))); } } $this->console->write(' Done ', Color::NORMAL, Color::CYAN); $this->console->write(' '); $this->console->write('The ZF library '); $this->console->write($version, Color::GREEN); $this->console->write(' has been installed in '); $this->console->writeLine(realpath($path), Color::GREEN); // output footer $this->consoleFooter('library was successfully installed'); }
/** * Create a view helper * * @return ConsoleModel */ public function viewHelperAction() { // check for help mode if ($this->requestOptions->getFlagHelp()) { return $this->viewHelperHelp(); } // output header $this->consoleHeader('Creating new view helper'); // get needed options to shorten code $path = $this->requestOptions->getPath(); $flagWithFactory = $this->requestOptions->getFlagWithFactory(); $moduleName = $this->requestOptions->getModuleName(); $modulePath = $this->requestOptions->getModulePath(); $viewHelperName = $this->requestOptions->getViewHelperName(); $viewHelperPath = $this->requestOptions->getViewHelperPath(); $viewHelperClass = $this->requestOptions->getViewHelperClass(); $viewHelperFile = $this->requestOptions->getViewHelperFile(); // 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 helper name provided if (!$viewHelperName) { return $this->sendError(array(array(Color::NORMAL => 'Please provide the view helper name as parameter.'))); } // check if module name provided if (!$moduleName) { return $this->sendError(array(array(Color::NORMAL => 'Please provide the module name as parameter.'))); } // set mode if (file_exists($viewHelperPath . $viewHelperFile)) { $mode = 'update'; } else { $mode = 'insert'; } // check if view helper exists already in module if ($mode == 'update' && !$flagWithFactory) { return $this->sendError(array(array(Color::NORMAL => 'The view helper '), array(Color::RED => $viewHelperName), array(Color::NORMAL => ' already exists in module '), array(Color::RED => $moduleName), array(Color::NORMAL => '.'))); } // create view helper if ($mode == 'insert') { // write start message $this->console->write(' => Creating view helper '); $this->console->write($viewHelperName, Color::GREEN); $this->console->write(' in module '); $this->console->writeLine($moduleName, Color::GREEN); // create view helper class $viewHelperFlag = $this->moduleGenerator->createViewHelper(); // write start message $this->console->write(' => Adding view helper configuration for '); $this->console->writeLine($moduleName, Color::GREEN); // add view helper configuration to module $moduleConfig = $this->moduleConfigurator->addViewHelperConfig(); } // check for factory flag if ($flagWithFactory) { // create view helper factory class try { $factoryFlag = $this->moduleGenerator->createViewHelperFactory(); } catch (GeneratorException $e) { return $this->sendError(array(array(Color::NORMAL => 'The factory for the view helper '), array(Color::RED => $viewHelperName), array(Color::NORMAL => ' of module '), array(Color::RED => $moduleName), array(Color::NORMAL => ' exists already.'))); } // write start message $this->console->write(' => Creating factory for view helper '); $this->console->write($viewHelperName, Color::GREEN); $this->console->write(' in module '); $this->console->writeLine($moduleName, Color::GREEN); // add view helper factory configuration to module $moduleConfig = $this->moduleConfigurator->addViewHelperFactoryConfig(); } else { $factoryFlag = false; } // check for module config updates if ($moduleConfig) { // update module configuration $this->moduleGenerator->updateConfiguration($moduleConfig, $modulePath . '/config/module.config.php'); // write start message $this->console->write(' => Updating configuration for module '); $this->console->writeLine($moduleName, Color::GREEN); } $this->console->writeLine(); $this->console->write(' Done ', Color::NORMAL, Color::CYAN); $this->console->write(' '); // write message if ($factoryFlag) { $this->console->write('The view helper '); $this->console->write($viewHelperName, Color::GREEN); $this->console->write(' has been created with a factory in module '); $this->console->writeLine($moduleName, Color::GREEN); } else { $this->console->write('The view helper '); $this->console->write($viewHelperName, Color::GREEN); $this->console->write(' has been created in module '); $this->console->writeLine($moduleName, Color::GREEN); } $this->console->writeLine(); $this->console->writeLine(' => In order to use the view helper add the following code to any view script.'); $this->console->writeLine(' <?php echo $this->' . lcfirst($viewHelperName) . '(); ?>', Color::CYAN); // output footer $this->consoleFooter('view helper was successfully created'); }