/** * Register specific services for the module * * @package base-app * @version 2.0 * * @param object $di dependency Injector * * @return void */ public function registerServices(\Phalcon\DiInterface $di) { //Registering a dispatcher $di->set('dispatcher', function () { //Create/Get an EventManager $eventsManager = new \Phalcon\Events\Manager(); //Attach a listener $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) { //controller or action doesn't exist if ($event->getType() == 'beforeException') { switch ($exception->getCode()) { case \Phalcon\Cli\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('task' => 'main', 'action' => 'notFound')); return false; } } }); //$dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher = new \Phalcon\Cli\Dispatcher(); //Set default namespace to frontend module $dispatcher->setDefaultNamespace("Baseapp\\Cli\\Tasks"); //Bind the EventsManager to the dispatcher $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); //Registering the view component $di->set('view', function () use($di) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(ROOT_PATH . '/app/frontend/views/'); $view->registerEngines(\Baseapp\Library\Tool::registerEngines($view, $di)); return $view; }); }
/** * 注册服务 * @param type $di */ public function registerServices(\Phalcon\DiInterface $di = NULL) { $config = \TConfig::instance()->getModule($this->moduleId); if (php_sapi_name() == 'cli') { //处理CLI模式 $di['dispatcher'] = function () use($config) { $dispatcher = new \Phalcon\Cli\Dispatcher(); if (isset($config['defaultNamespace']) && isset($config['defaultNamespace']['task'])) { $dispatcher->setDefaultNamespace($config['defaultNamespace']); } return $dispatcher; }; } else { //处理WEB模式 $di['dispatcher'] = function () use($config) { $dispatcher = new \Phalcon\Mvc\Dispatcher(); if (isset($config['defaultNamespace']) && isset($config['defaultNamespace']['controller'])) { $dispatcher->setDefaultNamespace($config['defaultNamespace']['controller']); } return $dispatcher; }; //添加视图 $di->set('view', function () use($config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir($config['autoloadDir']['view']); $view->registerEngines(array('.html' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }); } }
private static function cliRegister() { $di = get_app_di(); $config = self::getConfig(); self::registCommonService($di, $config); $di->setShared('dispatcher', function () { $dispatcher = new \Phalcon\Cli\Dispatcher(); $dispatcher->setDefaultNamespace("App\\Tasks\\"); return $dispatcher; }); }
protected function registerServices() { $di = $this->_di; $arguments = $this->_arguments; $this->_di->set('config', function () { return new Phalcon\Config\Adapter\Ini(APP_PATH . 'config/main.ini'); }, true); $this->_di->set('dispatcher', function () use($arguments) { $dispatcher = new Phalcon\Cli\Dispatcher(); $dispatcher->setDefaultAction('main'); $dispatcher->setTaskSuffix($arguments['task_suffix']); $dispatcher->setTaskName($arguments['namespace'] . '\\' . $arguments['task']); $dispatcher->setActionName($arguments['action']); $dispatcher->setParams($arguments['params']); return $dispatcher; }, true); $this->_di->set('db', function () use($di) { $config = $di->getShared('config'); return new Phalcon\Db\Adapter\Pdo\Mysql(['host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->name, 'port' => $config->database->port, "options" => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')]); }, true); $this->_di->set('modelsMetadata', function () { return new Phalcon\Mvc\Model\MetaData\Memory(); }, true); $this->_di->set('modelsManager', function () { return new Phalcon\Mvc\Model\Manager(); }, true); }
<?php /** * Register the autoloader and tell it to register the src/ directory */ $loader = new \Phalcon\Loader(); $loader->registerNamespaces(["PhalconDocs" => __DIR__ . "/scripts/src/"]); $loader->register(); // Using the CLI factory default services container $di = new \Phalcon\Di\FactoryDefault\Cli(); $di->setShared("dispatcher", function () { $dispatcher = new \Phalcon\Cli\Dispatcher(); $dispatcher->setDefaultNamespace("PhalconDocs\\Task"); return $dispatcher; }); // Create a console application $console = new \Phalcon\Cli\Console(); $console->setDI($di); /** * Process the console arguments */ $arguments = []; foreach ($argv as $k => $arg) { if ($k == 1) { $arguments["task"] = $arg; } elseif ($k == 2) { $arguments["action"] = $arg; } elseif ($k >= 3) { $arguments["params"][] = $arg; } }