/**
  * {@inheritdoc}
  */
 public function register(DiInterface $di)
 {
     $di->set(self::SERVICE_NAME, function () use($di) {
         $arrayConfig = (array) $di->get('config')->{self::SERVICE_NAME};
         $db = new \Phalcon\Db\Adapter\Pdo\Sqlite($arrayConfig);
         $db->setEventsManager($di->getShared('eventsManager'));
         return $db;
     }, true);
 }
Exemple #2
0
 /**
  * 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);
 }
    // $registry = new \Phalcon\Registry();
    $registry = $di->get('registry');
    $registry->dbCount = 0;
    // Listen all the database events
    $eventsManager->attach('db', function ($event, $connection) use($logger, $registry) {
        if ($event->getType() == 'beforeQuery') {
            $count = $registry->dbCount;
            $count++;
            $registry->dbCount = $count;
            // $logger->log($connection->getSQLStatement(), Logger::INFO);
        }
    });
    // $connection = new Connection($config['database']);
    $connection = new Phalcon\Db\Adapter\Pdo\Sqlite($config);
    // Assign the eventsManager to the db adapter instance
    $connection->setEventsManager($eventsManager);
    return $connection;
});
/**
 * 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 () {
    $in = file_get_contents('php://input');
    $in = json_decode($in, FALSE);
    // 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' => '358203478590723', 'more' => ''));
    }