Esempio n. 1
0
    /**
     * Generate module file
     * @throws \Exception
     */
    private function _createModule()
    {
        $code = "<?php\n" . Tools::getCopyright() . PHP_EOL . PHP_EOL . 'namespace ' . $this->_options['namespace'] . ';' . PHP_EOL . PHP_EOL;
        if (Tools::fullVersion()) {
            $code .= 'use Phalcon\\DiInterface;' . PHP_EOL . 'use Phalcon\\Loader;' . PHP_EOL . 'use Phalcon\\Mvc\\View;' . PHP_EOL . 'use Phalcon\\Mvc\\Dispatcher;' . PHP_EOL . 'use Phalcon\\Mvc\\ModuleDefinitionInterface;';
        }
        $baseModule = Tools::getBaseModule();
        if (!empty($baseModule)) {
            $base = explode('\\', Tools::getBaseModule());
            $baseClass = end($base);
            $useClass = PHP_EOL . 'use ' . Tools::getBaseModule() . ';';
            $code .= $useClass;
        }
        $code .= PHP_EOL . PHP_EOL . '/**
 * Class Module
 * @package ' . $this->_options['namespace'] . '
 */
class Module';
        if (!empty($baseModule)) {
            $code .= " extends {$baseClass}";
        }
        if (Tools::fullVersion()) {
            $code .= " implements ModuleDefinitionInterface\n{\n\t/**\n     * Register a specific autoloader for the module\n     * @param \\Phalcon\\DiInterface \$di\n     */\n    public function registerAutoloaders(DiInterface \$di = null)\n    {\n        \$loader = new Loader();\n        \$loader->registerNamespaces(\n            array(" . PHP_EOL . "\t\t\t\t'" . $this->_options['namespace'] . '\\' . Tools::getControllersDir() . "' => __DIR__ . '/" . Tools::getControllersDir() . "'," . PHP_EOL . "\t\t\t\t'" . $this->_options['namespace'] . '\\' . Tools::getModelsDir() . "' => __DIR__ . '/" . Tools::getModelsDir() . "'" . PHP_EOL . "\t\t\t)\n        );\n        \$loader->register();\n    }\n\n    /**\n     * Register specific services for the module\n     * @param \\Phalcon\\DiInterface \$di\n     */\n    public function registerServices(DiInterface \$di)\n    {\n        //Registering a dispatcher\n        \$di->set('dispatcher', function() {\n            \$dispatcher = new Dispatcher();\n            \$dispatcher->setDefaultNamespace('" . $this->_options['namespace'] . "');\n            return \$dispatcher;\n        });\n\n        //Registering the view component\n        \$di->set('view', function() {\n            \$view = new View();\n            \$view->setViewsDir(__DIR__ . '/" . Tools::getViewsDir() . "');\n            return \$view;\n        });\n    }\n}";
        } else {
            $code .= " {" . PHP_EOL . PHP_EOL . "}";
        }
        $code = str_replace("\t", "    ", $code);
        $modulePath = $this->_options['directory'] . DIRECTORY_SEPARATOR . 'Module.php';
        if (!file_exists($modulePath) || $this->_options['force'] == true) {
            if (!@file_put_contents($modulePath, $code)) {
                throw new \Exception("Unable to write to '{$modulePath}'");
            }
            @chmod($modulePath, 0777);
        } else {
            throw new \Exception("Module.php file already exists");
        }
    }