public function __construct($argv) { //Using the CLI factory default services container $di = new CliDI(); //Register the autoloader and tell it to register the tasks directory include __DIR__ . '/../config/loader.php'; //Load the configuration file $config = (include __DIR__ . '/../config/config.php'); $di->set('config', $config); include __DIR__ . '/../config/services.php'; //Create a console application $console = new ConsoleApp(); $console->setDI($di); //Process the console arguments foreach ($argv as $k => $arg) { if ($k == 1) { $this->arguments['task'] = $arg; } elseif ($k == 2) { $this->arguments['action'] = $arg; } elseif ($k >= 3) { $this->params[] = $arg; } } if (count($this->params) > 0) { $this->arguments['params'] = $this->params; } $this->console = $console; }
use Phalcon\DI\FactoryDefault\CLI as CliDI, Phalcon\CLI\Console as ConsoleApp; define('VERSION', '1.0.0'); // Using the CLI factory default services container $di = new CliDI(); // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__))); /** * Register the autoloader and tell it to register the tasks directory */ $loader = new \Phalcon\Loader(); $loader->registerDirs(array(APPLICATION_PATH . '/tasks', APPLICATION_PATH . '/models', APPLICATION_PATH . '/classes')); $loader->register(); // Load the configuration file (if any) if (is_readable(APPLICATION_PATH . '/config/config.php')) { $config = (include APPLICATION_PATH . '/config/config.php'); $di->set('config', $config); } // Create a console application $console = new ConsoleApp(); $console->setDI($di); //Setup the database service $di->set('db', function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname)); }); /** * Process the console arguments */ $arguments = array(); foreach ($argv as $k => $arg) { if ($k == 1) { $arguments['task'] = $arg;
/** * CLI 模式下的 DI 配置 * * @param CLI $di */ protected function cliDI(CLI $di) { global $argv; $di->set('router', function () use($di, $argv) { $router = new CLIRouter(); $router->setDI($di); return $router; }); $di->set('output', function () { return new ConsoleOutput(); }); $di->set('dispatcher', function () use($di, $argv) { $dispatcher = new CLIDispatcher(); $dispatcher->setDI($di); $moduleName = array_shift($argv); $taskName = array_shift($argv); $actionName = 'main'; if (strpos($taskName, ':') > 0) { @(list($taskName, $actionName) = preg_split("/:/", $taskName)); } if ($moduleName) { $dispatcher->setTaskName(ucwords($taskName)); $dispatcher->setActionName($actionName); $dispatcher->setParams($argv); if ($moduleName == '_current') { $_appName = ucwords($this->getAppName()); $dispatcher->setNamespaceName("{$_appName}\\Tasks"); } else { $dispatcher->setNamespaceName("Eva\\{$moduleName}\\Tasks"); } } else { $dispatcher->setTaskName('Main'); $dispatcher->setParams($argv); $dispatcher->setNamespaceName("Eva\\EvaEngine\\Tasks"); } return $dispatcher; }); }
* @date: 10.10.2015 */ use Phalcon\DI\FactoryDefault\CLI as CliDI, Phalcon\CLI\Console as ConsoleApp; try { // Using the CLI factory default services container $di = new CliDI(); // Define path to application directory defined('APP_PATH') || define('APP_PATH', realpath(dirname(__FILE__))); // Load necessary $loader = new \Phalcon\Loader(); $loader->registerDirs(array(APP_PATH . '/tasks', APP_PATH . '/models', APP_PATH . '/library', APP_PATH . '/library/PHPExcel/Classes/')); $loader->register(); // Load the conf if (is_readable(APP_PATH . '/config/config.php')) { $config = (include APP_PATH . '/config/config.php'); $di->set('config', $config); } // Mongo connection $di->set('mongo', function () use($config) { if (!$config->mongo->user or !$config->mongo->pass) { $mongo = new MongoClient('mongodb://' . $config->mongo->host); } else { $mongo = new MongoClient("mongodb://" . $config->mongo->user . ":" . $config->mongo->pass . "@" . $config->mongo->host, array("db" => $config->mongo->dbname)); } return $mongo->selectDb($config->mongo->dbname); }, false); // Load collection nanager $di->set('collectionManager', function () { return new \Phalcon\Mvc\Collection\Manager(); }); // Create a console app
use Phalcon\DI\FactoryDefault\CLI as CliDI, Phalcon\CLI\Console as ConsoleApp; define('VERSION', '1.0.0'); //Using the CLI factory default services container $di = new CliDI(); // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__))); /** * Register the autoloader and tell it to register the tasks directory */ $loader = new \Phalcon\Loader(); $loader->registerDirs(array(APPLICATION_PATH . '/tasks', APPLICATION_PATH . '/models', APPLICATION_PATH . '/library')); $loader->register(); // Load the configuration file (if any) if (is_readable(APPLICATION_PATH . '/config/config.php')) { $config = (include APPLICATION_PATH . '/config/config.php'); $di->set('config', $config); } $config = $di->get('config'); //Setup the database service $di->set('db', function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname)); }); $di->set('libaccess', function () use($config) { return new Mails(); }); /** * Loading routes from the routes.php file */ // $di->set('router', function() { // return require APPLICATION_PATH . '/config/routers.php'; // });
define('CONFIG_PATH', APPFULLPATH . DIRS . 'config'); } } $conf = new \Phalcon\Config\Adapter\Ini(CONFIG_PATH . DIRS . "ini/config.ini"); if (!defined('SITESLUG')) { define('SITESLUG', $conf->site->slug); } /** * Register the autoloader and tell it to register the tasks directory */ $loader = new \Phalcon\Loader(); $loader->registerNamespaces(array('PhpAmqpLib' => ROOTFULLPATH . '/vendor/videlalvaro/php-amqplib/PhpAmqpLib', 'Rpp\\Services' => APPFULLPATH . '/Rpp/Services/', 'Shared' => APPFULLPATH . '/Rpp/Services/Shared/Publish/', 'Rpp\\Extend' => APPFULLPATH . '/Rpp/Extend/', 'Rpp\\Analytics' => APPFULLPATH . '/Rpp/Analytics/', 'Rpp\\Destacados' => APPFULLPATH . '/Rpp/Destacados/', 'Rpp\\Repositorio' => APPFULLPATH . '/Rpp/Repositorio/', 'Rpp\\Dominio' => APPFULLPATH . '/Rpp/Dominio/')); $loader->registerDirs(array(ROOTFULLPATH . DIRS . 'app/tasks')); $loader->register(); $di->set('collectionManager', function () { return new Phalcon\Mvc\Collection\Manager(); }, true); $di->set('mongo', function () use($conf) { $mongo = new MongoClient("mongodb://" . $conf->mongo->credentials . $conf->mongo->host . ":" . $conf->mongo->port . "/" . $conf->mongo->db); return $mongo->selectDB($conf->mongo->db); }, true); $di->set('cache', function () use($conf) { $frontCache = new \Phalcon\Cache\Frontend\Data(array("lifetime" => $conf->cache->lifetime)); $cache = false; $cache = new \Rpp\Extend\Memcache($frontCache, array("host" => $conf->cache->memcache->host, "port" => $conf->cache->memcache->port, "prefix" => $conf->cache->memcache->prefix, "statsKey" => $conf->cache->memcache->stats)); $cache->localconnect(); return $cache; }); $view = new \Phalcon\Mvc\View(); $view->setViewsDir(APPFULLPATH . $conf->phalcon->viewsDir); $view->registerEngines(array($conf->volt->extn => function ($view, $di) {
<?php use Phalcon\DI\FactoryDefault\CLI as CliDI; use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter; $di = new CliDI(); $di->set('db', function () use($config) { $connection = new DbAdapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname)); return $connection; });
use Phalcon\DI\FactoryDefault\CLI as CliDI; use Phalcon\CLI\Console as ConsoleApp; define('VERSION', '1.0.0'); // Composer AutoLoad include __DIR__ . "/../vendor/autoload.php"; //使用CLI工厂类作为默认的服务容器 $di = new CliDI(); // 定义应用目录路径 defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__))); // 注册类自动加载器 $loader = new \Phalcon\Loader(); $loader->registerDirs([APPLICATION_PATH . '/tasks']); $loader->register(); // 加载配置文件 $config = (require APPLICATION_PATH . '/config/config.php'); $di->set('config', $config); // Queue $di->set('queue', function () use($di) { $config = $di->getShared('config'); return new \Phalcon\Queue\Beanstalk\Extended(['host' => $config->queue->host, 'port' => $config->queue->port, 'prefix' => $config->queue->prefix]); }); // 创建console应用 $console = new ConsoleApp(); $console->setDI($di); /** * 处理console应用参数 */ $arguments = []; foreach ($argv as $k => $arg) { if ($k == 1) { $arguments['task'] = $arg;