/** * Generate classmap * * @return ConsoleModel */ public function classmapAction() { // check for help mode if ($this->requestOptions->getFlagHelp()) { return $this->classmapHelp(); } // output header $this->consoleHeader('Generating classmap'); // get needed options to shorten code $directory = $this->requestOptions->getDirectory(); $destination = $this->requestOptions->getDestination(); $relativePath = ''; $usingStdout = false; // check if directory provided if (is_null($directory)) { return $this->sendError(array(array(Color::NORMAL => 'Please provide the directory to create the classmap in'))); } // Validate directory if (!is_dir($directory)) { return $this->sendError(array(array(Color::NORMAL => 'Invalid library directory provided '), array(Color::RED => $directory), array(Color::NORMAL => '.'))); } // check that destination file is not a directory if (is_dir($destination)) { return $this->sendError(array(array(Color::RED => $destination), array(Color::NORMAL => ' is not a valid output file.'))); } // check that destination file is writable if (!is_writeable(dirname($destination))) { return $this->sendError(array(array(Color::NORMAL => 'Cannot write to '), array(Color::RED => $destination), array(Color::NORMAL => '.'))); } // Determine output if ('-' == $destination) { $destination = STDOUT; $usingStdout = true; } else { // We need to add the $libraryPath into the relative path that is created in the classmap file. $classmapPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname($destination))); // Simple case: $libraryPathCompare is in $classmapPathCompare if (strpos($directory, $classmapPath) === 0) { if ($directory !== $classmapPath) { // prevent double dash in filepaths when using "." as directory $relativePath = substr($directory, strlen($classmapPath) + 1) . '/'; } } else { $libraryPathParts = explode('/', $directory); $classmapPathParts = explode('/', $classmapPath); // Find the common part $count = count($classmapPathParts); for ($i = 0; $i < $count; $i++) { if (!isset($libraryPathParts[$i]) || $libraryPathParts[$i] != $classmapPathParts[$i]) { // Common part end break; } } // Add parent dirs for the subdirs of classmap $relativePath = str_repeat('../', $count - $i); // Add library subdirs $count = count($libraryPathParts); for (; $i < $count; $i++) { $relativePath .= $libraryPathParts[$i] . '/'; } } } // start output if (!$usingStdout) { $this->console->writeLine(' => Scanning for files containing PHP classes '); } // generate new classmap $classMap = $this->moduleConfigurator->buildClassmapConfig($relativePath); // Check if we have found any PHP classes. if (!$classMap) { return $this->sendError(array(array(Color::NORMAL => 'Cannot find any PHP classes in '), array(Color::RED => $directory), array(Color::NORMAL => '.'))); } // continue output if (!$usingStdout) { $this->console->write(' => Found '); $this->console->write(count($classMap), Color::GREEN); $this->console->writeLine(' PHP classes'); $this->console->writeLine(' => Writing classmap'); } // update module configuration $this->moduleGenerator->updateConfiguration($classMap, $destination, true); // update module class with classmap autoloading $updateFlag = $this->moduleGenerator->updateModuleWithClassmapAutoloader(); // continue output if (!$usingStdout) { if ($updateFlag) { $this->console->writeLine(' => Update module class to use classmap for autoloading'); } else { $this->console->writeLine(); $this->console->write(' Warn ', Color::NORMAL, Color::RED); $this->console->write(' '); $this->console->writeLine('=> Could not find a module class in directory to update the autoloading for the new classmap', Color::RED); } } // end output if (!$usingStdout) { $this->console->writeLine(); $this->console->write(' Done ', Color::NORMAL, Color::CYAN); $this->console->write(' '); $this->console->write('Wrote classmap to '); $this->console->writeLine($destination, Color::GREEN); } // output footer $this->consoleFooter('classmap was successfully generated'); }
/** * 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'); }