public function testModelsMysql() { require 'unit-tests/config.db.php'; if (empty($configMysql)) { $this->markTestSkipped('Test skipped'); return; } $di = $this->_getDI(); $tracer = array(); $di->set('db', function () use(&$tracer) { require 'unit-tests/config.db.php'; $eventsManager = new Phalcon\Events\Manager(); $connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); $eventsManager->attach('db', function ($event, $connection) use(&$tracer) { if ($event->getType() == 'beforeQuery') { $tracer[] = $connection->getSqlStatement(); } }); $connection->setEventsManager($eventsManager); return $connection; }, true); $this->_executeTestsNormal($di, $tracer); $tracer = array(); $this->_executeTestsRenamed($di, $tracer); }
public function initPersistentDB($di) { // Setup the database service $di->set('db', function () { $eventsManager = new EventsManager(); $logger = new FileLogger(__DIR__ . '/' . date('Y-m-d') . '.sql.log'); $eventsManager->attach('db', function ($event, $connection) use($logger) { if ($event->getType() == 'beforeQuery') { $logger->info($connection->getSQLStatement()); } }); $db = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => "127.0.0.1", "username" => "root", "password" => "123456", "dbname" => "rookie", "charset" => "utf8", "persistent" => true)); $db->setEventsManager($eventsManager); return $db; }); }
public static function initDb() { $config = \Phalcon\DI::getDefault()->get('config'); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($config->database->toArray()); if (getenv('APPLICATION_ENV') == 'devel') { $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach('db', function ($event, $connection) { if ($event->getType() == 'beforeQuery') { //Start a profile with the active connection error_log($connection->getSQLStatement() . "\n" . json_encode($connection->getSQLVariables())); } }); $connection->setEventsManager($eventsManager); } return $connection; }
/** * Mount the module specific routes before the module is loaded. * Add ModuleRoutes Group and annotated controllers for parsing their routing information. * * @param \Phalcon\DiInterface $di */ public static function initRoutes(DiInterface $di) { $loader = new Loader(); $loader->registerNamespaces(['App\\Modules\\Backend\\Controllers' => __DIR__ . '/Controllers/', 'App\\Modules\\Backend\\Controllers\\API' => __DIR__ . '/Controllers/api/', 'App\\Modules\\Backend\\Models' => __DIR__ . '/Models/', 'App\\Modules\\Backend\\Library' => __DIR__ . '/Lib/', 'App\\Modules\\Frontend\\Controllers' => __DIR__ . '/../Frontend/Controllers/', 'App\\Modules\\Frontend\\Models' => __DIR__ . '/../Frontend/Models/'], TRUE)->register(); /** * Read application wide and module only configurations */ $appConfig = $di->get('config'); $moduleConfig = (include __DIR__ . '/config/config.php'); $di->setShared('moduleConfig', $moduleConfig); /** * The URL component is used to generate all kind of urls in the application */ $di->setShared('url', function () use($appConfig) { $url = new UrlResolver(); $url->setBaseUri($appConfig->application->baseUri); return $url; }); $di->setShared('request', function () use($appConfig) { return new \Phalcon\Http\Request(); }); /** * Read configuration */ include __DIR__ . "/../../config/env/" . $appConfig->application->environment . ".php"; $database = $di->getConfig()->application->site . $di->get('request')->getQuery("countryCode"); /** * Module specific database connection */ $di->set('db', function () use($config, $database) { $eventsManager = new \Phalcon\Events\Manager(); //Create a database listener $dbListener = new MyDBListener(); //Listen all the database events $eventsManager->attach('db', $dbListener); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(['host' => $config->{$database}->host, 'username' => $config->{$database}->username, 'password' => $config->{$database}->password, 'dbname' => $config->{$database}->dbname, 'options' => array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")]); //Assign the eventsManager to the db adapter instance $connection->setEventsManager($eventsManager); return $connection; }); /** * Simple database connection to localhost */ $di->set('mongo', function () use($config, $database) { $mongo = new \MongoClient(); return $mongo->selectDb($config->{$database}->dbname); }, true); $di->set('collectionManager', function () { return new \Phalcon\Mvc\Collection\Manager(); }, true); /** * Include composer autoloader */ require __DIR__ . "/../../../vendor/autoload.php"; /** * Module specific dispatcher */ $di->set('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('App\\Modules\\Backend\\Controllers\\'); return $dispatcher; }); $di->set('utils', function () { require __DIR__ . "/../../Common/Lib/Application/Plugins/Utils.php"; $utils = new Utils(); return $utils; }); /** * If our request contains a body, it has to be valid JSON. This parses the * body into a standard Object and makes that available from the DI. If this service * is called from a function, and the request body is not valid JSON or is empty, * the program will throw an Exception. */ $di->setShared('requestBody', function () { parse_str(file_get_contents("php://input"), $in); // JSON body could not be parsed, throw exception if ($in === null) { throw new HTTPException('There was a problem understanding the data sent to the server by the application.', 409, array('dev' => 'The JSON body sent to the server was unable to be parsed.', 'internalCode' => 'REQ1000', 'more' => '')); } return $in; }); /** * This means we can create listeners that run when an event is triggered. */ $di->setShared('modelsManager', function () use($di, $config, $database) { $eventsManager = new \Phalcon\Events\Manager(); $customModelsManager = new CustomModelsManager(); /** * Attach an anonymous function as a listener for "model" events */ $eventsManager->attach('model', $customModelsManager); /** * Setting a default EventsManager */ $customModelsManager->setEventsManager($eventsManager); return $customModelsManager; }); }
}); $di->set('dispatcher', function () use($di) { $dispatcher = new Phalcon\Mvc\Dispatcher(); return $dispatcher; }); $di->set('hash', function () { $hash = new \Phalcon\Security(); //Set the password hashing factor to 12 rounds $hash->setWorkFactor(12); return $hash; }, true); $di->setShared('db', function () use($di, $config) { // Events Manager para la base de datos $eventsManager = new \Phalcon\Events\Manager(); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($config->database->toArray()); $connection->setEventsManager($eventsManager); return $connection; }); $di->set('modelsManager', function () { return new \Phalcon\Mvc\Model\Manager(); }); $di->set('logger', function () { // Archivo de log return new \Phalcon\Logger\Adapter\File("../app/logs/debug.log"); }); $urlManager = new \Silar\Misc\UrlManager($config); $di->set('urlManager', $urlManager); $di->set('flashSession', function () { $flash = new \Phalcon\Flash\Session(array('error' => 'alert alert-danger text-center', 'success' => 'alert alert-success text-center', 'notice' => 'alert alert-info text-center', 'warning' => 'alert alert-warning text-center')); return $flash; });
/** * Set the database service * @return void */ protected function db() { $config = $this->_config; $profiler = $this->profiler(); //@todo get this sodding profiler working $this->_di->set('db', function () use($config, $profiler) { $eventsManager = new \Phalcon\Events\Manager(); // Listen to all database events $eventsManager->attach('db', function ($event, $connection) use($profiler) { /*$profiler = new \Phalcon\Db\Profiler(); //var_dump($profiler); exit; if ($event->getType() == 'beforeQuery') { $profiler->startProfile($connection->getSQLStatement()); } if ($event->getType() == 'afterQuery') { $profiler->stopProfile(); }*/ }); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "options" => array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'))); $connection->setEventsManager($eventsManager); return $connection; }); }
use App\Constants\Services as AppServices; $di = new \PhalconRest\DI\FactoryDefault(); /** * @description Phalcon - \Phalcon\Config */ $di->setShared(AppServices::CONFIG, function () use($config) { return $config; }); /** * @description Phalcon - \Phalcon\Db\Adapter\Pdo\Mysql */ $di->set(AppServices::DB, function () use($config, $di) { $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name)); //Assign the eventsManager to the db adapter instance $connection->setEventsManager($di->get(AppServices::EVENTS_MANAGER)); return $connection; }); /** * @description Phalcon - \Phalcon\Mvc\Url */ $di->set(AppServices::URL, function () use($config) { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri($config->application->baseUri); return $url; }); /** * @description Phalcon - \Phalcon\Mvc\View\Simple */ $di->set(AppServices::VIEW, function () use($config) { $view = new Phalcon\Mvc\View\Simple();
/** * Database Object, conexion primaria a la base de datos * @return DI object */ private function setDb() { $config = $this->config; $di = $this->di; $di->setShared('db', function () use($config) { // Events Manager para la base de datos $eventsManager = new \Phalcon\Events\Manager(); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($config->database->toArray()); $connection->setEventsManager($eventsManager); return $connection; }); }
/** * Mount the module specific routes before the module is loaded. * Add ModuleRoutes Group and annotated controllers for parsing their routing information. * * @param \Phalcon\DiInterface $di */ public static function initRoutes(DiInterface $di) { $loader = new Loader(); $loader->registerNamespaces(['App\\Modules\\Admin' => __DIR__, 'App\\Modules\\Admin\\Models' => __DIR__ . '/Models/', 'App\\Modules\\Admin\\Controllers' => __DIR__ . '/Controllers/', 'App\\Modules\\Admin\\Controllers\\API' => __DIR__ . '/Controllers/api/'], TRUE)->register(); /** * Read application wide and module only configurations */ $appConfig = $di->get('config'); $moduleConfig = (include __DIR__ . '/config/config.php'); $di->set('moduleConfig', $moduleConfig); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($appConfig) { $url = new UrlResolver(); $url->setBaseUri($appConfig->application->baseUri); return $url; }); $di->setShared('request', function () use($appConfig) { return new \Phalcon\Http\Request(); }); /** * Read configuration */ include __DIR__ . "/../../config/env/" . $appConfig->application->environment . ".php"; $database = $di->getConfig()->application->site . $di->get('request')->getQuery("countryCode"); /** * Module specific database connection */ $di->set('dbMysql', function () use($config, $database) { $eventsManager = new \Phalcon\Events\Manager(); $logger = new FileLogger(__DIR__ . "/../../Common/logs/admin/debug.log"); //Listen all the database events $eventsManager->attach('dbMysql', function ($event, $connection) use($logger) { if ($event->getType() == 'beforeQuery') { $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO); } }); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(['host' => $config->{$database}->host, 'username' => $config->{$database}->username, 'password' => $config->{$database}->password, 'dbname' => $config->{$database}->dbname, 'options' => array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")]); //Assign the eventsManager to the db adapter instance $connection->setEventsManager($eventsManager); return $connection; }); /** * Module specific dispatcher */ $di->setShared('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('App\\Modules\\Admin\\Controllers\\'); return $dispatcher; }); $di->set('utils', function () { require __DIR__ . "/../../Common/Lib/Application/Plugins/Utils.php"; $utils = new Utils(); return $utils; }); /** * If our request contains a body, it has to be valid JSON. This parses the * body into a standard Object and makes that available from the DI. If this service * is called from a function, and the request body is not valid JSON or is empty, * the program will throw an Exception. */ $di->setShared('requestBody', function () { parse_str(file_get_contents("php://input"), $in); // JSON body could not be parsed, throw exception if ($in === null) { throw new HTTPException('There was a problem understanding the data sent to the server by the application.', 409, array('dev' => 'The JSON body sent to the server was unable to be parsed.', 'internalCode' => 'REQ1000', 'more' => '')); } return $in; }); }
protected function _getDI() { Phalcon\DI::reset(); $di = new Phalcon\DI(); $di->set('modelsManager', function () { return new Phalcon\Mvc\Model\Manager(); }); $di->set('modelsMetadata', function () { return new Phalcon\Mvc\Model\Metadata\Memory(); }); $di->set('db', function () { require 'unit-tests/config.db.php'; //return new Twm\Db\Adapter\Pdo\Mssql($configMssql); $connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); $eventsManager = new Phalcon\Events\Manager(); //Listen all the database events $eventsManager->attach('db', function ($event, $connection) { if ($event->getType() == 'beforeQuery') { echo $connection->getSQLStatement(); } }); //Assign the eventsManager to the db adapter instance $connection->setEventsManager($eventsManager); return $connection; }); return $di; }