示例#1
0
 /**
  * Create view instance.
  * If no events manager provided - events would not be attached.
  *
  * @param DIBehaviour  $di             DI.
  * @param Config       $config         Configuration.
  * @param string|null  $viewsDirectory Views directory location.
  * @param Manager|null $em             Events manager.
  *
  * @return View
  */
 public static function factory($di, $config, $viewsDirectory = null, $em = null)
 {
     $view = new View();
     $volt = new Volt($view, $di);
     $volt->setOptions(["compiledPath" => $config->application->view->compiledPath, "compiledExtension" => $config->application->view->compiledExtension, 'compiledSeparator' => $config->application->view->compiledSeparator, 'compileAlways' => $config->application->debug && $config->application->view->compileAlways]);
     $compiler = $volt->getCompiler();
     $compiler->addExtension(new Extension());
     $view->registerEngines([".volt" => $volt])->setRenderLevel(View::LEVEL_ACTION_VIEW)->restoreViewDir();
     if (!$viewsDirectory) {
         $view->setViewsDir($viewsDirectory);
     }
     // Attach a listener for type "view".
     if ($em) {
         $em->attach("view", function ($event, $view) use($di, $config) {
             if ($config->application->profiler && $di->has('profiler')) {
                 if ($event->getType() == 'beforeRender') {
                     $di->get('profiler')->start();
                 }
                 if ($event->getType() == 'afterRender') {
                     $di->get('profiler')->stop($view->getActiveRenderPath(), 'view');
                 }
             }
             if ($event->getType() == 'notFoundView') {
                 throw new Exception('View not found - "' . $view->getActiveRenderPath() . '"');
             }
         });
         $view->setEventsManager($em);
     }
     return $view;
 }
 /**
  * Init database.
  *
  * @param DIBehaviour|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;
 }