Beispiel #1
0
 /**
  * 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;
     });
 }
Beispiel #2
0
 /**
  * 注册服务
  * @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;
         });
     }
 }
Beispiel #3
0
 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;
     });
 }
Beispiel #4
0
<?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;
    }
}