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; }
public function execTask($argv) { $di = new CLI(); $application = new Console(); $this->load(APP_ROOT . 'apps/config/loadercli.php', $di); $application->setDI($di); $di->set('console', $application); $namespace = 'Xz\\Task\\'; $task = 'Main'; $action = 'main'; if (!empty($di['config']->console)) { $namespace = $di['config']->console->namespace; $task = $di['config']->console->task; $action = $di['config']->console->action; } $arguments = array('task' => $namespace . $task, 'action' => $action); foreach ($argv as $k => $arg) { if ($k == 1) { strpos($arg, '\\') !== false && ($namespace = ''); $arguments['task'] = $namespace . $arg; } elseif ($k == 2) { $arguments['action'] = $arg; } elseif ($k >= 3) { $arguments['params'][] = $arg; } } $application->handle($arguments); }
/** * @param $aOptions * @return mixed|void * @throws \Exception */ public function run($aOptions) { try { foreach ($this->loaders as $service) { $function = 'init' . ucfirst($service); $this->{$function}($aOptions); } // Create a console application $console = new ConsoleApp(); $console->setDI($this->oDI); return $console; } catch (\Exception $e) { throw $e; } }
// 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; } elseif ($k == 2) { $arguments['action'] = $arg; } elseif ($k >= 3) { $arguments['params'][] = $arg;
public function testArgumentOptions() { $this->specify("CLI Console doesn't work with options set in arguments", function () { $di = new CliFactoryDefault(); $di->setShared('router', function () { $router = new Router(true); return $router; }); $console = new Console(); $console->setDI($di); $dispatcher = $console->getDI()->getShared('dispatcher'); $console->setArgument(array('php', '-opt1', '--option2', '--option3=hoge', 'main', 'hello', 'World', '######'))->handle(); expect($dispatcher->getTaskName())->equals('main'); expect($dispatcher->getActionName())->equals('hello'); expect($dispatcher->getParams())->equals(array('World', '######')); expect($dispatcher->getReturnedValue())->equals('Hello World######'); expect($dispatcher->getOptions())->equals(array('opt1' => true, 'option2' => true, 'option3' => 'hoge')); $console->setArgument(array('php', 'main', '-opt1', 'hello', '--option2', 'World', '--option3=hoge', '######'))->handle(); expect($dispatcher->getTaskName())->equals('main'); expect($dispatcher->getActionName())->equals('hello'); expect($dispatcher->getParams())->equals(array('World', '######')); expect($dispatcher->getReturnedValue())->equals('Hello World######'); expect($dispatcher->getOptions())->equals(array('opt1' => true, 'option2' => true, 'option3' => 'hoge')); }); }
public function cliApp() { $console = new ConsoleApp(); $console->setDI(Bootstrap::get()->cliDi()); $console->registerModules(Bootstrap::get()->modules()); return $console; }
<?php use Phalcon\CLI\Console as ConsoleApp; $config = (include "app/config/config.php"); include "app/config/loader.php"; include "app/config/services.php"; $arguments = array(); foreach ($argv as $k => $arg) { if ($k == 1) { $arguments['task'] = $arg; } elseif ($k == 2) { $arguments['action'] = $arg; } elseif ($k >= 3) { $arguments[] = $arg; } } try { $app = new ConsoleApp(); $app->setDI($di); $app->handle($arguments); } catch (Exception $e) { echo $e->getMessage(); }