Exemplo n.º 1
0
 protected function _prepareDI(&$trace)
 {
     Phalcon\DI::reset();
     $eventsManager = new Phalcon\Events\Manager();
     $eventsManager->attach('model', function ($event, $model) use(&$trace) {
         if (!isset($trace[$event->getType()][get_class($model)])) {
             $trace[$event->getType()][get_class($model)] = 1;
         } else {
             $trace[$event->getType()][get_class($model)]++;
         }
     });
     $di = new Phalcon\DI();
     $di->set('modelsManager', function () use($eventsManager) {
         $modelsManager = new Phalcon\Mvc\Model\Manager();
         $modelsManager->setEventsManager($eventsManager);
         return $modelsManager;
     }, true);
     $di->set('modelsMetadata', function () {
         return new Phalcon\Mvc\Model\Metadata\Memory();
     }, true);
     $di->set('db', function () {
         require 'unit-tests/config.db.php';
         //return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
         return new Twm\Db\Adapter\Pdo\Mssql($configMssql);
     }, true);
 }
Exemplo n.º 2
0
 /**
  * Initializes the database and metadata adapter
  */
 public function init()
 {
     $di = $this->getDi();
     $eventsManager = $this->getEventsManager();
     $config = $this->_config;
     $adapter = $this->_getDatabaseAdapter($config->database->adapter);
     if (!$adapter) {
         throw new \Engine\Exception("Database adapter '{$config->database->adapter}' not exists!");
     }
     $connection = new $adapter(["host" => $this->_config->database->host, "username" => $this->_config->database->username, "password" => $this->_config->database->password, "dbname" => $this->_config->database->dbname, "options" => [\PDO::ATTR_EMULATE_PREPARES => false]]);
     if (!$config->application->debug && $config->database->useCache) {
         if ($di->offsetExists('modelsCache')) {
             //$connection->setCache($di->get('modelsCache'));
         }
     }
     if ($config->application->debug) {
         // Attach logger & profiler
         $logger = new \Phalcon\Logger\Adapter\File($config->application->logger->path . "db.log");
         $profiler = new \Phalcon\Db\Profiler();
         $eventsManager->attach('db', function ($event, $connection) use($logger, $profiler) {
             if ($event->getType() == 'beforeQuery') {
                 $statement = $connection->getSQLStatement();
                 $logger->log($statement, \Phalcon\Logger::INFO);
                 $profiler->startProfile($statement);
             }
             if ($event->getType() == 'afterQuery') {
                 //Stop the active profile
                 $profiler->stopProfile();
             }
         });
         if ($this->_config->application->profiler && $di->has('profiler')) {
             $di->get('profiler')->setDbProfiler($profiler);
         }
         $connection->setEventsManager($eventsManager);
     }
     $di->set('db', $connection);
     if (isset($config->database->useAnnotations) && $config->database->useAnnotations) {
         $di->set('modelsManager', function () use($config, $eventsManager) {
             $modelsManager = new \Phalcon\Mvc\Model\Manager();
             $modelsManager->setEventsManager($eventsManager);
             //Attach a listener to models-manager
             $eventsManager->attach('modelsManager', new \Engine\Model\AnnotationsInitializer());
             return $modelsManager;
         }, true);
     }
     /**
      * If the configuration specify the use of metadata adapter use it or use memory otherwise
      */
     $service = $this;
     $di->set('modelsMetadata', function () use($config, $service) {
         if ((!$config->application->debug || $config->application->useCachingInDebugMode) && isset($config->metadata)) {
             $metaDataConfig = $config->metadata;
             $metadataAdapter = $service->_getMetaDataAdapter($metaDataConfig->adapter);
             if (!$metadataAdapter) {
                 throw new \Engine\Exception("MetaData adapter '{$metaDataConfig->adapter}' not exists!");
             }
             $metaData = new $metadataAdapter($config->metadata->toArray());
         } else {
             $metaData = new \Phalcon\Mvc\Model\MetaData\Memory();
         }
         if (isset($config->database->useAnnotations) && $config->database->useAnnotations) {
             $metaData->setStrategy(new \Engine\Model\AnnotationsMetaDataInitializer());
         }
         return $metaData;
     }, true);
 }
