/**
  * AllModels Construct
  *
  * @param $options
  * @throws \Exception
  */
 public function __construct($options)
 {
     if (empty($options['name'])) {
         $options['name'] = Text::camelize($options['tableName']);
     }
     if (empty($options['directory'])) {
         $options['directory'] = Tools::getModulesPath() . $options['module'] . DIRECTORY_SEPARATOR . Tools::getModelsDir();
     }
     if (empty($options['namespace']) || $options['namespace'] != 'None') {
         if (empty($options['module'])) {
             $options['namespace'] = Tools::getBaseModule() . Tools::getModelsDir();
         } else {
             $options['namespace'] = Tools::getBaseModule() . $options['module'] . '\\' . Tools::getModelsDir();
         }
     }
     if (empty($options['baseClass'])) {
         $options['baseClass'] = 'Phalcon\\Mvc\\Model';
     }
     if (empty($options['tableName'])) {
         throw new \Exception("Please, specify the table name");
     }
     if (!isset($options['force'])) {
         $options['force'] = false;
     }
     $this->_options = $options;
 }
 /**
  * Controller constructor
  *
  * @param array $options
  * @throws \Exception
  */
 public function __construct($options)
 {
     if (empty($options['name'])) {
         throw new \Exception("Please specify the controller name");
     }
     if (empty($options['force'])) {
         $options['force'] = false;
     }
     if (empty($options['directory'])) {
         $options['directory'] = Tools::getModulesPath() . $options['module'] . DIRECTORY_SEPARATOR . Tools::getControllersDir() . DIRECTORY_SEPARATOR;
     } else {
         $options['directory'] .= DIRECTORY_SEPARATOR;
     }
     if (empty($options['namespace']) || $options['namespace'] != 'None') {
         if (empty($options['module'])) {
             $options['namespace'] = Tools::getBaseModule() . Tools::getControllersDir();
         } else {
             $options['namespace'] = Tools::getBaseModule() . $options['module'] . '\\' . Tools::getControllersDir();
         }
     }
     if (empty($options['baseClass'])) {
         $options['baseClass'] = 'Phalcon\\Mvc\\Controller';
     }
     $this->_options = $options;
 }
Beispiel #3
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");
        }
    }