Example #1
0
 /**
  * Filters the Response.
  *
  * @param Symfony\Components\EventDispatcher\Event $event    An Event instance
  * @param Symfony\Components\HttpKernel\Response   $response A Response instance
  */
 public function filter(Event $event, Response $response)
 {
     if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type') || $response->headers->has('Content-Type')) {
         return $response;
     }
     $request = $event->getParameter('request');
     $format = $request->getRequestFormat();
     if (null !== $format && ($mimeType = $request->getMimeType($format))) {
         $response->headers->set('Content-Type', $mimeType);
     }
     return $response;
 }
 public function filter(Event $event, Response $response)
 {
     if (!$event->getParameter('main_request') || $response->headers->has('Content-Type')) {
         return $response;
     }
     $request = $event->getParameter('request');
     $format = $request->getRequestFormat();
     if (null !== $format && ($mimeType = $request->getMimeType($format))) {
         $response->headers->set('Content-Type', $mimeType);
     }
     return $response;
 }
Example #3
0
 public function handle(Event $event, Response $response)
 {
     if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type')) {
         return $response;
     }
     $request = $event->getParameter('request');
     if ('3' === substr($response->getStatusCode(), 0, 1) || $response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html') || 'html' !== $request->getRequestFormat() || $request->isXmlHttpRequest()) {
         return $response;
     }
     $response->setContent($this->injectToolbar($request, $response));
     return $response;
 }
 public function resolve(Event $event)
 {
     $request = $event->getParameter('request');
     if (!($bundle = $request->getPathParameter('_bundle')) || !($controller = $request->getPathParameter('_controller')) || !($action = $request->getPathParameter('_action'))) {
         if (null !== $this->logger) {
             $this->logger->err(sprintf('Unable to look for the controller as some mandatory parameters are missing (_bundle: %s, _controller: %s, _action: %s)', isset($bundle) ? var_export($bundle, true) : 'NULL', isset($controller) ? var_export($controller, true) : 'NULL', isset($action) ? var_export($action, true) : 'NULL'));
         }
         return false;
     }
     $event->setReturnValue($this->find($bundle, $controller, $action, $event->getParameter('request')->getPathParameters()));
     return true;
 }
 public function resolve(Event $event)
 {
     $request = $event->getParameter('request');
     if (!($bundle = $request->getPathParameter('_bundle')) || !($controller = $request->getPathParameter('_controller')) || !($action = $request->getPathParameter('_action'))) {
         if (null !== $this->logger) {
             $this->logger->err(sprintf('Unable to look for the controller as some mandatory parameters are missing (_bundle: %s, _controller: %s, _action: %s)', isset($bundle) ? var_export($bundle, true) : 'NULL', isset($controller) ? var_export($controller, true) : 'NULL', isset($action) ? var_export($action, true) : 'NULL'));
         }
         return false;
     }
     $controller = $this->findController($bundle, $controller, $action);
     $r = new \ReflectionObject($controller[0]);
     $arguments = $this->getMethodArguments($r->getMethod($controller[1]), $event->getParameter('request')->getPathParameters(), sprintf('%s::%s()', get_class($controller[0]), $controller[1]));
     $event->setReturnValue(array($controller, $arguments));
     return true;
 }
Example #6
0
 public function handle(Event $event, Response $response)
 {
     if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type')) {
         return $response;
     }
     $this->profiler->collect($response);
     return $response;
 }
 public function handle(Event $event)
 {
     if (!$event->getParameter('main_request')) {
         return false;
     }
     $exception = $event->getParameter('exception');
     if (null !== $this->logger) {
         $this->logger->err(sprintf('%s (uncaught %s exception)', $exception->getMessage(), get_class($exception)));
     }
     $parameters = array('_bundle' => $this->bundle, '_controller' => $this->controller, '_action' => $this->action, 'exception' => $exception, 'originalRequest' => $event->getParameter('request'), 'logs' => $this->container->hasService('zend.logger.writer.debug') ? $this->container->getService('zend.logger.writer.debug')->getLogs() : array());
     $request = $event->getParameter('request')->duplicate(array('path' => $parameters));
     try {
         $response = $event->getSubject()->handleRaw($request, false);
     } catch (\Exception $e) {
         return false;
     }
     $event->setReturnValue($response);
     return true;
 }
 public function resolve(Event $event)
 {
     $request = $event->getParameter('request');
     if (!($pattern = $request->getPathParameter('_pattern'))) {
         if (null !== $this->logger) {
             $this->logger->err('Unable to look for the controller as the _pattern is not set');
         }
         return false;
     }
     if (!($controller = $this->container->getKernelService()->getController($pattern))) {
         throw new \InvalidArgumentException(sprintf('Unable to find controller for "%s".', $pattern));
     }
     $r = new \ReflectionFunction($controller);
     $parameters = $event->getParameter('request')->getPathParameters();
     $parameters['controller'] = new Controller($this->container);
     $arguments = $this->getMethodArguments($r, $parameters, $pattern);
     $event->setReturnValue(array($controller, $arguments));
     return true;
 }
 public function handle(Event $event, Response $response)
 {
     if (!$event->getParameter('main_request')) {
         return $response;
     }
     $this->response = $response;
     foreach ($this->collectors as $name => $collector) {
         $this->data[$name] = $collector->collect();
     }
     return $response;
 }
