/** * 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 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->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######'); $dispatcher->setActionName('hello'); $dispatcher->setParams(array('hello' => 'World', 'goodbye' => 'Everybody')); $dispatcher->dispatch(); $this->assertTrue($dispatcher->hasParam('hello')); $this->assertTrue($dispatcher->hasParam('goodbye')); $this->assertFalse($dispatcher->hasParam('salutations')); // 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'); } }
public function testConsoles() { $di = new Phalcon\DI\FactoryDefault\CLI(); $di->set('data', function () { return "data"; }); $console = new \Phalcon\CLI\Console(); $console->setDI($di); $dispatcher = $console->getDI()->getShared('dispatcher'); $console->handle(array()); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getActionName(), 'main'); $this->assertEquals($dispatcher->getParams(), array()); $this->assertEquals($dispatcher->getReturnedValue(), 'mainAction'); $console->handle(array('task' => 'echo')); $this->assertEquals($dispatcher->getTaskName(), 'echo'); $this->assertEquals($dispatcher->getActionName(), 'main'); $this->assertEquals($dispatcher->getParams(), array()); $this->assertEquals($dispatcher->getReturnedValue(), 'echoMainAction'); $console->handle(array('task' => 'main', 'action' => 'hello')); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getActionName(), 'hello'); $this->assertEquals($dispatcher->getParams(), array()); $this->assertEquals($dispatcher->getReturnedValue(), 'Hello !'); $console->handle(array('task' => 'main', 'action' => 'hello', 'World', '######')); $this->assertEquals($dispatcher->getTaskName(), 'main'); $this->assertEquals($dispatcher->getActionName(), 'hello'); $this->assertEquals($dispatcher->getParams(), array('World', '######')); $this->assertEquals($dispatcher->getReturnedValue(), 'Hello World######'); // testing module try { $console->handle(array('module' => 'devtools', 'task' => 'main', 'action' => 'hello', 'World', '######')); $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(), "Module 'devtools' isn't registered in the console container"); } // testing namespace try { $dispatcher->setDefaultNamespace('Dummy\\'); $console->handle(array('task' => 'main', 'action' => 'hello', 'World', '!')); $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'); } }
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')); }
/** * 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); }
$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 { return new Phalcon\Mvc\Model\Metadata\Memory(); } }); $console = new \Phalcon\CLI\Console(); $console->setDI($di);
} 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); $di->set('simpleView', function () use($di, $config) { $view = new \Phalcon\Mvc\View\Simple();
<?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 { //无效参数抛弃 } } } 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']; $di->set($keyWrite, function () use($dbWriteConfig) { return new Phalcon\Db\Adapter\Pdo\Mysql($dbWriteConfig); });
<?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) { // case $e instanceof PageNotFound:
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'); return $dispatcher; }); } //Setting Router if (PHP_SAPI != 'cli') {