/** * * Register the services here to make them module-specific * */ public function registerServices($di) { // get bootstrap obj $bootstrap = $di->get('bootstrap'); // get config class name $confClass = $bootstrap->getConfClass(); // module config $mConfPath = __DIR__ . '/confs/' . PHALCON_ENV . '.' . PHALCON_CONF_TYPE; if (!is_file($mConfPath)) { throw new \Phalcon\Config\Exception("Module config file not exist, file position: {$mConfPath}"); } if (PHALCON_CONF_TYPE == 'ini') { $mConfig = new $confClass($mConfPath); } else { if (PHALCON_CONF_TYPE == 'php') { $mConfig = new $confClass(require_once $mConfPath); } } // global config $gConfig = $di->get('config'); // merge module config and global config, module's will override global's $gConfig->merge($mConfig); // set config back $di->set('config', $gConfig); // Registering a dispatcher $di->set('dispatcher', function () { $dispatcher = new \Phalcon\CLI\Dispatcher(); $dispatcher->setDefaultNamespace("BullSoft\\Sample\\Tasks\\"); return $dispatcher; }); }
public function testDispatcher() { $di = new \Phalcon\DI\FactoryDefault\CLI(); $di->set('data', function () { return "data"; }); $dispatcher = new \Phalcon\CLI\Dispatcher(); $dispatcher->setDI($di); $dispatcher->dispatch(); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getActionName(), 'main'); $this->assertEquals($dispatcher->getParams(), array()); $this->assertEquals($dispatcher->getReturnedValue(), 'mainAction'); $dispatcher->setTaskName('echo'); $dispatcher->dispatch(); $this->assertEquals($dispatcher->getTaskName(), 'echo'); $this->assertEquals($dispatcher->getActionName(), 'main'); $this->assertEquals($dispatcher->getParams(), array()); $this->assertEquals($dispatcher->getReturnedValue(), 'echoMainAction'); $dispatcher->setTaskName('main'); $dispatcher->setActionName('hello'); $dispatcher->dispatch(); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getActionName(), 'hello'); $this->assertEquals($dispatcher->getParams(), array()); $this->assertEquals($dispatcher->getReturnedValue(), 'Hello !'); $dispatcher->setActionName('hello'); $dispatcher->setParams(array('World', '######')); $dispatcher->dispatch(); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getActionName(), 'hello'); $this->assertEquals($dispatcher->getParams(), array('World', '######')); $this->assertEquals($dispatcher->getReturnedValue(), 'Hello World######'); $dispatcher->setActionName('hello'); $dispatcher->setParams(array('hello' => 'World', 'goodbye' => 'Everybody')); $dispatcher->dispatch(); $this->assertTrue($dispatcher->hasParam('hello')); $this->assertTrue($dispatcher->hasParam('goodbye')); $this->assertFalse($dispatcher->hasParam('salutations')); // testing namespace try { $dispatcher->setDefaultNamespace('Dummy\\'); $dispatcher->setTaskName('main'); $dispatcher->setActionName('hello'); $dispatcher->setParams(array('World')); $dispatcher->dispatch(); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getActionName(), 'hello'); $this->assertEquals($dispatcher->getParams(), array('World')); $this->assertEquals($dispatcher->getReturnedValue(), 'Hello World!'); } catch (Exception $e) { $this->assertEquals($e->getMessage(), 'Dummy\\MainTask handler class cannot be loaded'); } }
error_reporting(E_ALL | E_NOTICE); if (!extension_loaded('phalcon')) { throw new Exception("Phalcon extension is required"); } $params = $argv; $file = array_shift($argv); $task = array_shift($argv); define('APP_ROOT', __DIR__ . '/app'); $loader = new Phalcon\Loader(); $loader->registerNamespaces(['ApiDocs' => APP_ROOT]); $loader->register(); $di = new Phalcon\DI\FactoryDefault\CLI(); $di->setShared('dispatcher', function () use($di) { $dispatcher = new Phalcon\CLI\Dispatcher(); $dispatcher->setDI($di); $dispatcher->setDefaultNamespace('ApiDocs\\Tasks'); return $dispatcher; }); $di->setShared('modelsManager', function () { return new Phalcon\Mvc\Model\Manager(); }); $di->setShared('db', function () use($di) { $connection = new Phalcon\Db\Adapter\Pdo\Mysql((array) $di->get('config')->db); return $connection; }); $di->setShared('config', function () { return new Phalcon\Config(require APP_ROOT . '/config/config.php'); }); $console = new Phalcon\CLI\Console(); $console->setDI($di); $console->handle(['task' => $task ?: 'help', 'action' => 'main', 'params' => $argv]);
$di = new \Phalcon\DI\FactoryDefault\CLI(); } //Setting MongoDB $di->set('mongo', function () { $mongo = new MongoClient(); return $mongo->selectDb("ppro"); }, true); //Registering the collectionManager service $di->set('collectionManager', function () { return new Phalcon\Mvc\Collection\Manager(); }, true); //Setting Router if (PHP_SAPI == 'cli') { $di->set('dispatcher', function () { $dispatcher = new Phalcon\CLI\Dispatcher(); $dispatcher->setDefaultNamespace('Notnull\\DailyNews\\Tasks'); return $dispatcher; }); } //Setting Router if (PHP_SAPI != 'cli') { $di->set('router', function () { return require __DIR__ . '/../app/config/routes.php'; }); } //Setting URL Helper $di->set('url', function () { $url = new Phalcon\Mvc\Url(); $base = 'http' . (isset($_SERVER['HTTPS']) ? 's' : ''); $base .= "://"; $base .= isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'revis.pt';