Example #10
0
 public function resolve(Event $event)
 {
     $request = $event->getParameter('request');
     if (HttpKernelInterface::MASTER_REQUEST === $event->getParameter('request_type')) {
         // set the context even if the parsing does not need to be done
         // to have correct link generation
         $this->router->setContext(array('base_url' => $request->getBaseUrl(), 'method' => $request->getMethod(), 'host' => $request->getHost(), 'is_secure' => $request->isSecure()));
     }
     if ($request->attributes->has('_controller')) {
         return;
     }
     if (false !== ($parameters = $this->router->match($request->getPathInfo()))) {
         if (null !== $this->logger) {
             $this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], str_replace("\n", '', var_export($parameters, true))));
         }
         $request->attributes->replace($parameters);
     } elseif (null !== $this->logger) {
         $this->logger->err(sprintf('No route found for %s', $request->getPathInfo()));
     }
 }
Example #11
0
 public function handle(Event $event)
 {
     if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type')) {
         return false;
     }
     $exception = $event->getParameter('exception');
     if (null !== $this->logger) {
         $this->logger->err(sprintf('%s (uncaught %s exception)', $exception->getMessage(), get_class($exception)));
     }
     $parameters = array('_controller' => $this->controller, 'exception' => $exception, 'originalRequest' => $event->getParameter('request'), 'logs' => $this->container->hasService('zend.logger.writer.debug') ? $this->container->getService('zend.logger.writer.debug')->getLogs() : array());
     $request = $event->getParameter('request')->duplicate(null, null, $parameters);
     try {
         $response = $event->getSubject()->handle($request, HttpKernelInterface::FORWARDED_REQUEST, true);
         error_log(sprintf('%s: %s', get_class($exception), $exception->getMessage()));
     } catch (\Exception $e) {
         return false;
     }
     $event->setReturnValue($response);
     return true;
 }
Example #12
0
 public function resolve(Event $event)
 {
     if (!$event->getParameter('main_request')) {
         return;
     }
     $request = $event->getParameter('request');
     $this->container->setParameter('request.base_path', $request->getBasePath());
     if ($request->getPathParameter('_bundle')) {
         return;
     }
     $this->router->setContext(array('base_url' => $request->getBaseUrl(), 'method' => $request->getMethod(), 'host' => $request->getHost(), 'is_secure' => $request->isSecure()));
     if (false !== ($parameters = $this->router->match($request->getPathInfo()))) {
         if (null !== $this->logger) {
             $this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], str_replace("\n", '', var_export($parameters, true))));
         }
         $request->setPathParameters($parameters);
     } elseif (null !== $this->logger) {
         $this->logger->err(sprintf('No route found for %s', $request->getPathInfo()));
     }
 }
Example #13
0
 public function handle(Event $event, Response $response)
 {
     if (!$event->getParameter('main_request')) {
         return $response;
     }
     $request = $this->container->getRequestService();
     if ('3' === substr($response->getStatusCode(), 0, 1) || $response->hasHeader('Content-Type') && false === strpos($response->getHeader('Content-Type'), 'html') || 'html' !== $request->getRequestFormat() || $request->isXmlHttpRequest()) {
         return $response;
     }
     $response->setContent($this->injectToolbar($response));
     return $response;
 }
 public function handle(Event $event, Response $response)
 {
     if (!$event->getParameter('main_request')) {
         return $response;
     }
     $this->response = $response;
     $data = array();
     foreach ($this->collectors as $name => $collector) {
         $data[$name] = $collector->getData();
     }
     $this->profilerStorage->write($data);
     $this->profilerStorage->purge($this->lifetime);
     return $response;
 }
 /**
  * Creates the Controller associated with the given Request.
  *
  * @param Event $event An Event instance
  *
  * @return Boolean true if the controller has been found, false otherwise
  */
 public function resolve(Event $event)
 {
     $request = $event->getParameter('request');
     if (!($controller = $request->path->get('_controller'))) {
         if (null !== $this->logger) {
             $this->logger->err('Unable to look for the controller as the "_controller" parameter is missing');
         }
         return false;
     }
     list($controller, $method) = $this->manager->findController($controller);
     $controller->setRequest($request);
     $arguments = $this->manager->getMethodArguments($request->path->all(), $controller, $method);
     $event->setReturnValue(array(array($controller, $method), $arguments));
     return true;
 }
