public function boot(Application $app)
 {
     $app->finish(function () use($app) {
         // To speed things up (by avoiding Swift Mailer initialization), flush
         // messages only if our mailer has been created (potentially used)
         if ($app['mailer.initialized'] && $app['swiftmailer.use_spool'] && $app['swiftmailer.spooltransport'] instanceof \Swift_Transport_SpoolTransport) {
             $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
         }
     });
 }
 public function boot(Application $app)
 {
     // BC: to be removed before 1.0
     if (isset($app['swiftmailer.class_path'])) {
         throw new \RuntimeException('You have provided the swiftmailer.class_path parameter. The autoloader has been removed from Silex. It is recommended that you use Composer to manage your dependencies and handle your autoloading. If you are already using Composer, you can remove the parameter. See http://getcomposer.org for more information.');
     }
     $app->finish(function () use($app) {
         $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
     });
 }
 /** @test */
 public function eventHelpersShouldDirectlyAddListenersAfterBoot()
 {
     $app = new Application();
     $fired = false;
     $app->get("/", function () use($app, &$fired) {
         $app->finish(function () use(&$fired) {
             $fired = true;
         });
     });
     $request = Request::create('/');
     $response = $app->handle($request);
     $app->terminate($request, $response);
     $this->assertTrue($fired, 'Event was not fired');
 }
 public function testCallbacksAsServices()
 {
     $app = new Application();
     $app['service'] = $app->share(function () {
         return new self();
     });
     $app->before('service:beforeApp');
     $app->after('service:afterApp');
     $app->finish('service:finishApp');
     $app->error('service:error');
     $app->on('kernel.request', 'service:onRequest');
     $app->match('/', 'service:controller')->convert('foo', 'service:convert')->before('service:before')->after('service:after');
     $request = Request::create('/');
     $response = $app->handle($request);
     $app->terminate($request, $response);
     $this->assertEquals(array('CONVERT', 'BEFORE APP', 'ON REQUEST', 'BEFORE', 'ERROR', 'AFTER', 'AFTER APP', 'FINISH APP'), $app['service']->called);
 }
Example #5
0
 public function testWithAppendMiddlewares()
 {
     $app = new Application();
     $app->get('/foo', function () {
         return 'bar';
     });
     $finished = false;
     $app->finish(function () use(&$finished) {
         $finished = true;
     });
     $stack = new Builder();
     $stack->push('functional\\Append', '.A')->push('functional\\Append', '.B');
     $app = $stack->resolve($app);
     $request = Request::create('/foo');
     $response = $app->handle($request);
     $app->terminate($request, $response);
     $this->assertSame('bar.B.A', $response->getContent());
     $this->assertTrue($finished);
 }
 public function testFinishFilter()
 {
     $containerTarget = array();
     $app = new Application();
     $app->finish(function () use(&$containerTarget) {
         $containerTarget[] = '4_filterFinish';
     });
     $app->get('/foo', function () use(&$containerTarget) {
         $containerTarget[] = '1_routeTriggered';
         return new StreamedResponse(function () use(&$containerTarget) {
             $containerTarget[] = '3_responseSent';
         });
     });
     $app->after(function () use(&$containerTarget) {
         $containerTarget[] = '2_filterAfter';
     });
     $app->run(Request::create('/foo'));
     $this->assertSame(array('1_routeTriggered', '2_filterAfter', '3_responseSent', '4_filterFinish'), $containerTarget);
 }
Example #7
0
 /**
  *  Init middlewares
  * 
  * @param Application $app 
  */
 private function _iniMiddlewares(Application $app)
 {
     // The middleware is run before the routing and the security.
     $app->before(function (Request $request, Application $app) {
         // The request body should only be parsed as JSON
         // if the Content-Type header begins with application/json
         if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
             $content = $request->getContent();
             $data = json_decode($content, true);
             $request->request->replace(is_array($data) ? $data : array());
         }
     }, Application::EARLY_EVENT);
     // The middleware is run after the routing and the security.
     $app->before(function (Request $request, Application $app) {
         // Get route
         $attrs = $request->attributes->all();
         if (isset($attrs['_route'])) {
             $route = $attrs['_route'];
             $app['route'] = $route;
         }
         // Get route params
         if (isset($attrs['_route_params']) && count($attrs['_route_params'])) {
             $route_params = $attrs['_route_params'];
             $app['route_params'] = $route_params;
         }
     });
     // Set event after the Response
     $app->finish(function (Request $request, Response $response) use($app) {
         // Stop event 'eApp'
         $event = $app['watch']->stop('eApp');
         if ($app['debug']) {
             $data = array();
             //----------------
             // Get sum profile params
             $duration = $event->getDuration();
             $memory = $event->getMemory();
             $data['Sum'] = array('t' => $duration, 'm' => $memory);
             // Get periods
             $periods = $event->getPeriods();
             // Get profile params for periods
             if (isset($periods[0])) {
                 $bootstrapDuration = $periods[0]->getDuration();
                 $data['Bootstrap'] = array('t' => $bootstrapDuration);
             }
             if (isset($periods[1])) {
                 $routingDuration = $periods[1]->getDuration();
                 $data['Routing'] = array('t' => $routingDuration);
             }
             if (isset($periods[2])) {
                 $controllerDuration = $periods[2]->getDuration();
                 $data['Controller'] = array('t' => $controllerDuration);
             }
             if (isset($periods[3])) {
                 $renderDuration = $periods[3]->getDuration();
                 $data['Render'] = array('t' => $renderDuration);
             }
             $app['monolog']->addDebug('<== Profile:eApp ==>', $data);
         }
     });
 }
