public function testDispatcher() { $this->specify("CLI Dispatcher doesn't work with typical parameters", function () { $di = new CliFactoryDefault(); $di->set('data', function () { return "data"; }); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); $dispatcher->dispatch(); expect($dispatcher->getTaskName())->equals('main'); expect($dispatcher->getActionName())->equals('main'); expect($dispatcher->getParams())->equals([]); expect($dispatcher->getReturnedValue())->equals('mainAction'); $dispatcher->setTaskName('echo'); $dispatcher->dispatch(); expect($dispatcher->getTaskName())->equals('echo'); expect($dispatcher->getActionName())->equals('main'); expect($dispatcher->getParams())->equals([]); expect($dispatcher->getReturnedValue())->equals('echoMainAction'); $dispatcher->setTaskName('main'); $dispatcher->setActionName('hello'); $dispatcher->dispatch(); expect($dispatcher->getTaskName())->equals('main'); expect($dispatcher->getActionName())->equals('hello'); expect($dispatcher->getParams())->equals([]); expect($dispatcher->getReturnedValue())->equals('Hello !'); $dispatcher->setActionName('hello'); $dispatcher->setParams(array('World', '######')); $dispatcher->dispatch(); expect($dispatcher->getTaskName())->equals('main'); expect($dispatcher->getActionName())->equals('hello'); expect($dispatcher->getParams())->equals(array('World', '######')); expect($dispatcher->getReturnedValue())->equals('Hello World######'); $dispatcher->setActionName('hello'); $dispatcher->setParams(array('hello' => 'World', 'goodbye' => 'Everybody')); $dispatcher->dispatch(); expect($dispatcher->hasParam('hello'))->true(); expect($dispatcher->hasParam('goodbye'))->true(); expect($dispatcher->hasParam('salutations'))->false(); // testing namespace try { $dispatcher->setDefaultNamespace('Dummy\\'); $dispatcher->setTaskName('main'); $dispatcher->setActionName('hello'); $dispatcher->setParams(array('World')); $dispatcher->dispatch(); expect($dispatcher->getTaskName())->equals('main'); expect($dispatcher->getActionName())->equals('hello'); expect($dispatcher->getParams())->equals(array('World')); expect($dispatcher->getReturnedValue())->equals('Hello World!'); } catch (\Exception $e) { expect($e->getMessage())->equals('Dummy\\MainTask handler class cannot be loaded'); } }); }
public function testConsoles() { $this->specify("CLI Console doesn't work with typical parameters", function () { $di = new CliFactoryDefault(); $di->set('data', function () { return "data"; }); $console = new Console(); $console->setDI($di); $dispatcher = $console->getDI()->getShared('dispatcher'); $console->handle([]); expect($dispatcher->getTaskName())->equals('main'); expect($dispatcher->getActionName())->equals('main'); expect($dispatcher->getParams())->equals([]); expect($dispatcher->getReturnedValue())->equals('mainAction'); $console->handle(['task' => 'echo']); expect($dispatcher->getTaskName())->equals('echo'); expect($dispatcher->getActionName())->equals('main'); expect($dispatcher->getParams())->equals([]); expect($dispatcher->getReturnedValue())->equals('echoMainAction'); $console->handle(['task' => 'main', 'action' => 'hello']); expect($dispatcher->getTaskName())->equals('main'); expect($dispatcher->getActionName())->equals('hello'); expect($dispatcher->getParams())->equals([]); expect($dispatcher->getReturnedValue())->equals('Hello !'); $console->handle(['task' => 'main', 'action' => 'hello', 'World', '######']); expect($dispatcher->getTaskName())->equals('main'); expect($dispatcher->getActionName())->equals('hello'); expect($dispatcher->getParams())->equals(array('World', '######')); expect($dispatcher->getReturnedValue())->equals('Hello World######'); }); $this->specify("CLI Console doesn't throw exception when module isn't found", function () { $di = new CliFactoryDefault(); $di->set('data', function () { return "data"; }); $console = new Console(); $console->setDI($di); $dispatcher = $console->getDI()->getShared('dispatcher'); // testing module $console->handle(['module' => 'devtools', 'task' => 'main', 'action' => 'hello', 'World', '######']); }, ['throws' => [\Phalcon\Cli\Console\Exception::class, "Module 'devtools' isn't registered in the console container"]]); $this->specify("CLI Console doesn't throw exception when task isn't found", function () { $di = new CliFactoryDefault(); $console = new Console(); $console->setDI($di); $dispatcher = $console->getDI()->getShared('dispatcher'); $dispatcher->setDefaultNamespace('Dummy\\'); // testing namespace $console->handle(['task' => 'main', 'action' => 'hello', 'World', '!']); }, ['throws' => [\Phalcon\Cli\Dispatcher\Exception::class, 'Dummy\\MainTask handler class cannot be loaded']]); }
public function testRouters() { $this->specify("CLI Router doesn't match the correct paths", function () { $di = new CliFactoryDefault(); $di->set("data", function () { return "data"; }); $router = new Router(); $router->handle([]); expect($router->getModuleName())->null(); expect($router->getTaskName())->null(); expect($router->getActionName())->null(); expect($router->getParams())->equals([]); $router->handle(["task" => "main"]); expect($router->getModuleName())->null(); expect($router->getTaskName())->equals("main"); expect($router->getActionName())->null(); expect($router->getParams())->equals([]); $router->handle(["task" => "echo"]); expect($router->getModuleName())->null(); expect($router->getTaskName())->equals("echo"); expect($router->getActionName())->null(); expect($router->getParams())->equals([]); $router->handle(["task" => "main", "action" => "hello"]); expect($router->getModuleName())->null(); expect($router->getTaskName())->equals("main"); expect($router->getActionName())->equals("hello"); expect($router->getParams())->equals([]); $router->handle(["task" => "main", "action" => "hello", "arg1", "arg2"]); expect($router->getModuleName())->null(); expect($router->getTaskName())->equals("main"); expect($router->getActionName())->equals("hello"); expect($router->getParams())->equals(["arg1", "arg2"]); $router->handle(["module" => "devtools", "task" => "main", "action" => "hello", "arg1", "arg2"]); expect($router->getModuleName())->equals("devtools"); expect($router->getTaskName())->equals("main"); expect($router->getActionName())->equals("hello"); expect($router->getParams())->equals(["arg1", "arg2"]); $router->handle(["module" => "devtools", "task" => "echo", "action" => "hello", "arg1", "arg2"]); expect($router->getModuleName())->equals("devtools"); expect($router->getTaskName())->equals("echo"); expect($router->getActionName())->equals("hello"); expect($router->getParams())->equals(["arg1", "arg2"]); }); }
/** * 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); $worker = new GearmanWorker(); $worker->setId(sha1(date("d.m.Y H:i:s"))); $worker->addServer('192.168.77.56', 4730); $worker->addFunction("send_metrix_calc", function ($job) use(&$console, &$di) { $di->set('workerJob', new MetrixResponse($di, $job, new FileAdapter(__DIR__ . "/logs/workjob_" . date("d-m-Y") . ".log"))); $arguments = json_decode($job->workload(), true); $console->setDi($di); $console->handle($arguments); }); while ($worker->work()) { if (GEARMAN_SUCCESS != $worker->returnCode()) { echo "Worker failed: " . $worker->error() . "\n"; } }
// set the date to come in Spanish setlocale(LC_TIME, "es_ES"); 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__))); // include composer include_once APPLICATION_PATH . "/vendor/autoload.php"; // Register the autoloader and tell it to register the tasks directory $loader = new \Phalcon\Loader(); $loader->registerDirs(array(APPLICATION_PATH . '/tasks', APPLICATION_PATH . '/classes/')); $loader->register(); // Creating the global path to the root folder $di->set('path', function () { return array("root" => APPLICATION_PATH, "http" => "https://apretaste.com"); }); // Making the config global $di->set('config', function () { return new ConfigIni(APPLICATION_PATH . '/configs/config.ini'); }); // Setup the database service $config = $di->get('config'); $di->set('db', function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config['database']['host'], "username" => $config['database']['user'], "password" => $config['database']['password'], "dbname" => $config['database']['database'])); }); // get the environment $di->set('environment', function () use($config) { if (isset($config['global']['environment'])) { return $config['global']['environment']; } else {
/** * @return $this */ public function createDependencies() { $dependency = new Cli(); $dependency->set('db', function () { return $this->getDatabase(); }); $dependency->setShared('application', $this); $this->setDI($dependency); return $this; }