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); }
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; }
/** * @param array $config * @return Console */ public static function createCliFrom(array $config) : Console { $di = Di::createCliFrom($config); $application = new Console($di); if ($di->has('applicationEventManager')) { $application->setEventsManager($di->getShared('applicationEventManager')); } return $application; }
public function execute(Console $console) { $this->getWriteConnection()->execute("\n\t\t\tupdate {$this->getSource()} set executed_at = ? where cron_job_id = ?\n\t\t", [$this->executed_at = date("Y-m-d H:i:s"), $this->cron_job_id]); try { $console->handle(array('task' => $this->task_name, 'action' => $this->action_name)); $this->save(['last_message' => '', 'completed_at' => date('Y-m-d H:i:s'), 'locked_till' => time() + $this->delay]); Assert::saved($this); return static::RESULT_OK; } catch (Exception $e) { $this->getLogger()->exception($e); $this->save(['last_message' => get_class($e) . ': ' . $e->getMessage(), 'completed_at' => null, 'locked_till' => time() + $this->delay]); return static::RESULT_ERR; } }
/** * @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; } }
/** * Handle the whole command-line tasks * * @param array $arguments Cli arguments * * @return mixed * @throws \Phalcon\Cli\Console\Exception */ public function handle(array $arguments = null) { if (isset($arguments['task']) && in_array($arguments['task'], array('-h', '--help', 'help'))) { $this->setTasksDir(); $this->createHelp(); $this->showHelp(); return; } elseif (isset($arguments['action']) && in_array($arguments['action'], array('-h', '--help', 'help'))) { $this->setTasksDir(); $this->createHelp(); $this->showTaskHelp($arguments['task']); return; } parent::handle($arguments); }
/** * handles the Console application and starts the Phalcon Module * * @param array $arguments */ public function handle($arguments = null) { $config = $this->getDI()->getConfig(); $this->progPath = array_shift($arguments['params']); $this->cmd = array_shift($arguments['params']); if (is_null($this->cmd)) { if (!isset($arguments['defaultCmd'])) { throw new \Exception('The Console was not given a command', 1); } $this->cmd = $arguments['defaultCmd']; } if (in_array($this->cmd, ['help', '--help', '-h'])) { $this->printCmdList(); exit(0); } if (strpos($this->cmd, '.') !== false || strpos($this->cmd, '/') !== false) { throw new \Exception('Invalid command name', 1); } // All environments $cmdArr = (require "{$config->path->modulesDir}/{$arguments['module']}/cmd.php"); if (DEV_ENV === ENV) { $devCmdArr = (require "{$config->dev->path->devDir}/cmd_overrides.php"); $cmdArr = array_replace($cmdArr, $devCmdArr); } if (!array_key_exists($this->cmd, $cmdArr)) { throw new \Exception('The command description does not exist', 1); } $taskParts = explode('::', $cmdArr[$this->cmd]); $task = $taskParts[0]; $action = isset($taskParts[1]) ? $taskParts[1] : 'main'; try { parent::handle(['module' => $arguments['module'], 'task' => 'Webird\\Cli\\Tasks\\' . ucfirst($task), 'action' => $action, 'params' => $arguments['params']]); } catch (ArgumentValidationException $e) { $this->printHelpRecommend($e->getMessage()); exit(1); } catch (PrintHelpException $e) { if ($e->getCode() == 1) { $this->printHelpRecommend($e->getMessage()); exit(1); } else { $this->printHelp($e->getCmdDef(), $e->getSpecs()); exit(0); } } }
<?php use Phalcon\Di\FactoryDefault\Cli; use Phalcon\Cli\Console; require __DIR__ . '/../vendor/autoload.php'; $di = new Cli(); $di->get('dispatcher')->setNamespaceName('CaioFRAlmeida\\SoccerCompanyEvent\\Task'); $args = []; if (isset($argv[1])) { $args['task'] = $argv[1]; } $console = new Console($di); try { $console->handle($args); } catch (\Exception $e) { die('erro ao executar task'); }
* Read auto-loader */ include __DIR__ . '/config/loader.php'; /** * Read the configuration */ $config = (include __DIR__ . '/config/config.php'); /** * Read the services */ $di = new CliDi(); include __DIR__ . '/config/services.php'; /** * Create a console application */ $console = new ConsoleApp($di); /** * 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; } } try { /**
/** * Handle the command-line arguments * * @package las * @version 1.0 * * @param mixed $arguments */ public function handle($arguments = null) { $params = array(); switch (count($arguments)) { case 1: $task = 'main'; $action = 'main'; break; case 2: $task = $arguments[1]; $action = 'main'; break; case 3: $task = $arguments[1]; $action = $arguments[2]; break; default: $task = $arguments[1]; $action = $arguments[2]; $params = array_slice($arguments, 3); break; } $arguments = array_merge(array('module' => 'cli', 'task' => $task, 'action' => $action), $params); parent::handle($arguments); }
<?php use Phalcon\DI\FactoryDefault\CLI as CliDI, Phalcon\CLI\Console as ConsoleApp; //Using the CLI factory default services container $di = new CliDI(); //Create a console application $console = new ConsoleApp(); $console->setDI($di); // $console->handle(array('task' => 'shell_script_name', 'action' => 'echo'));
public function setModules($modules) { parent::registerModules($modules); }
<?php use Phalcon\Exception; use Phalcon\DI\FactoryDefault\CLI; use Phalcon\CLI\Console; date_default_timezone_set('America/New_York'); //load composer dependencies require __DIR__ . '/../../vendor/autoload.php'; //Using the CLI factory default services container $di = new CLI(); //load configuration $config = (include __DIR__ . '/config.php'); //load classes include __DIR__ . '/loader.php'; //load services class include __DIR__ . '/services.php'; Services::load($di, $config); //Create a console application $console = new Console(); $console->setDI($di); try { $console->handle(array('task' => 'Service', 'action' => 'listen')); } catch (\Phalcon\Exception $e) { echo $e->getMessage(); exit(255); }
/** * Register an array of modules present in the application * *<code> * $this->registerModules(array( * 'frontend' => array( * 'className' => 'Multiple\Frontend\Module', * 'path' => 'app/frontend/Module.php' * ), * 'backend' => array( * 'className' => 'Multiple\Backend\Module', * 'path' => '../apps/backend/Module.php' * ) * )); *</code> * * @param array $modules */ public function registerModules($modules) { $this->registerSharedData($modules); parent::registerModules($modules); }
<?php use Phalcon\CLI\Console as ConsoleApp; // Create a console application $console = new ConsoleApp(); $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; } } // define global constants for the current task and action define('CURRENT_TASK', isset($argv[1]) ? $argv[1] : null); define('CURRENT_ACTION', isset($argv[2]) ? $argv[2] : null);
/** * Console constructor - set the dependency Injector. * * @param \Phalcon\DiInterface $di */ public function __construct(DiInterface $di) { $this->_di = $di; $this->_stderr = $this->_stdout = ''; $this->_isSingleInstance = $this->_isRecording = false; $this->_task = $this->_action = null; $this->_params = array(); $this->_taskId = null; $loaders = array('config', 'loader', 'db', 'router', 'markdown', 'mail', 'view', 'queue'); // Register services foreach ($loaders as $service) { $this->{$service}(); } // Register the app itself as a service $this->_di->set('app', $this); // Set the dependency Injector parent::__construct($this->_di); }
public function cliApp() { $console = new ConsoleApp(); $console->setDI(Bootstrap::get()->cliDi()); $console->registerModules(Bootstrap::get()->modules()); return $console; }
$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 $console = new ConsoleApp(); $console->setDI($di); } catch (Exception $e) { echo $e->getMessage(); } try { $console->handle(); } catch (\Phalcon\Exception $e) { echo $e->getMessage(); }
<?php use Phalcon\DI\FactoryDefault\CLI as CliDI, Phalcon\CLI\Console as ConsoleApp, Phalcon\DI; //Using the CLI factory default services container $di = DI::setDefault(new CliDI()); require __DIR__ . '/../../autoloader.php'; /** * Register the autoloader and tell it to register the tasks directory */ $loader = new \Phalcon\Loader(); $dirs = []; $dirs[] = __DIR__ . '/tasks'; //registra diretorios que serao lidos $loader->registerDirs($dirs)->register(); //Create a console application $console = new ConsoleApp(); $console->setDI($di); $di->setShared('console', $console); $di->setShared('consoleDispatcher', function () use($di) { $dispatcher = new \Phalcon\CLI\Dispatcher(); $dispatcher->setDI($di); return $dispatcher; }); /** * Process the console arguments */ $arguments = array(); foreach ($argv as $k => $arg) { if ($k == 1) { $arguments['task'] = $arg; } elseif ($k == 2) {
$this->log->info('Каталог бэкапа {folder} отсутствовал, создали его.', ['folder' => $this->folderName]); } elseif (is_dir($this->folderName) && is_writable($this->folderName)) { $this->log->info('Каталог бэкапа {folder} уже существует и доступен для записи', ['folder' => $this->folderName]); } else { $this->log->error('Каталог бэкапа {folder} отсутствует или недоступен для создания', ['folder' => $this->folderName]); } } } $di = new CLI(); $di['dispatcher'] = function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultTask('Backup'); $dispatcher->setDefaultAction('backup'); return $dispatcher; }; $di['log'] = function () { return new Phalcon\Logger\Adapter\Stream('php://stdout'); }; try { $console = new Console($di); $handleParams = []; array_shift($argv); foreach ($argv as $param) { list($name, $value) = explode('=', $param); $handleParams[$name] = $value; } $console->handle($handleParams); } catch (\Phalcon\Exception $e) { echo $e->getMessage(); exit(255); }
/** * Register the autoloader and tell it to register the tasks directory */ $loader = new \Phalcon\Loader(); $loader->registerNamespaces($config->namespaces->toArray()); $loader->register(); $di->set('config', $config); $di->setShared('db', function () use($config) { $dbConfig = $config->database->toArray(); $adapter = $dbConfig['adapter']; unset($dbConfig['adapter']); $class = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; return new $class($dbConfig); }); // Create a console application $console = new ConsoleApp(); $console->setDI($di); /** * 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; } } // Define global constants for the current task and action
/** * Include cli environment specific services */ include APP_PATH . '/config/services_cli.php'; /** * Include Autoloader */ include APP_PATH . '/config/loader.php'; /** * Get config service for use in inline setup below */ $config = $di->getConfig(); /** * Create a console application */ $console = new ConsoleApp($di); /** * Register console modules */ $console->registerModules(['cli' => ['className' => '@@namespace@@\\Modules\\Cli\\Module']]); /** * Setup the arguments to use the 'cli' module */ $arguments = ['module' => 'cli']; /** * Process the console arguments */ foreach ($argv as $k => $arg) { if ($k == 1) { $arguments['task'] = $arg; } elseif ($k == 2) {
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')); }); }
$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; } elseif ($k == 2) { $arguments['action'] = $arg; } elseif ($k >= 3) {
<?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(); }