/** * Scaffold Construct * @param $options * @throws \Exception */ public function __construct($options) { if (empty($options['tableName'])) { throw new \Exception("Please, specify the table name"); } if (empty($options['name'])) { $options['name'] = Text::camelize($options['tableName']); } if (empty($options['controllersDir'])) { $options['controllersDir'] = Tools::getModulesPath() . $options['module'] . DIRECTORY_SEPARATOR . Tools::getControllersDir() . DIRECTORY_SEPARATOR; } else { $options['controllersDir'] .= DIRECTORY_SEPARATOR; } if (empty($options['controllersNamespace']) || $options['namespace'] != 'None') { if (empty($options['module'])) { $options['controllersNamespace'] = Tools::getBaseNamespace() . Tools::getControllersDir(); } else { $options['controllersNamespace'] = Tools::getBaseNamespace() . $options['module'] . '\\' . Tools::getControllersDir(); } } if (empty($options['modelsDir'])) { $options['modelsDir'] = Tools::getModulesPath() . $options['module'] . DIRECTORY_SEPARATOR . Tools::getModelsDir() . DIRECTORY_SEPARATOR; } else { $options['modelsDir'] .= DIRECTORY_SEPARATOR; } if (empty($options['modelsNamespace']) || $options['namespace'] != 'None') { if (empty($options['module'])) { $options['modelsNamespace'] = Tools::getBaseNamespace() . Tools::getModelsDir(); } else { $options['modelsNamespace'] = Tools::getBaseNamespace() . $options['module'] . '\\' . Tools::getModelsDir(); } } if (empty($options['viewsDir'])) { $options['viewsDir'] = Tools::getModulesPath() . $options['module'] . DIRECTORY_SEPARATOR . Tools::getViewsDir() . DIRECTORY_SEPARATOR; } else { $options['viewsDir'] .= DIRECTORY_SEPARATOR; } if (empty($options['baseClass'])) { $options['baseClass'] = 'Phalcon\\Mvc\\Model'; } if (!isset($options['force'])) { $options['force'] = false; } $this->_options = $options; }
/** * View Construct * @param $options * @throws \Exception */ public function __construct($options) { if (empty($options['name'])) { throw new \Exception("Please specify the view name"); } if (!isset($options['force'])) { $options['force'] = false; } if (!isset($options['directory'])) { $options['directory'] = Tools::getModulesPath() . $options['module'] . DIRECTORY_SEPARATOR . Tools::getViewsDir() . DIRECTORY_SEPARATOR; } else { $options['directory'] .= DIRECTORY_SEPARATOR; } if (!isset($options['action'])) { $options['action'] = 'index'; } $this->_options = $options; }
/** * 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"); } }