Example #16
0
 public function resolve(Event $event)
 {
     $request = $event->getParameter('request');
     if (!($controller = $request->path->get('_controller'))) {
         if (null !== $this->logger) {
             $this->logger->err('Unable to look for the controller as the _controller parameter is missing');
         }
         return false;
     }
     $controller = $this->findController($controller);
     $controller[0]->setRequest($request);
     $r = new \ReflectionObject($controller[0]);
     $arguments = $this->getMethodArguments($r->getMethod($controller[1]), $request->path->all(), sprintf('%s::%s()', get_class($controller[0]), $controller[1]));
     $event->setReturnValue(array($controller, $arguments));
     return true;
 }
Example #17
0
 public function handle(Event $event, Response $response)
 {
     if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type')) {
         return $response;
     }
     $this->response = $response;
     $data = array();
     foreach ($this->collectors as $name => $collector) {
         $data[$name] = $collector->getData();
     }
     try {
         $this->profilerStorage->write($data);
         $this->profilerStorage->purge($this->lifetime);
     } catch (\Exception $e) {
         $this->logger->err('Unable to store the profiler information.');
     }
     return $response;
 }
Example #18
0
$subject = new stdClass();
$parameters = array('foo' => 'bar');
$event = new Event($subject, 'name', $parameters);

// ->getSubject()
$t->diag('->getSubject()');
$t->is($event->getSubject(), $subject, '->getSubject() returns the event subject');

// ->getName()
$t->diag('->getName()');
$t->is($event->getName(), 'name', '->getName() returns the event name');

// ->getParameters() ->setParameter() ->hasParameter() ->getParameter()
$t->diag('->getParameters()');
$t->is($event->getParameters(), $parameters, '->getParameters() returns the event parameters');
$t->is($event->getParameter('foo'), 'bar', '->getParameter() returns the value of a parameter');
$event->setParameter('foo', 'foo');
$t->is($event->getParameter('foo'), 'foo', '->setParameter() changes the value of a parameter');
$t->ok($event->hasParameter('foo'), '->hasParameter() returns true if the parameter is defined');
unset($event['foo']);
$t->ok(!$event->hasParameter('foo'), '->hasParameter() returns false if the parameter is not defined');