Example #8
0
    }
    $studentView = $request->get('isStudentView');
    if (!empty($studentView)) {
        if ($studentView == 'true') {
            $session->set('studentview', 'studentview');
        } else {
            $session->set('studentview', 'teacherview');
        }
    }
});
/** An after application middleware allows you to tweak the Response before it is sent to the client */
$app->after(function (Request $request, Response $response) {
});
/** A "finish" application middleware allows you to execute tasks after the Response has been sent to
 * the client (like sending emails or logging) */
$app->finish(function (Request $request) use($app) {
});
// End Silex Middlewares
// The global variable $charset has been defined in a language file too (trad4all.inc.php), this is legacy situation.
// So, we have to reassign this variable again in order to keep its value right.
$charset = $charset_initial_value;
// The global variable $text_dir has been defined in the language file trad4all.inc.php.
// For determing text direction correspondent to the current language we use now information from the internationalization library.
$text_dir = api_get_text_direction();
/** Setting the is_admin key */
$app['is_admin'] = false;
/** Including routes */
require_once 'routes.php';
// Setting doctrine2 extensions
if (isset($app['configuration']['main_database']) && isset($app['db.event_manager'])) {
    // @todo improvement do not create every time this objects
    $sortableGroup = new Gedmo\Mapping\Annotation\SortableGroup(array());
 public function boot(Application $app)
 {
     $app->mount('/', $this);
     if ($app['debug']) {
         $self = $this;
         $app->after(function () use($app, $self) {
             $self->outOfRequestScopeTypes['request'] = get_class($app['request']);
         });
         $app->finish(function () use($app, $self) {
             if (!$self->processed) {
                 $self->dump($app);
             }
         }, -1);
     }
 }
 /**
  * @param \Silex\Application $app
  *
  * @return void
  */
 public function register(Application $app)
 {
     $app->finish(function (Request $request) {
         $this->getClient()->persistCacheForRequest($request);
     });
 }
 public function boot(Application $app)
 {
     $app->finish(function () use($app) {
         $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
     });
 }
Example #12
0
$app->finish(function ($request, $response) use($app) {
    // Définition du point de log
    $ctLog['watchpoint'] = "@" . basename(__FILE__) . ".before." . __LINE__;
    $ctLog['tag'] = "CORTEXT-VESPA";
    // Log de l'user id si l'utilisateur est loggé, sinon on log 0
    $token = $app['security']->getToken();
    if ($token !== null) {
        try {
            if ($token->getUser() === "anon.") {
                $userId = 0;
            } else {
                $userId = $token->getUser()->getId();
            }
        } catch (Exception $e) {
            $userId = 0;
        }
    } else {
        $userId = 0;
    }
    $ctLog['user'] = $userId;
    // Log de l'id de session pour mieux suivre les parcours utilisateurs
    $ctLog['session'] = $app['session']->getId();
    $ctLog['route'] = $request->getPathInfo();
    $ctLog['parameters'] = $request->request->all();
    if (is_null($ctLog['parameters']) || count($ctLog['parameters']) == 0) {
        $ctLog['parameters'] = $request->query->all();
    }
    if (is_null($ctLog['parameters']) || count($ctLog['parameters']) == 0) {
        $ctLog['parameters'] = json_decode($request->getContent(), true);
    }
    if (is_null($ctLog['parameters']) || count($ctLog['parameters']) == 0) {
        $ctLog['parameters'] = $request->attributes->get("_route_params");
    }
    if (!is_null($ctLog['parameters']) && count($ctLog['parameters']) > 0) {
        array_walk($ctLog['parameters'], function (&$v, $k) {
            if (in_array($k, array("_password", "password", "confirm-password"), TRUE)) {
                $v = "";
            }
        });
    }
    $ctLog['type'] = $request->getMethod();
    $ctLog['status'] = $response->getStatusCode();
    $ctLog['response'] = json_decode($response->getContent());
    if (!$ctLog['response']) {
        $ctLog['response'] = "Not JSON";
    }
    $duration = $app['stopwatch']->stop('vespa');
    $ctLog['duration'] = $duration->getDuration();
    $ctLog['ip'] = $request->getClientIp();
    $ctLog['msg'] = "";
    // Output de la ligne de log
    $app['monolog']->addInfo("[VESPA] " . json_encode($ctLog, JSON_UNESCAPED_SLASHES));
    // Output vers mysql
    array_walk($ctLog, function (&$v, $k) {
        // Les objects provoquent une erreur et les array apparaissent comme "array" en bdd, alors on encode tout ça pour les avoir en base
        if (in_array(gettype($v), array("object", "array"), TRUE)) {
            $v = json_encode($v, JSON_UNESCAPED_SLASHES);
        }
    });
    $app['mysqlog']->addInfo("[VESPA]", $ctLog);
});