Example #1
0
 public function testIssue787()
 {
     $di = new \Phalcon\DI\FactoryDefault\CLI();
     $di->setShared('dispatcher', function () use($di) {
         $dispatcher = new Phalcon\CLI\Dispatcher();
         $dispatcher->setDI($di);
         return $dispatcher;
     });
     $console = new \Phalcon\CLI\Console();
     $console->setDI($di);
     $console->handle(array('task' => 'issue787', 'action' => 'main'));
     $this->assertTrue(class_exists('Issue787Task'));
     $actual = Issue787Task::$output;
     $expected = "beforeExecuteRoute\ninitialize\n";
     $this->assertEquals($actual, $expected);
 }
}
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]);
Example #3
0
 public function testArgumentOptions()
 {
     $di = new Phalcon\DI\FactoryDefault\CLI();
     $di->setShared('router', function () {
         $router = new \Phalcon\Cli\Router(true);
         return $router;
     });
     $console = new \Phalcon\CLI\Console();
     $console->setDI($di);
     $dispatcher = $console->getDI()->getShared('dispatcher');
     $console->setArgument(array('php', '-opt1', '--option2', '--option3=hoge', 'main', 'hello', 'World', '######'))->handle();
     $this->assertEquals($dispatcher->getTaskName(), 'main');
     $this->assertEquals($dispatcher->getActionName(), 'hello');
     $this->assertEquals($dispatcher->getParams(), array('World', '######'));
     $this->assertEquals($dispatcher->getReturnedValue(), 'Hello World######');
     $this->assertEquals($dispatcher->getOptions(), array('opt1' => true, 'option2' => true, 'option3' => 'hoge'));
     $console->setArgument(array('php', 'main', '-opt1', 'hello', '--option2', 'World', '--option3=hoge', '######'))->handle();
     $this->assertEquals($dispatcher->getTaskName(), 'main');
     $this->assertEquals($dispatcher->getActionName(), 'hello');
     $this->assertEquals($dispatcher->getParams(), array('World', '######'));
     $this->assertEquals($dispatcher->getReturnedValue(), 'Hello World######');
     $this->assertEquals($dispatcher->getOptions(), array('opt1' => true, 'option2' => true, 'option3' => 'hoge'));
 }
Example #4
0
<?php

define('VERSION', '1.0.0');
// Define path to application directory
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__)));
// Create service container
$di = new \Phalcon\DI\FactoryDefault\CLI();
$application = new \Phalcon\CLI\Console();
require_once APPLICATION_PATH . '/autoload.php';
$di->setShared('console', $application);
/**
 * 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[] = $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);
try {
    // handle incoming arguments
    $application->handle($arguments);
} catch (\Phalcon\Exception $e) {
    echo $e->getMessage();
Example #5
0
 /**
  * Set Dependency Injector with configuration variables
  *
  *
  * @param string $file full path to configuration file
  * @throws \Exception
  */
 public function setConfig($file)
 {
     //        static $config;
     $di = new \Phalcon\DI\FactoryDefault\CLI();
     $di->set('config', new \Phalcon\Config(require $file));
     $config = $di->get('config');
     $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . ucfirst($config->dbMaster->adapter);
     /** @var Pdo $connMaster */
     $connMaster = new $adapter(["host" => $config->dbMaster->host, "port" => $config->dbMaster->port, "username" => $config->dbMaster->username, "password" => $config->dbMaster->password, "dbname" => $config->dbMaster->dbname, "prefix" => $config->dbMaster->prefix, 'options' => [\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '" . $config->dbMaster->charset . "'", \PDO::ATTR_CASE => \PDO::CASE_LOWER, \PDO::ATTR_PERSISTENT => true]]);
     $di->set('dbMaster', $connMaster);
     $caches['cache'] = $config->cache->toArray();
     if (!empty($config->cacheSlave)) {
         foreach ($config->cacheSlave as $cache) {
             $caches['cacheSlave'][] = $cache->toArray();
         }
     }
     $adapter = ucfirst($config->cache->adapter);
     if ($adapter == "Redis") {
         $cacheAdapter = '\\Engine\\' . $adapter;
     } else {
         $cacheAdapter = '\\Phalcon\\Cache\\Backend\\' . $adapter;
     }
     $frontEndOptions = ['lifetime' => $config->cache->lifetime];
     $cacheDataAdapter = new $cacheAdapter($frontEndOptions, $caches);
     $di->set('cacheData', $cacheDataAdapter, true);
     $di->setShared('transactions', function () {
         $manager = new \Phalcon\Mvc\Model\Transaction\Manager();
         return $manager->setDbService("dbMaster");
     });
     $this->setDI($di);
 }
Example #6
0
 public function testCallActionMethod()
 {
     $di = new \Phalcon\DI\FactoryDefault\CLI();
     $dispatcher = new \Phalcon\CLI\Dispatcher();
     $di->setShared("dispatcher", $dispatcher);
     $dispatcher->setDI($di);
     $mainTask = new MainTask();
     $mainTask->setDI($di);
     $this->assertEquals($dispatcher->callActionMethod($mainTask, 'mainAction', []), 'mainAction');
     $this->assertEquals($dispatcher->callActionMethod($mainTask, 'helloAction', ['World']), 'Hello World!');
     $this->assertEquals($dispatcher->callActionMethod($mainTask, 'helloAction', ['World', '.']), 'Hello World.');
 }