/**
  * @return self
  * @throws Phalcon\Exception
  */
 public function init()
 {
     /** @var Phalcon\DI $di */
     $di = $this->getDI();
     $di->setShared('db', (new Orm\Database\Adapter($di))->resolve());
     $di->setShared('modelsManager', function () use($di) {
         $eventManager = $di->get('eventsManager');
         $modelsManager = new ModelsManager();
         $modelsManager->setEventsManager($eventManager);
         $eventManager->attach('modelsManager', new Orm\Annotation\Initializer());
         return $modelsManager;
     });
     $di->setShared('modelsMetadata', function () use($di) {
         $metadataAdapter = (new Orm\Metadata\Adapter($di))->resolve();
         $metadataAdapter->setStrategy(new Orm\Annotation\MetaDataInitializer());
         return $metadataAdapter;
     });
     return $this;
 }
Exemple #2
0
    $session = new $sessionAdapter($di->get('config')->application->session->options->toArray());
    $session->start();
    return $session;
}, true);
/**
 * This service controls the initialization of models, keeping record of relations
 * between the different models of the application.
 */
$di->setShared('collectionManager', function () use($eventsManager) {
    $collectionManager = new CollectionManager();
    $collectionManager->setEventsManager($eventsManager);
    return $collectionManager;
});
$di->setShared('modelsManager', function () use($eventsManager) {
    $modelsManager = new ModelsManager();
    $modelsManager->setEventsManager($eventsManager);
    return $modelsManager;
});
// Set the views cache service
$di->set('viewCache', function () use($di) {
    $config = $di->get('config');
    if ($config->application->debug) {
        return new MemoryBackend(new FrontendNone());
    } else {
        // Cache data for one day by default
        $frontCache = new FrontendOutput(['lifetime' => $config->cache->lifetime]);
        return new FileCache($frontCache, ['cacheDir' => $config->cache->cacheDir, 'prefix' => $config->cache->prefix]);
    }
});
//  Setting up the view component
$di->set('view', function () use($di, $eventsManager) {
Exemple #3
0
 /**
  * Initialize the Database connection.
  *
  * Database connection is created based in the parameters defined in the configuration file.
  *
  * @param DiInterface   $di     Dependency Injector
  * @param Config        $config App config
  * @param EventsManager $em     Events Manager
  *
  * @return void
  */
 protected function initDatabase(DiInterface $di, Config $config, EventsManager $em)
 {
     $di->setShared('db', function () use($config, $em, $di) {
         $config = $config->get('database')->toArray();
         $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config['adapter'];
         unset($config['adapter']);
         /** @var \Phalcon\Db\Adapter\Pdo $connection */
         $connection = new $adapter($config);
         // Listen all the database events
         $em->attach('db', function ($event, $connection) use($di) {
             /**
              * @var \Phalcon\Events\Event $event
              * @var \Phalcon\Db\AdapterInterface $connection
              * @var \Phalcon\DiInterface $di
              */
             if ($event->getType() == 'beforeQuery') {
                 $variables = $connection->getSQLVariables();
                 $string = $connection->getSQLStatement();
                 if ($variables) {
                     $string .= ' [' . join(',', $variables) . ']';
                 }
                 // To disable logging change logLevel in config
                 $di->get('logger', ['db.log'])->debug($string);
             }
         });
         // Assign the eventsManager to the db adapter instance
         $connection->setEventsManager($em);
         return $connection;
     });
     $di->setShared('modelsManager', function () use($em) {
         $modelsManager = new ModelsManager();
         $modelsManager->setEventsManager($em);
         return $modelsManager;
     });
     $di->setShared('modelsMetadata', function () use($config, $em) {
         $config = $config->get('metadata')->toArray();
         $adapter = '\\Phalcon\\Mvc\\Model\\Metadata\\' . $config['adapter'];
         unset($config['adapter']);
         $metaData = new $adapter($config);
         return $metaData;
     });
 }
 /**
  * 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)
 {
     $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]]);
     $connSlave = new $adapter(["host" => $config->dbSlave->host, "port" => $config->dbSlave->port, "username" => $config->dbSlave->username, "password" => $config->dbSlave->password, "dbname" => $config->dbSlave->dbname, "prefix" => $config->dbSlave->prefix, 'options' => [\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '" . $config->dbSlave->charset . "'", \PDO::ATTR_CASE => \PDO::CASE_LOWER, \PDO::ATTR_PERSISTENT => true]]);
     $isDebug = $config->debug;
     $isProfiler = $config->profiler;
     if ($isDebug || $isProfiler) {
         // Attach logger & profiler.
         $logger = null;
         $profiler = null;
         if ($isDebug) {
             $logger = write_log("database", "");
         }
         if ($isProfiler) {
             $profiler = new DatabaseProfiler();
             $di->set('profiler', $profiler);
         }
         $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();
                 }
             }
         });
         $connMaster->setEventsManager($eventsManager);
         $connSlave->setEventsManager($eventsManager);
     }
     $di->set('dbMaster', $connMaster);
     $di->set('db', $connSlave);
     $di->set('modelsManager', function () use($config, $eventsManager) {
         $modelsManager = new ModelsManager();
         $modelsManager->setEventsManager($eventsManager);
         return $modelsManager;
     }, true);
     return $connMaster;
 }
 /**
  * Initializes the models manager
  */
 public function initModelsmanager($options = [])
 {
     $this->di->set('modelsManager', function () {
         $eventsManager = new PhEventsManager();
         $modelsManager = new PhModelsManager();
         $modelsManager->setEventsManager($eventsManager);
         //Attach a listener to models-manager
         $eventsManager->attach('modelsManager', new FlyAnnotationsInitializer());
         return $modelsManager;
     });
 }
Exemple #6
0
 /**
  * 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;
 }
 public function run(Api $api, DiInterface $di, Config $config)
 {
     /**
      * @description Config - \Phalcon\Config
      */
     $di->setShared(Services::CONFIG, $config);
     /**
      * @description Phalcon - \Phalcon\Db\Adapter\Pdo\Mysql
      */
     $di->set(Services::DB, function () use($config, $di) {
         $config = $config->get('database')->toArray();
         $adapter = $config['adapter'];
         unset($config['adapter']);
         $class = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter;
         $connection = new $class($config);
         // Assign the eventsManager to the db adapter instance
         $connection->setEventsManager($di->get(Services::EVENTS_MANAGER));
         return $connection;
     });
     /**
      * @description Phalcon - \Phalcon\Mvc\Url
      */
     $di->set(Services::URL, function () use($config) {
         $url = new UrlResolver();
         $url->setBaseUri($config->get('application')->baseUri);
         return $url;
     });
     /**
      * @description Phalcon - \Phalcon\Mvc\View\Simple
      */
     $di->set(Services::VIEW, function () use($config) {
         $view = new View();
         $view->setViewsDir($config->get('application')->viewsDir);
         return $view;
     });
     /**
      * @description Phalcon - EventsManager
      */
     $di->setShared(Services::EVENTS_MANAGER, function () use($di, $config) {
         return new EventsManager();
     });
     /**
      * @description Phalcon - TokenParsers
      */
     $di->setShared(Services::TOKEN_PARSER, function () use($di, $config) {
         return new JWTTokenParser($config->get('authentication')->secret, JWTTokenParser::ALGORITHM_HS256);
     });
     /**
      * @description Phalcon - AuthManager
      */
     $di->setShared(Services::AUTH_MANAGER, function () use($di, $config) {
         $authManager = new AuthManager($config->get('authentication')->expirationTime);
         $authManager->registerAccountType(UsernameAccountType::NAME, new UsernameAccountType());
         return $authManager;
     });
     /**
      * @description Phalcon - \Phalcon\Mvc\Model\Manager
      */
     $di->setShared(Services::MODELS_MANAGER, function () use($di) {
         $modelsManager = new ModelsManager();
         return $modelsManager->setEventsManager($di->get(Services::EVENTS_MANAGER));
     });
     /**
      * @description PhalconRest - \League\Fractal\Manager
      */
     $di->setShared(Services::FRACTAL_MANAGER, function () {
         $fractal = new FractalManager();
         $fractal->setSerializer(new CustomSerializer());
         return $fractal;
     });
     /**
      * @description PhalconRest - \PhalconRest\User\Service
      */
     $di->setShared(Services::USER_SERVICE, new UserService());
 }
Exemple #8
0
 /**
  * Setup database connection.
  *
  * @param array|null $connectionSettings Connection data.
  *
  * @return void
  */
 protected function _setupDatabase($connectionSettings = null)
 {
     if ($connectionSettings != null) {
         $this->config->database = new Config($connectionSettings);
     }
     $config = $this->config;
     $eventsManager = new EventsManager();
     $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config->database->adapter;
     $connection = new $adapter(["host" => $config->database->host, "port" => $config->database->port, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname]);
     $this->di->set('db', $connection);
     $this->di->set('modelsManager', function () use($config, $eventsManager) {
         $modelsManager = new ModelManager();
         $modelsManager->setEventsManager($eventsManager);
         //Attach a listener to models-manager
         $eventsManager->attach('modelsManager', new ModelAnnotationsInitializer());
         return $modelsManager;
     }, true);
 }
 /**
  * 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;
 }