$di->setShared(AppServices::TOKEN_PARSER, function () use($di, $config) {
    return new \PhalconRest\Auth\TokenParser\JWT($config->authentication->secret, \PhalconRest\Auth\TokenParser\JWT::ALGORITHM_HS256);
});
/**
 * @description Phalcon - AuthManager
 */
$di->setShared(AppServices::AUTH_MANAGER, function () use($di, $config) {
    $authManager = new \PhalconRest\Auth\Manager($config->authentication->expirationTime);
    $authManager->registerAccountType(App\Auth\UsernameAccountType::NAME, new \App\Auth\UsernameAccountType());
    return $authManager;
});
/**
 * @description Phalcon - \Phalcon\Mvc\Model\Manager
 */
$di->setShared(AppServices::MODELS_MANAGER, function () use($di) {
    $modelsManager = new \Phalcon\Mvc\Model\Manager();
    return $modelsManager->setEventsManager($di->get(AppServices::EVENTS_MANAGER));
});
/**
 * @description PhalconRest - \League\Fractal\Manager
 */
$di->setShared(AppServices::FRACTAL_MANAGER, function () {
    $fractal = new \League\Fractal\Manager();
    $fractal->setSerializer(new \App\Fractal\CustomSerializer());
    return $fractal;
});
/**
 * @description App - \Library\App\Services\UserService
 */
$di->setShared(AppServices::USER_SERVICE, function () {
    return new \App\Services\UserService();
            $module->registerAutoloaders($application->getDI());
        }
        /**
         * @var $dispatcher \Phalcon\Mvc\Dispatcher
         */
        $dispatcher->setModuleName('frontend');
        $dispatcher->setParam('error', $exception);
        $dispatcher->forward(array('namespace' => 'Frontend\\Controller', 'module' => 'frontend', 'controller' => 'error', 'action' => 'index'));
        return false;
    });
    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setEventsManager($evManager);
    return $dispatcher;
}), 'modelsManager' => array('class' => function ($application) {
    $eventsManager = $application->getDI()->get('eventsManager');
    $modelsManager = new \Phalcon\Mvc\Model\Manager();
    $modelsManager->setEventsManager($eventsManager);
    $eventsManager->attach('modelsManager', new \Engine\Db\Model\Annotations\Initializer());
    return $modelsManager;
}), 'router' => array('class' => function ($application) {
    $router = new Router(false);
    $router->add('/', array('module' => 'frontend', 'controller' => 'index', 'action' => 'index'))->setName('default');
    foreach ($application->getModules() as $key => $module) {
        $router->add('/' . $key . '/:params', array('module' => $key, 'controller' => 'index', 'action' => 'index', 'params' => 1))->setName($key);
        $router->add('/' . $key . '/:controller/:params', array('module' => $key, 'controller' => 1, 'action' => 'index', 'params' => 2));
        $router->add('/' . $key . '/:controller/:action/:params', array('module' => $key, 'controller' => 1, 'action' => 2, 'params' => 3));
    }
    $router->add('/catalog/category/{id:([0-9]{1,11})}/:params', array('module' => 'catalog', 'controller' => 'index', 'action' => 'category', 'id' => 1))->setName('catalog-category');
    $router->add('/catalog/product/{id:([0-9]{1,11})}/:params', array('module' => 'catalog', 'controller' => 'product', 'action' => 'index', 'id' => 1))->setName('catalog-product-view');
    $router->add('/oauth/login/{id:([a-z]{1,20})}/:params', array('module' => 'oauth', 'controller' => 'index', 'action' => 'index'))->setName('oauth-index-index');
    $router->add('/api/users/{id:([0-9]{1,32})}/:params', array('module' => 'api', 'controller' => 'users', 'action' => 'get'));