Ejemplo n.º 1
0
 /**
  * Initializes the application instance.
  *
  * This method
  * starts the session,
  * call Slim constructor,
  * set the custom log writer (if is defined in config),
  * bootstraps the Doctrine,
  * bootstraps the Auth Manager,
  * creates the cache and rcache components,
  * sets the file storage,
  * adds midlewares,
  * instantiates the Route Manager and
  * includes the theme.php file of the active theme if the file exists.
  *
  *
  * If the application was previously initiated, this method returns the application in the first line.
  *
  * @return \MapasCulturais\App
  */
 public function init($config = array())
 {
     if ($this->_initiated) {
         return $this;
     }
     $this->_initiated = true;
     if ($config['slim.debug']) {
         error_reporting(E_ALL ^ E_STRICT);
     }
     session_start();
     $config['app.mode'] = key_exists('app.mode', $config) ? $config['app.mode'] : 'production';
     $this->_config = $config;
     $this->_config['path.layouts'] = APPLICATION_PATH . 'themes/active/layouts/';
     $this->_config['path.templates'] = APPLICATION_PATH . 'themes/active/views/';
     $this->_config['path.metadata_inputs'] = APPLICATION_PATH . 'themes/active/metadata-inputs/';
     if (!key_exists('app.sanitize_filename_function', $this->_config)) {
         $this->_config['app.sanitize_filename_function'] = null;
     }
     parent::__construct(array('log.level' => $config['slim.log.level'], 'log.enabled' => $config['slim.log.enabled'], 'debug' => $config['slim.debug'], 'templates.path' => $this->_config['path.templates'], 'view' => new View(), 'mode' => $this->_config['app.mode']));
     $config = $this->_config;
     // custom log writer
     if (isset($config['slim.log.writer']) && is_object($config['slim.log.writer']) && method_exists($config['slim.log.writer'], 'write')) {
         $log = $this->getLog();
         $log->setWriter($config['slim.log.writer']);
     }
     // =============== CACHE =============== //
     if (key_exists('app.cache', $config) && is_object($config['app.cache']) && is_subclass_of($config['app.cache'], '\\Doctrine\\Common\\Cache\\CacheProvider')) {
         $this->_cache = $config['app.cache'];
     } else {
         $this->_cache = new \Doctrine\Common\Cache\ArrayCache();
     }
     // creates runtime cache component
     $this->_rcache = new \Doctrine\Common\Cache\ArrayCache();
     // ===================================== //
     // ========== BOOTSTRAPING DOCTRINE ========== //
     // annotation driver
     $doctrine_config = Setup::createConfiguration($config['doctrine.isDev']);
     $classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
     $classLoader->register();
     $driver = new AnnotationDriver(new AnnotationReader());
     $driver->addPaths(array(__DIR__ . '/Entities/'));
     // tells the doctrine to ignore hook annotation.
     AnnotationReader::addGlobalIgnoredName('hook');
     // driver must be pdo_pgsql
     $config['doctrine.database']['driver'] = 'pdo_pgsql';
     // registering noop annotation autoloader - allow all annotations by default
     AnnotationRegistry::registerLoader('class_exists');
     $doctrine_config->setMetadataDriverImpl($driver);
     $proxy_dir = APPLICATION_PATH . 'lib/MapasCulturais/DoctrineProxies';
     $proxy_namespace = 'MapasCulturais\\DoctrineProxies';
     $doctrine_config->setProxyDir($proxy_dir);
     $doctrine_config->setProxyNamespace($proxy_namespace);
     \Doctrine\ORM\Proxy\Autoloader::register($proxy_dir, $proxy_namespace);
     /** DOCTRINE2 SPATIAL */
     $doctrine_config->addCustomStringFunction('st_asbinary', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STAsBinary');
     $doctrine_config->addCustomStringFunction('st_astext', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STAsText');
     $doctrine_config->addCustomNumericFunction('st_area', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STArea');
     $doctrine_config->addCustomStringFunction('st_centroid', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STCentroid');
     $doctrine_config->addCustomStringFunction('st_closestpoint', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STClosestPoint');
     $doctrine_config->addCustomNumericFunction('st_contains', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STContains');
     $doctrine_config->addCustomNumericFunction('st_containsproperly', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STContainsProperly');
     $doctrine_config->addCustomNumericFunction('st_covers', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STCovers');
     $doctrine_config->addCustomNumericFunction('st_coveredby', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STCoveredBy');
     $doctrine_config->addCustomNumericFunction('st_crosses', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STCrosses');
     $doctrine_config->addCustomNumericFunction('st_disjoint', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STDisjoint');
     $doctrine_config->addCustomNumericFunction('st_distance', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STDistance');
     $doctrine_config->addCustomStringFunction('st_envelope', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STEnvelope');
     $doctrine_config->addCustomStringFunction('st_geomfromtext', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STGeomFromText');
     $doctrine_config->addCustomNumericFunction('st_length', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STLength');
     $doctrine_config->addCustomNumericFunction('st_linecrossingdirection', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STLineCrossingDirection');
     $doctrine_config->addCustomStringFunction('st_startpoint', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STStartPoint');
     $doctrine_config->addCustomStringFunction('st_summary', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STSummary');
     $doctrine_config->addCustomStringFunction('string_agg', 'MapasCulturais\\DoctrineMappings\\Functions\\StringAgg');
     $doctrine_config->addCustomStringFunction('unaccent', 'MapasCulturais\\DoctrineMappings\\Functions\\Unaccent');
     $doctrine_config->addCustomStringFunction('recurring_event_occurrence_for', 'MapasCulturais\\DoctrineMappings\\Functions\\RecurringEventOcurrenceFor');
     $doctrine_config->addCustomNumericFunction('st_dwithin', 'MapasCulturais\\DoctrineMappings\\Functions\\STDWithin');
     $doctrine_config->addCustomNumericFunction('st_makepoint', 'MapasCulturais\\DoctrineMappings\\Functions\\STMakePoint');
     $doctrine_config->setQueryCacheImpl($this->_cache);
     // obtaining the entity manager
     $this->_em = EntityManager::create($config['doctrine.database'], $doctrine_config);
     \MapasCulturais\DoctrineMappings\Types\Frequency::register();
     \MapasCulturais\DoctrineMappings\Types\Point::register();
     \MapasCulturais\DoctrineMappings\Types\Geography::register();
     \MapasCulturais\DoctrineMappings\Types\Geometry::register();
     if (@$config['app.log.query']) {
         $doctrine_config->setSQLLogger($config['app.queryLogger']);
     }
     $this->_em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('point', 'point');
     $this->_em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('geography', 'geography');
     $this->_em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('geometry', 'geometry');
     // ============= STORAGE =============== //
     if (key_exists('storage.driver', $config) && class_exists($config['storage.driver']) && is_subclass_of($config['storage.driver'], '\\MapasCulturais\\Storage')) {
         $storage_class = $config['storage.driver'];
         $this->_storage = key_exists('storage.config', $config) ? $storage_class::i($config['storage.config']) : $storage_class::i();
     } else {
         $this->_storage = \MapasCulturais\Storage\FileSystem::i();
     }
     // ===================================== //
     // add middlewares
     if (is_array($config['slim.middlewares'])) {
         foreach ($config['slim.middlewares'] as $middleware) {
             $this->add($middleware);
         }
     }
     // instantiate the route manager
     $this->_routesManager = new RoutesManager(key_exists('routes', $config) ? $config['routes'] : array());
     $this->applyHookBoundTo($this, 'mapasculturais.init');
     $this->register();
     // =============== AUTH ============== //
     $auth_class_name = $config['auth.provider'][0] === '\\' ? $config['auth.provider'] : 'MapasCulturais\\AuthProviders\\' . $config['auth.provider'];
     $this->_auth = new $auth_class_name($config['auth.config']);
     $this->_auth->setCookies();
     // run theme theme.php
     if (file_exists(ACTIVE_THEME_PATH . 'theme.php')) {
         include ACTIVE_THEME_PATH . 'theme.php';
     }
     // ===================================== //
     // run plugins
     foreach ($config['plugins.enabled'] as $plugin) {
         include PLUGINS_PATH . $plugin . '.php';
     }
     // ===================================== //
     if (defined('DB_UPDATES_FILE') && file_exists(DB_UPDATES_FILE)) {
         $this->_dbUpdates();
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * Initializes the application instance.
  *
  * This method
  * starts the session,
  * call Slim constructor,
  * set the custom log writer (if is defined in config),
  * bootstraps the Doctrine,
  * bootstraps the Auth Manager,
  * creates the cache and rcache components,
  * sets the file storage,
  * adds midlewares,
  * instantiates the Route Manager and
  * includes the theme.php file of the active theme if the file exists.
  *
  *
  * If the application was previously initiated, this method returns the application in the first line.
  *
  * @return \MapasCulturais\App
  */
 public function init($config = [])
 {
     if ($this->_initiated) {
         return $this;
     }
     $this->_initiated = true;
     if ($config['slim.debug']) {
         error_reporting(E_ALL ^ E_STRICT);
     }
     session_start();
     if ($config['app.offline']) {
         $bypass_callable = $config['app.offlineBypassFunction'];
         if (!is_callable($bypass_callable) || !$bypass_callable()) {
             http_response_code(307);
             header('Location: ' . $config['app.offlineUrl']);
         }
     }
     // =============== CACHE =============== //
     if (key_exists('app.cache', $config) && is_object($config['app.cache']) && is_subclass_of($config['app.cache'], '\\Doctrine\\Common\\Cache\\CacheProvider')) {
         $this->_cache = $config['app.cache'];
     } else {
         $this->_cache = new \Doctrine\Common\Cache\ArrayCache();
     }
     $this->_cache->setNamespace($config['app.cache.namespace']);
     spl_autoload_register(function ($class) use($config) {
         $cache_id = "AUTOLOAD_CLASS:{$class}";
         if ($config['app.useRegisteredAutoloadCache'] && $this->cache->contains($cache_id)) {
             $path = $this->cache->fetch($cache_id);
             require_once $path;
             return true;
         }
         foreach ($config['namespaces'] as $namespace => $base_dir) {
             if (strpos($class, $namespace) === 0) {
                 $path = str_replace('\\', '/', str_replace($namespace, $base_dir, $class) . '.php');
                 if (\file_exists($path)) {
                     require_once $path;
                     if ($config['app.useRegisteredAutoloadCache']) {
                         $this->cache->save($cache_id, $path, $config['app.registeredAutoloadCache.lifetime']);
                     }
                     return true;
                 }
             }
         }
     });
     // extende a config with theme config files
     $theme_class = "\\" . $config['themes.active'] . '\\Theme';
     $theme_path = $theme_class::getThemeFolder() . '/';
     if (file_exists($theme_path . 'conf-base.php')) {
         $theme_config = (require $theme_path . 'conf-base.php');
         $config = array_merge($config, $theme_config);
     }
     if (file_exists($theme_path . 'config.php')) {
         $theme_config = (require $theme_path . 'config.php');
         $config = array_merge($config, $theme_config);
     }
     $config['app.mode'] = key_exists('app.mode', $config) ? $config['app.mode'] : 'production';
     $this->_config = $config;
     $this->_config['path.layouts'] = APPLICATION_PATH . 'themes/active/layouts/';
     $this->_config['path.templates'] = APPLICATION_PATH . 'themes/active/views/';
     $this->_config['path.metadata_inputs'] = APPLICATION_PATH . 'themes/active/metadata-inputs/';
     if (!key_exists('app.sanitize_filename_function', $this->_config)) {
         $this->_config['app.sanitize_filename_function'] = null;
     }
     $theme_class = $config['themes.active'] . '\\Theme';
     parent::__construct(['log.level' => $config['slim.log.level'], 'log.enabled' => $config['slim.log.enabled'], 'debug' => $config['slim.debug'], 'templates.path' => $this->_config['path.templates'], 'view' => new $theme_class($config['themes.assetManager']), 'mode' => $this->_config['app.mode']]);
     $config = $this->_config;
     // custom log writer
     if (isset($config['slim.log.writer']) && is_object($config['slim.log.writer']) && method_exists($config['slim.log.writer'], 'write')) {
         $log = $this->getLog();
         $log->setWriter($config['slim.log.writer']);
     }
     // creates runtime cache component
     $this->_rcache = new \Doctrine\Common\Cache\ArrayCache();
     // ===================================== //
     // ========== BOOTSTRAPING DOCTRINE ========== //
     // annotation driver
     $doctrine_config = Setup::createConfiguration($config['doctrine.isDev']);
     $classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
     $classLoader->register();
     $driver = new AnnotationDriver(new AnnotationReader());
     $driver->addPaths([__DIR__ . '/Entities/']);
     // tells the doctrine to ignore hook annotation.
     AnnotationReader::addGlobalIgnoredName('hook');
     // driver must be pdo_pgsql
     $config['doctrine.database']['driver'] = 'pdo_pgsql';
     // registering noop annotation autoloader - allow all annotations by default
     AnnotationRegistry::registerLoader('class_exists');
     $doctrine_config->setMetadataDriverImpl($driver);
     $proxy_dir = APPLICATION_PATH . 'lib/MapasCulturais/DoctrineProxies';
     $proxy_namespace = 'MapasCulturais\\DoctrineProxies';
     $doctrine_config->setProxyDir($proxy_dir);
     $doctrine_config->setProxyNamespace($proxy_namespace);
     \Doctrine\ORM\Proxy\Autoloader::register($proxy_dir, $proxy_namespace);
     /** DOCTRINE2 SPATIAL */
     $doctrine_config->addCustomStringFunction('st_asbinary', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STAsBinary');
     $doctrine_config->addCustomStringFunction('st_astext', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STAsText');
     $doctrine_config->addCustomNumericFunction('st_area', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STArea');
     $doctrine_config->addCustomStringFunction('st_centroid', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STCentroid');
     $doctrine_config->addCustomStringFunction('st_closestpoint', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STClosestPoint');
     $doctrine_config->addCustomNumericFunction('st_contains', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STContains');
     $doctrine_config->addCustomNumericFunction('st_containsproperly', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STContainsProperly');
     $doctrine_config->addCustomNumericFunction('st_covers', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STCovers');
     $doctrine_config->addCustomNumericFunction('st_coveredby', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STCoveredBy');
     $doctrine_config->addCustomNumericFunction('st_crosses', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STCrosses');
     $doctrine_config->addCustomNumericFunction('st_disjoint', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STDisjoint');
     $doctrine_config->addCustomNumericFunction('st_distance', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STDistance');
     $doctrine_config->addCustomStringFunction('st_envelope', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STEnvelope');
     $doctrine_config->addCustomStringFunction('st_geomfromtext', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STGeomFromText');
     $doctrine_config->addCustomNumericFunction('st_length', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STLength');
     $doctrine_config->addCustomNumericFunction('st_linecrossingdirection', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STLineCrossingDirection');
     $doctrine_config->addCustomStringFunction('st_startpoint', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STStartPoint');
     $doctrine_config->addCustomStringFunction('st_summary', 'CrEOF\\Spatial\\ORM\\Query\\AST\\Functions\\PostgreSql\\STSummary');
     $doctrine_config->addCustomStringFunction('string_agg', 'MapasCulturais\\DoctrineMappings\\Functions\\StringAgg');
     $doctrine_config->addCustomStringFunction('unaccent', 'MapasCulturais\\DoctrineMappings\\Functions\\Unaccent');
     $doctrine_config->addCustomStringFunction('recurring_event_occurrence_for', 'MapasCulturais\\DoctrineMappings\\Functions\\RecurringEventOcurrenceFor');
     $doctrine_config->addCustomNumericFunction('st_dwithin', 'MapasCulturais\\DoctrineMappings\\Functions\\STDWithin');
     $doctrine_config->addCustomNumericFunction('st_makepoint', 'MapasCulturais\\DoctrineMappings\\Functions\\STMakePoint');
     $doctrine_config->setMetadataCacheImpl($this->_cache);
     $doctrine_config->setQueryCacheImpl($this->_cache);
     $doctrine_config->setResultCacheImpl($this->_cache);
     // obtaining the entity manager
     $this->_em = EntityManager::create($config['doctrine.database'], $doctrine_config);
     \MapasCulturais\DoctrineMappings\Types\Frequency::register();
     \MapasCulturais\DoctrineMappings\Types\Point::register();
     \MapasCulturais\DoctrineMappings\Types\Geography::register();
     \MapasCulturais\DoctrineMappings\Types\Geometry::register();
     if (@$config['app.log.query']) {
         $doctrine_config->setSQLLogger($config['app.queryLogger']);
     }
     $this->_em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('point', 'point');
     $this->_em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('geography', 'geography');
     $this->_em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('geometry', 'geometry');
     // ============= STORAGE =============== //
     if (key_exists('storage.driver', $config) && class_exists($config['storage.driver']) && is_subclass_of($config['storage.driver'], '\\MapasCulturais\\Storage')) {
         $storage_class = $config['storage.driver'];
         $this->_storage = key_exists('storage.config', $config) ? $storage_class::i($config['storage.config']) : $storage_class::i();
     } else {
         $this->_storage = \MapasCulturais\Storage\FileSystem::i();
     }
     // ===================================== //
     // add middlewares
     if (is_array($config['slim.middlewares'])) {
         foreach ($config['slim.middlewares'] as $middleware) {
             $this->add($middleware);
         }
     }
     // instantiate the route manager
     $this->_routesManager = new RoutesManager(key_exists('routes', $config) ? $config['routes'] : []);
     $this->applyHookBoundTo($this, 'mapasculturais.init');
     $this->register();
     // =============== AUTH ============== //
     if ($token = $this->request()->headers->get('authorization')) {
         $this->_auth = new AuthProviders\JWT(['token' => $token]);
     } else {
         $auth_class_name = strpos($config['auth.provider'], '\\') !== false ? $config['auth.provider'] : 'MapasCulturais\\AuthProviders\\' . $config['auth.provider'];
         $this->_auth = new $auth_class_name($config['auth.config']);
         $this->_auth->setCookies();
     }
     // initialize theme
     $this->view->init();
     // ===================================== //
     // run plugins
     foreach ($config['plugins.enabled'] as $plugin) {
         include PLUGINS_PATH . $plugin . '.php';
     }
     // ===================================== //
     if (defined('DB_UPDATES_FILE') && file_exists(DB_UPDATES_FILE)) {
         $this->_dbUpdates();
     }
     return $this;
 }