try
{
  $event->getParameter('foobar');
  $t->fail('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
}
catch (\InvalidArgumentException $e)
{
  $t->pass('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
}
 /**
  * @param Symfony\Components\EventDispatcher\Event $event
  *
  * @see Symfony\Components\EventDispatcher\EventDispatcher::notifyUntil()
  */
 public function handle(Event $event)
 {
     /** @var Bundle\ServerBundle\RequestInterface $request */
     $request = $event->getSubject();
     /** @var Bundle\ServerBundle\Socket\ServerSocket $server */
     $server = $event->getParameter('server');
     /** @var Bundle\ServerBundle\Socket\ClientSocket $client */
     $client = $event->getParameter('client');
     // collect parameters
     $requestMethod = $request->getRequestMethod();
     $requestUrl = $request->getRequestUrl();
     $url = parse_url($requestUrl);
     $queryString = isset($url['path']) ? $url['path'] : null;
     // @TODO: fix script name, script filename & path_translated
     $scriptName = '/index.php';
     // GET & POST parameters
     $getParameters = array();
     parse_str($queryString, $getParameters);
     $postParameters = array();
     if ($request->getRequestMethod() == Request::METHOD_POST) {
         parse_str($request->getBody(), $postParameters);
     }
     // local & remote address:port
     list($address, $port) = explode(':', $server->getName());
     list($remoteAddress, $remotePort) = explode(':', $client->getPeerName());
     // fake _SERVER
     // @see http://php.net/manual/de/reserved.variables.server.php
     $server = array('SERVER_SIGNATURE' => sprintf('<address>Symfony/%s (ServerBundle) Server at %s Port %d</address>', Kernel::VERSION, $this->options['hostname'], $port), 'SERVER_SOFTWARE' => sprintf('Symfony/%s (ServerBundle)', Kernel::VERSION), 'SERVER_NAME' => $this->options['hostname'], 'SERVER_ADDR' => $address, 'SERVER_PORT' => $port, 'SERVER_ADMIN' => $this->options['admin'], 'GATEWAY_INTERFACE' => 'CGI/1.1', 'SERVER_PROTOCOL' => 'HTTP/' . $request->getHttpVersion(), 'REQUEST_METHOD' => $requestMethod, 'REMOTE_ADDR' => $remoteAddress, 'REMOTE_PORT' => $remotePort, 'DOCUMENT_ROOT' => $this->options['document_root'], 'QUERY_STRING' => $queryString, 'REQUEST_URI' => $requestUrl, 'REQUEST_TIME' => $client->getAccepted(), 'PHP_SELF' => $url['path'], 'SCRIPT_NAME' => $scriptName, 'SCRIPT_FILENAME' => $scriptFilename = $this->options['document_root'] . $scriptName, 'PATH_INFO' => str_replace($scriptName, '', $url['path']), 'PATH_TRANSLATED' => $scriptFilename);
     // @TODO: PATH
     // @TODO: AUTH > AUTH_TYPE, REMOTE_USER, REMOTE_IDENT
     // @TODO: POST, PUT > CONTENT_TYPE, CONTENT_LENGTH
     // extend _SERVER
     if ($request->getRequestMethod() == Request::METHOD_GET) {
         $server['argv'] = $queryString;
         $server['argc'] = count($getParameters);
     }
     if ($this->options['hostname_lookups']) {
         $server['REMOTE_HOST'] = gethostbyaddr($remoteAddress);
     }
     if ($request->hasHeader('Host')) {
         $server['HTTP_HOST'] = $request->getHeader('Host');
     }
     if ($request->hasHeader('Connection')) {
         $server['HTTP_CONNECTION'] = $request->getHeader('Connection');
     }
     if ($request->hasHeader('User-Agent')) {
         $server['HTTP_USER_AGENT'] = $request->getHeader('User-Agent');
     }
     if ($request->hasHeader('Accept')) {
         $server['HTTP_ACCEPT'] = $request->getHeader('Accept');
     }
     if ($request->hasHeader('Accept-Encoding')) {
         $server['HTTP_ACCEPT_ENCODING'] = $request->getHeader('Accept-Encoding');
     }
     if ($request->hasHeader('Accept-Language')) {
         $server['HTTP_ACCEPT_LANGUAGE'] = $request->getHeader('Accept-Language');
     }
     if ($request->hasHeader('Accept-Charset')) {
         $server['HTTP_ACCEPT_CHARSET'] = $request->getHeader('Accept-Charset');
     }
     if ($request->hasHeader('Cookie')) {
         $server['HTTP_COOKIE'] = $request->getHeader('Cookie');
     }
     if ($request->hasHeader('Referer')) {
         $server['HTTP_REFERER'] = $request->getHeader('Referer');
     }
     // fake _COOKIE
     $cookies = array();
     if ($request->hasHeader('Cookie')) {
         parse_str($request->getHeader('Cookie'), $cookies);
     }
     // @TODO: php.ini - request_order > _REQUEST
     $parameters = array_merge($getParameters, $postParameters);
     // @TODO: fake _FILES
     $files = array();
     // initialize SymfonyRequest
     $sfRequest = SymfonyRequest::create($requestUrl, $requestMethod, $parameters, $cookies, $files, $server);
     try {
         if (null !== $this->customKernel) {
             /** @var $sfResponse Symfony\Components\HttpKernel\Response */
             $sfResponse = $this->customKernel->handle($sfRequest);
         } else {
             /** @var $sfResponse Symfony\Components\HttpKernel\Response */
             $sfResponse = $this->kernel->handle($sfRequest);
         }
     } catch (\Exception $e) {
         $code = 500;
         $status = SymfonyResponse::$statusTexts[$code];
         $headers = array();
         $content = sprintf('<h1>Error %d - %s</h1>', $code, $status);
         // ExceptionController-like view renderer would be cool
         // add Date header
         $date = new \DateTime();
         $headers['Date'] = $date->format(DATE_RFC822);
         // add Content-Length header
         $headers['Content-Length'] = strlen($content);
         // build Response
         $response = $this->container->getServer_ResponseService();
         $response->setHttpVersion($request->getHttpVersion());
         $response->setStatusCode($code, $status);
         $response->addHeaders($headers);
         $response->setBody($content);
         $event->setReturnValue($response);
         return true;
     }
     // add Date header
     $date = new \DateTime();
     $sfResponse->headers->set('Date', $date->format(DATE_RFC822));
     // add Content-Length header
     $sfContent = $sfResponse->getContent();
     $sfResponse->headers->set('Content-Length', strlen($sfContent));
     // build Response
     $response = $this->container->getServer_ResponseService();
     $response->setHttpVersion($sfResponse->getProtocolVersion());
     $response->setStatusCode($sfResponse->getStatusCode());
     $response->addHeaders($sfResponse->headers->all());
     $response->setBody($sfContent);
     $event->setReturnValue($response);
     return true;
 }