Beispiel #1
0
    public function moduleAction()
    {
        $console = $this->getServiceLocator()->get('console');
        $tmpDir = sys_get_temp_dir();
        $request = $this->getRequest();
        $name = $request->getParam('name');
        $path = rtrim($request->getParam('path'), '/');
        if (empty($path)) {
            $path = '.';
        }
        if (!file_exists("{$path}/module") || !file_exists("{$path}/config/application.config.php")) {
            return $this->sendError("The path {$path} doesn't contain a ZF2 application. I cannot create a module here.");
        }
        if (file_exists("{$path}/module/{$name}")) {
            return $this->sendError("The module {$name} already exists.");
        }
        $filter = new CamelCaseToDashFilter();
        $viewfolder = strtolower($filter->filter($name));
        $name = ucfirst($name);
        mkdir("{$path}/module/{$name}/config", 0777, true);
        mkdir("{$path}/module/{$name}/src/{$name}/Controller", 0777, true);
        mkdir("{$path}/module/{$name}/view/{$viewfolder}", 0777, true);
        // Create the Module.php
        file_put_contents("{$path}/module/{$name}/Module.php", Skeleton::getModule($name));
        // Create the module.config.php
        file_put_contents("{$path}/module/{$name}/config/module.config.php", Skeleton::getModuleConfig($name));
        // Add the module in application.config.php
        $application = (require "{$path}/config/application.config.php");
        if (!in_array($name, $application['modules'])) {
            $application['modules'][] = $name;
            copy("{$path}/config/application.config.php", "{$path}/config/application.config.old");
            $content = <<<EOD
<?php
/**
 * Configuration file generated by ZFTool
 * The previous configuration file is stored in application.config.old
 *
 * @see https://github.com/zendframework/ZFTool
 */

EOD;
            $content .= 'return ' . Skeleton::exportConfig($application) . ";\n";
            file_put_contents("{$path}/config/application.config.php", $content);
        }
        if ($path === '.') {
            $console->writeLine("The module {$name} has been created", Color::GREEN);
        } else {
            $console->writeLine("The module {$name} has been created in {$path}", Color::GREEN);
        }
    }