コード例 #1
0
 public function onEventTerminate(Application $app)
 {
     // To speed things up (by avoiding Swift Mailer initialization), flush
     // messages only if our mailer has been created (potentially used)
     // IMPORTANT! For files spool you need flush queue from console
     if ($app->getParameter('swiftmailer.initialized') && $app->getParameter('swiftmailer.use_spool') && $app->getParameter('swiftmailer.options.spool_type', 'memory') == 'memory') {
         $app->make('swiftmailer.transport_spool')->getSpool()->flushQueue($app->make('swiftmailer.transport'));
     }
 }
コード例 #2
0
 public function register(Application $app)
 {
     $app->singleton('twig', function () use($app) {
         $options = array_replace(['charset' => $app->getParameter('charset'), 'debug' => $app->getParameter('debug'), 'strict_variables' => $app->getParameter('debug')], $app->getParameter('twig.options', []));
         /** @var Twig_LoaderInterface $loader */
         $loader = $app->make('twig.loader');
         $twig = new \Twig_Environment($loader, $options);
         $twig->addGlobal('app', $app);
         return $twig;
     });
     $app->singleton('twig.loader', function () use($app) {
         return new \Twig_Loader_Filesystem($app->getParameter('twig.path'));
     });
 }
コード例 #3
0
 public function onEventResponse(Application $app, Response $response)
 {
     if (null === $response->getCharset()) {
         $response->setCharset($app->getParameter('charset'));
     }
     $response->prepare($app->getRequest());
 }
コード例 #4
0
 public function onEventException(Application $app, \Exception $e)
 {
     $response = $app->response('', 500);
     if ($e instanceof HttpException) {
         $response->setStatusCode($e->getStatusCode());
     }
     if ($app->getParameter('debug')) {
         $response->setContent($this->prettyException($e));
     }
     $app->setResponse($response);
 }
コード例 #5
0
 public function register(Application $app)
 {
     $app->singleton('session', function () use($app) {
         /** @var SessionHandler $handler */
         $handler = null;
         $container = $app->getContainer();
         if ($container->exists('session.handler')) {
             $handler = $container->make('session.handler');
         }
         return new Session(new NativeSessionStorage($app->getParameter('session.options', []), $handler));
     });
 }
コード例 #6
0
 public function register(Application $app)
 {
     $app->setParameter('cache', ['namespace' => null, 'type' => 'array']);
     $app->singleton('cache', function () use($app) {
         $cache = null;
         $type = $app->getParameter('cache.type', 'array');
         if ($type == 'array') {
             $cache = new ArrayCache();
         } elseif ($type == 'apc') {
             $cache = new ApcCache();
         } elseif ($type == 'xcache') {
             $cache = new XcacheCache();
         } elseif ($type == 'memcache') {
             $cache = new MemcacheCache();
             $memcache = new \Memcache();
             $memcache->addserver($app->getParameter('cache.memcached.host', '127.0.0.1'), $app->getParameter('cache.memcached.port', 11211));
             $cache->setMemcache($memcache);
         } elseif ($type == 'memcached') {
             $cache = new MemcachedCache();
             $memcached = new \Memcached();
             $memcached->addServer($app->getParameter('cache.memcached.host', '127.0.0.1'), $app->getParameter('cache.memcached.port', 11211));
             $cache->setMemcached($memcached);
         }
         $cache->setNamespace($app->getParameter('cache.namespace'));
         return $cache;
     });
 }
コード例 #7
0
 public function register(Application $app)
 {
     $app->setParameter('db.options_default', ['dsn' => '', 'username' => 'root', 'password' => null, 'options' => []]);
     $app->singleton('db', function () use($app) {
         $manager = new Manager();
         $optionsDefault = $app->getParameter('db.options_default');
         $dbsOptions = $app->getParameter('dbs.options');
         if (!$dbsOptions) {
             $dbsOptions['default'] = $app->getParameter('db.options', []);
         }
         $defaultSet = false;
         foreach ($dbsOptions as $name => &$options) {
             $options = array_replace($optionsDefault, $options);
             $manager->addConnection($options, $name);
             if (!$defaultSet) {
                 $manager->setDefaultConnectionName($defaultSet);
                 $defaultSet = true;
             }
         }
         return $manager;
     });
 }