public function testDispatcher() { $di = new \Phalcon\DI\FactoryDefault\CLI(); $di->set('data', function () { return "data"; }); $dispatcher = new \Phalcon\CLI\Dispatcher(); $dispatcher->setDI($di); $dispatcher->dispatch(); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getTaskClass(), 'MainTask'); $this->assertEquals($dispatcher->getActionName(), 'main'); $this->assertEquals($dispatcher->getParams(), array()); $this->assertEquals($dispatcher->getReturnedValue(), 'mainAction'); $dispatcher->setTaskName('echo'); $dispatcher->dispatch(); $this->assertEquals($dispatcher->getTaskName(), 'echo'); $this->assertEquals($dispatcher->getActionName(), 'main'); $this->assertEquals($dispatcher->getParams(), array()); $this->assertEquals($dispatcher->getReturnedValue(), 'echoMainAction'); $dispatcher->setTaskName('main'); $dispatcher->setActionName('hello'); $dispatcher->dispatch(); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getActionName(), 'hello'); $this->assertEquals($dispatcher->getParams(), array()); $this->assertEquals($dispatcher->getReturnedValue(), 'Hello !'); $dispatcher->setActionName('hello'); $dispatcher->setParams(array('World', '######')); $dispatcher->dispatch(); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getActionName(), 'hello'); $this->assertEquals($dispatcher->getParams(), array('World', '######')); $this->assertEquals($dispatcher->getReturnedValue(), 'Hello World######'); // testing namespace try { $dispatcher->setDefaultNamespace('Dummy\\'); $dispatcher->setTaskName('main'); $dispatcher->setActionName('hello'); $dispatcher->setParams(array('World')); $dispatcher->dispatch(); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getActionName(), 'hello'); $this->assertEquals($dispatcher->getParams(), array('World')); $this->assertEquals($dispatcher->getReturnedValue(), 'Hello World!'); } catch (Exception $e) { $this->assertEquals($e->getMessage(), 'Dummy\\MainTask handler class cannot be loaded'); } }
/** * 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); }
public function testTasks() { $di = new \Phalcon\DI\FactoryDefault\CLI(); $di->set('data', function () { return "data"; }); $task = new MainTask(); $task->setDI($di); $this->assertEquals($task->requestDiAction(), 'data'); $this->assertEquals($task->helloAction(), 'Hello !'); $this->assertEquals($task->helloAction('World'), 'Hello World!'); $task2 = new EchoTask(); $task2->setDI($di); $this->assertEquals($task2->mainAction(), 'echoMainAction'); }
public function testRouters() { $di = new \Phalcon\DI\FactoryDefault\CLI(); $di->set('data', function () { return "data"; }); $router = new \Phalcon\CLI\Router(); $router->handle(array()); $this->assertEquals($router->getModuleName(), null); $this->assertEquals($router->getTaskName(), null); $this->assertEquals($router->getActionName(), null); $this->assertEquals($router->getParams(), array()); $router->handle(array('task' => 'main')); $this->assertEquals($router->getModuleName(), null); $this->assertEquals($router->getTaskName(), 'main'); $this->assertEquals($router->getActionName(), null); $this->assertEquals($router->getParams(), array()); $router->handle(array('task' => 'echo')); $this->assertEquals($router->getModuleName(), null); $this->assertEquals($router->getTaskName(), 'echo'); $this->assertEquals($router->getActionName(), null); $this->assertEquals($router->getParams(), array()); $router->handle(array('task' => 'main', 'action' => 'hello')); $this->assertEquals($router->getModuleName(), null); $this->assertEquals($router->getTaskName(), 'main'); $this->assertEquals($router->getActionName(), 'hello'); $this->assertEquals($router->getParams(), array()); $router->handle(array('task' => 'main', 'action' => 'hello', 'arg1', 'arg2')); $this->assertEquals($router->getModuleName(), null); $this->assertEquals($router->getTaskName(), 'main'); $this->assertEquals($router->getActionName(), 'hello'); $this->assertEquals($router->getParams(), array('arg1', 'arg2')); $router->handle(array('module' => 'devtools', 'task' => 'main', 'action' => 'hello', 'arg1', 'arg2')); $this->assertEquals($router->getModuleName(), 'devtools'); $this->assertEquals($router->getTaskName(), 'main'); $this->assertEquals($router->getActionName(), 'hello'); $this->assertEquals($router->getParams(), array('arg1', 'arg2')); $router->handle(array('module' => 'devtools', 'task' => 'echo', 'action' => 'hello', 'arg1', 'arg2')); $this->assertEquals($router->getModuleName(), 'devtools'); $this->assertEquals($router->getTaskName(), 'echo'); $this->assertEquals($router->getActionName(), 'hello'); $this->assertEquals($router->getParams(), array('arg1', 'arg2')); }
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); }
echo "{$backTrace[0]['file']} ({$backTrace[0]['line']})" . PHP_EOL . PHP_EOL; $is_dump || !$str || is_bool($str) || is_numeric($str) ? var_dump($str) : print_r($str); die(PHP_EOL . PHP_EOL); } 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');
/** * Set Dependency Injector with configuration variables * * @throws Exception * @param string $file full path to configuration file */ public function setConfig($file) { if (!file_exists($file)) { throw new \Exception('Unable to load configuration file'); } $di = new \Phalcon\DI\FactoryDefault\CLI(); $di->set('config', new \Phalcon\Config(require $file)); $di->set('db', function () use($di) { $type = strtolower($di->get('config')->database->adapter); $creds = array('host' => $di->get('config')->database->host, 'username' => $di->get('config')->database->username, 'password' => $di->get('config')->database->password, 'dbname' => $di->get('config')->database->name); if ($type == 'mysql') { $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($creds); } else { if ($type == 'postgres') { $connection = new \Phalcon\Db\Adapter\Pdo\Postgesql($creds); } else { if ($type == 'sqlite') { $connection = new \Phalcon\Db\Adapter\Pdo\Sqlite($creds); } else { throw new Exception('Bad Database Adapter'); } } } $connection->setEventsManager(new \Events\Database\Profile()); return $connection; }); $this->setDI($di); }
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')); }
error_reporting(E_ALL); set_time_limit(0); define('APP_PATH', realpath('.')); try { //Read the configuration $config = (include APP_PATH . "/app/config/config.php"); $loader = new \Phalcon\Loader(); // var_dump($config); /** * We're a registering a set of directories taken from the configuration file */ $loader->registerDirs(array($config->application->controllersDir, $config->application->pluginsDir, $config->application->libraryDir, $config->application->modelsDir, $config->application->tasksDir))->register(); /** * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework */ $di = new \Phalcon\DI\FactoryDefault\CLI(); /** * Database connection is created based in the parameters defined in the configuration file */ $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)); }); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise */ $di->set('modelsMetadata', function () use($config) { if (isset($config->models->metadata)) { $metaDataConfig = $config->models->metadata; $metadataAdapter = 'Phalcon\\Mvc\\Model\\Metadata\\' . $metaDataConfig->adapter; return new $metadataAdapter(); } else {
if (!$environment || !in_array($environment, array('development', 'staging', 'production'), true)) { define('ENVIRONMENT', 'development'); } else { define('ENVIRONMENT', $environment); } unset($environment); } define('SITENAME', 'Backend'); require ROOT . DS . 'vendor' . DS . 'autoload.php'; /** * Registering a set of directories taken from the configuration file */ $loader = new \Phalcon\Loader(); $loader->registerDirs(array(ROOT . DS . 'library', ROOT . DS . 'models', ROOT . DS . 'plugins', APPLICATION_PATH . DS . 'Library', APPLICATION_PATH . DS . 'Plugins', APPLICATION_PATH . DS . 'Models', APPLICATION_PATH . DS . 'Tasks')); $loader->registerNamespaces(array('Lininliao\\Library' => ROOT . DS . 'library', 'Lininliao\\Models' => ROOT . DS . 'models', 'Lininliao\\Plugins' => ROOT . DS . 'plugins', 'Lininliao\\Backend\\Library' => APPLICATION_PATH . DS . 'Library', 'Lininliao\\Backend\\Plugins' => APPLICATION_PATH . DS . 'Plugins', 'Lininliao\\Backend\\Models' => APPLICATION_PATH . DS . 'Models'))->register(); $di = new \Phalcon\DI\FactoryDefault\CLI(); $config = new \Phalcon\Config\Adapter\Ini(ROOT . DS . 'configs' . DS . 'config.ini'); $di->set('config', $config, true); /** * Database connection is created based in the parameters defined in the configuration file */ $di->set('db', function () use($config) { $db_config = $config->database[ENVIRONMENT]; return new \Phalcon\Db\Adapter\Pdo\Mysql(array('host' => $db_config->dbhost, 'username' => $db_config->dbusername, 'password' => $db_config->dbpassword, 'dbname' => $db_config->dbname)); }, true); $di->set('acl', function () use($di, $config) { return new Phalcon\Acl\Adapter\Database(array('db' => $di->get('db'), 'roles' => 'roles', 'rolesInherits' => 'roles_inherits', 'resources' => 'resources', 'resourcesAccesses' => 'resources_accesses', 'accessList' => 'access_list')); }, true); $di->set('translate', function () { return new Lininliao\Library\Locale\Translate(SITENAME); }, true);
<?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();
<?php error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', 1); include __DIR__ . "/common/define/var.php"; $loader = new \Phalcon\Loader(); $loader->registerNamespaces(array('Modules\\Frontend\\Controllers' => __DIR__ . '/frontend/controllers/', 'Modules\\Frontend\\Models' => __DIR__ . '/frontend/models/', 'Modules\\Frontend\\Services' => __DIR__ . '/frontend/services/')); $loader->registerDirs(array(__DIR__ . '/frontend/tasks/')); $loader->register(); $di = new Phalcon\DI\FactoryDefault\CLI(); $config = (require __DIR__ . "/common/configs/config.php"); $di->set('crm', function () use($config) { return new Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->crm->host, "username" => $config->crm->username, "password" => $config->crm->password, "dbname" => $config->crm->name, 'charset' => $config->crm->charset)); }); $di->set('ads', function () use($config) { return new Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->ads->host, "username" => $config->ads->username, "password" => $config->ads->password, "dbname" => $config->ads->name)); }); $di->set('collectionManager', function () { return new Phalcon\Mvc\Collection\Manager(); }, true); $di->set('bigdata', function () use($config) { if (!$config->bigdata->username or !$config->bigdata->password) { $mongo = new MongoClient('mongodb://' . $config->bigdata->host); } else { $mongo = new MongoClient("mongodb://" . $config->bigdata->username . ":" . $config->bigdata->password . "@" . $config->bigdata->host, array("db" => $config->bigdata->name)); } return $mongo->selectDb($config->bigdata->name); }, TRUE); $di->set('crmcache', function () { // Cache data for one day by default $frontCache = new \Phalcon\Cache\Frontend\Data(array("lifetime" => 86400));
} else { $cliData['params'][$key] = $value; } } else { //无效参数抛弃 } } } if (!isset($cliData['module']) || !isset($cliData['task']) || !isset($cliData['action'])) { exit("The params module/task/action is not have"); } $config = (include WEBROOT . '/config/console_test.php'); //加载程序文件 $loader = new \Phalcon\Loader(); $loader->registerDirs($config->autoload->toArray())->register(); $di = new \Phalcon\DI\FactoryDefault\CLI(); //配置模型元数据 $di->set('modelsMetadata', function () { return new Phalcon\Mvc\Model\Metadata\Memory(); }); //config 工具类 TConfig::instance()->setAll($config->toArray()); //加载DB负载均衡 BalanceDb::config($config->balanceDb->toArray()); foreach ($config->balanceDb->toArray() as $dbKey => $currentConfig) { if ($dbKey == 'default') { continue; } $keyWrite = 'db' . ucfirst($dbKey); $keyRead = $keyWrite . '_read'; $dbWriteConfig = $currentConfig['write']['db'];
<?php define('APP_START_TIME', microtime(true)); define('DOCROOT', dirname(__FILE__) . '/'); require_once DOCROOT . '../vendor/autoload.php'; class_alias('Rj\\TestView124', 'TestView'); $loader = new Phalcon\Loader(); $loader->registerDirs([__DIR__ . '/models', __DIR__ . '/classes', __DIR__ . '/controllers', __DIR__ . '/forms', __DIR__ . '/tasks']); $loader->register(); if (!isset($di)) { $di = new Phalcon\DI\FactoryDefault\CLI(); } \Phalcon\Mvc\Model::setup(['notNullValidations' => false]); $di->set('config', function () { /** @var Phalcon\Config $default */ $config = (require __DIR__ . '/config/config.default.php'); $config->merge(require __DIR__ . '/config/config.php'); return $config; }, true); function logException(Exception $e, $mail = true) { Logger::messages()->exception($e); if (Config::instance()->production) { if (Phalcon\DI::getDefault()->has('request')) { /** @var \Phalcon\Http\Request $request */ $request = Phalcon\DI::getDefault()->getShared('request'); $message = sprintf("%s %s: %s\n" . "UserAgent: %s\n" . "HTTP Referer: %s\n" . "%s URL: %s://%s\n" . "LoggedUser: %s\n" . "%s", date('Y-m-d H:i:s'), get_class($e), $e->getMessage(), $request->getUserAgent(), urldecode($request->getHTTPReferer()), $request->getClientAddress(), $request->getScheme(), $request->getHttpHost() . urldecode(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '<!undefined>'), $e->getTraceAsString()); } else { $message = date('Y-m-d H:i:s') . ' ' . $e->getMessage() . "\n" . "There is no request object\n" . $e->getTraceAsString(); } switch (true) {
if (isset($_SERVER["APP_ENV"]) && $_SERVER["APP_ENV"] == 'dev') { ini_set("display_errors", 1); error_reporting(E_ALL); } try { //Register an autoloader $loader = new \Phalcon\Loader(); // Twig require_once __DIR__ . '/../vendor/autoload.php'; $loader->registerNamespaces(['Notnull\\DailyNews\\Controllers' => __DIR__ . '/../app/controllers/', 'Notnull\\DailyNews\\Models' => __DIR__ . '/../app/models/', 'Notnull\\DailyNews\\Tasks' => __DIR__ . '/../app/tasks/']); $loader->register(); //Create a DI if (PHP_SAPI != 'cli') { $di = new \Phalcon\DI\FactoryDefault(); } else { $di = new \Phalcon\DI\FactoryDefault\CLI(); } //Setting MongoDB $di->set('mongo', function () { $mongo = new MongoClient(); return $mongo->selectDb("ppro"); }, true); //Registering the collectionManager service $di->set('collectionManager', function () { return new Phalcon\Mvc\Collection\Manager(); }, true); //Setting Router if (PHP_SAPI == 'cli') { $di->set('dispatcher', function () { $dispatcher = new Phalcon\CLI\Dispatcher(); $dispatcher->setDefaultNamespace('Notnull\\DailyNews\\Tasks');
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.'); }