public function testRegisterRoutes() { $this->testable->registerRoutes(); /* @var Router $router */ $router = $this->testable->getRouter(); $this->assertFalse($this->di->has('router')); $this->assertNotEmpty($router->getRoutes()); $this->assertTrue(is_array($router->getRoutes())); $this->assertCount(8, $router->getRoutes()); $this->assertInstanceOf('Phalcon\\Mvc\\Router\\Route', $router->getRouteByName('fifi')); $this->assertInstanceOf('Phalcon\\Mvc\\Router\\Route', $router->getRouteByName('get-all')); }
/** * @param Response $response * * @return mixed * @throws Exception */ public function modifyResponse($response) { $request = $this->di['request']; $config = $this->config; if (!$this->isEnabled()) { return $response; } if ($this->shouldCollect('config', false) && $this->di->has('config')) { try { $config_data = $this->di['config']->toArray(); $protect = $config->options->config->get('protect'); $configCollector = new ConfigCollector($config_data); $configCollector->setProtect($protect); $this->addCollector($configCollector); } catch (\Exception $e) { $this->addException(new Exception('Cannot add ConfigCollector to Phalcon Debugbar: ' . $e->getMessage(), $e->getCode(), $e)); } } if ($this->shouldCollect('session') && $this->di->has('session')) { try { $this->addCollector(new SessionCollector($this->di['session'])); } catch (\Exception $e) { $this->addException(new Exception('Cannot add SessionCollector to Phalcon Debugbar: ' . $e->getMessage(), $e->getCode(), $e)); } } if ($this->shouldCollect('phalcon_request', true) and !$this->hasCollector('request')) { try { $this->addCollector(new PhalconRequestCollector($this->di['request'], $response, $this->di)); } catch (\Exception $e) { $this->addException(new Exception('Cannot add PhalconRequestCollector to Phalcon Debugbar: ' . $e->getMessage(), $e->getCode(), $e)); } } if ($this->hasCollector('pdo')) { /** @var Profiler $profiler */ $profiler = $this->getCollector('pdo')->getProfiler(); $profiler->handleFailed(); } if ($this->isDebugbarRequest) { // Notice: All Collectors must be added before check if is debugbar request. return $response; } try { if ($this->isRedirection($response)) { $this->stackData(); } elseif ($this->isJsonRequest($request) && $this->config->get('capture_ajax', true)) { $this->sendDataInHeaders(true); } elseif ($content_type = $response->getHeaders()->get('Content-Type') and strpos($response->getHeaders()->get('Content-Type'), 'html') === false) { $this->collect(); } elseif ($this->config->get('inject', true)) { $response->setHeader('Phalcon-Debugbar', 'on'); $this->injectDebugbar($response); } } catch (\Exception $e) { $this->addException($e); } // Stop further rendering (on subrequests etc) $this->disable(); return $response; }
public function __construct(DI $di, $aggregate = true, $formatter = 'line') { $this->_di = $di; $this->_debugbar = $this->_di['debugbar']; $this->_formatter = strtolower($formatter); if ($di->has('log') && ($log = $di->get('log'))) { $debugbar_loger = new Debugbar($di['debugbar']); if ($log instanceof Adapter) { $di->remove('log'); $multiple = new Multiple(); $multiple->push(clone $log); $multiple->push($debugbar_loger); /** @var DI\Service $service */ $di->set('log', $multiple); } elseif ($log instanceof Multiple) { $log->push($debugbar_loger); } $this->_aggregate = $this->isAggregate($aggregate); } }
/** * {@inheritdoc} */ public function has($id) { return $this->di->has($id); }
/** * Init database. * * @param DI $di Dependency Injection. * @param Config $config Config object. * @param EventsManager $eventsManager Event manager. * * @return Pdo */ protected function _initDb($di, $config, $eventsManager) { $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config->db->mysql->adapter; /** @var Pdo $connection */ $connection = new $adapter(['host' => $config->db->mysql->host, 'port' => $config->db->mysql->port, 'username' => $config->db->mysql->username, 'password' => $config->db->mysql->password, 'dbname' => $config->db->mysql->dbname]); $isProfiler = $config->global->profiler; if ($isProfiler) { // Attach logger & profiler. $profiler = null; if ($isProfiler) { $profiler = new PhDbProfiler(); } $eventsManager->attach('db', function ($event, $connection) use($profiler) { if ($event->getType() == 'beforeQuery') { $statement = $connection->getSQLStatement(); if ($profiler) { $profiler->startProfile($statement); } } if ($event->getType() == 'afterQuery') { // Stop the active profile. if ($profiler) { $profiler->stopProfile(); } } }); if ($profiler && $di->has('profiler')) { $di->get('profiler')->setDbProfiler($profiler); } $connection->setEventsManager($eventsManager); } $di->set('db', $connection); /** * Add db service connect to five.vn database */ $di->set('dbfive', function () use($config) { $fiveAdapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config->db->dbfive->adapter; return new $fiveAdapter(['host' => $config->db->dbfive->host, 'port' => $config->db->dbfive->port, 'username' => $config->db->dbfive->username, 'password' => $config->db->dbfive->password, 'dbname' => $config->db->dbfive->dbname]); }); $di->set('modelsManager', function () use($config, $eventsManager) { $modelsManager = new PhModelsManager(); $modelsManager->setEventsManager($eventsManager); // Attach a listener to models-manager $eventsManager->attach('modelsManager', new ModelAnnotationsInitializer()); return $modelsManager; }, true); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise. */ $di->set('modelsMetadata', function () use($config) { if (ENV == ENV_PRODUCTION && isset($config->global->metadata)) { $metaDataConfig = $config->global->metadata; $metadataAdapter = '\\Phalcon\\Mvc\\Model\\Metadata\\' . $metaDataConfig->adapter; $metaData = new $metadataAdapter($config->global->metadata->toArray()); } else { $metaData = new \Phalcon\Mvc\Model\MetaData\Memory(); } $metaData->setStrategy(new PhStrategyAnnotations()); return $metaData; }, true); return $connection; }
/** * Init database. * * @param DI $di Dependency Injection. * @param Config $config Config object. * @param EventsManager $eventsManager Event manager. * * @return Pdo */ protected function _initDatabase($di, $config, $eventsManager) { if (!$config->installed) { return; } $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config->database->adapter; /** @var Pdo $connection */ $connection = new $adapter(["host" => $config->database->host, "port" => $config->database->port, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname]); $isDebug = $config->application->debug; $isProfiler = $config->application->profiler; if ($isDebug || $isProfiler) { // Attach logger & profiler. $logger = null; $profiler = null; if ($isDebug) { $logger = new File($config->application->logger->path . "db.log"); } if ($isProfiler) { $profiler = new DatabaseProfiler(); } $eventsManager->attach('db', function ($event, $connection) use($logger, $profiler) { if ($event->getType() == 'beforeQuery') { $statement = $connection->getSQLStatement(); if ($logger) { $logger->log($statement, Logger::INFO); } if ($profiler) { $profiler->startProfile($statement); } } if ($event->getType() == 'afterQuery') { // Stop the active profile. if ($profiler) { $profiler->stopProfile(); } } }); if ($profiler && $di->has('profiler')) { $di->get('profiler')->setDbProfiler($profiler); } $connection->setEventsManager($eventsManager); } $di->set('db', $connection); $di->set('modelsManager', function () use($config, $eventsManager) { $modelsManager = new ModelsManager(); $modelsManager->setEventsManager($eventsManager); // Attach a listener to models-manager $eventsManager->attach('modelsManager', new ModelAnnotationsInitializer()); return $modelsManager; }, true); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise. */ $di->set('modelsMetadata', function () use($config) { if (!$config->application->debug && isset($config->application->metadata)) { $metaDataConfig = $config->application->metadata; $metadataAdapter = '\\Phalcon\\Mvc\\Model\\Metadata\\' . $metaDataConfig->adapter; $metaData = new $metadataAdapter($config->application->metadata->toArray()); } else { $metaData = new \Phalcon\Mvc\Model\MetaData\Memory(); } $metaData->setStrategy(new StrategyAnnotations()); return $metaData; }, true); return $connection; }