Example #1
0
 /**
  * @covers Symfony\Components\HttpFoundation\Request::getFormat
  */
 public function testGetFormat()
 {
     $request = new Request();
     $this->assertNull($request->getFormat(null), '->getFormat() returns null when mime-type is null');
     $this->assertNull($request->getFormat('unexistant-mime-type'), '->getFormat() returns null when mime-type is unknown');
     $this->assertEquals('txt', $request->getFormat('text/plain'), '->getFormat() returns correct format when mime-type have one format only');
     $this->assertEquals('js', $request->getFormat('application/javascript'), '->getFormat() returns correct format when format have multiple mime-type (first)');
     $this->assertEquals('js', $request->getFormat('application/x-javascript'), '->getFormat() returns correct format when format have multiple mime-type');
     $this->assertEquals('js', $request->getFormat('text/javascript'), '->getFormat() returns correct format when format have multiple mime-type (last)');
 }
Example #2
0
 public function request($method, $uri = '/', $server = array(), $cookies = array())
 {
     if (null === $this->kernel) {
         throw new \LogicException('You must call setNextResponse() before calling request().');
     }
     $this->kernel->reset();
     $this->store = new Store(sys_get_temp_dir() . '/http_cache');
     $this->cacheConfig['debug'] = true;
     $this->cache = new Cache($this->kernel, $this->store, null, $this->cacheConfig);
     $this->request = Request::create($uri, $method, array(), $cookies, array(), $server);
     $this->response = $this->cache->handle($this->request);
     $this->responses[] = $this->response;
 }
 /**
  * @throws \InvalidArgumentException When the exception template does not exist
  */
 public function exceptionAction(\Exception $exception, Request $originalRequest, array $logs)
 {
     $template = $this->container->getParameter('kernel.debug') ? 'exception' : 'error';
     $request = $this->getRequest();
     $format = $format = $originalRequest->getRequestFormat();
     // when using CLI, we force the format to be TXT
     if (0 === strncasecmp(PHP_SAPI, 'cli', 3)) {
         $format = 'txt';
     }
     $template = $this->container->getTemplatingService()->getLoader()->load($template, array('bundle' => 'FrameworkBundle', 'controller' => 'Exception', 'format' => '.' . $format));
     if (false === $template) {
         throw new \InvalidArgumentException(sprintf('The exception template for format "%s" does not exist.', $format));
     }
     $code = $exception instanceof HttpException ? $exception->getCode() : 500;
     $text = Response::$statusTexts[$code];
     $formatter = new ExceptionFormatter($this->container);
     $message = null === $exception->getMessage() ? 'n/a' : $exception->getMessage();
     $name = get_class($exception);
     $traces = $formatter->getTraces($exception, 'html' === $format ? 'html' : 'text');
     $charset = $this->container->getParameter('kernel.charset');
     $errors = 0;
     foreach ($logs as $log) {
         if ('ERR' === $log['priorityName']) {
             ++$errors;
         }
     }
     $currentContent = '';
     while (false !== ($content = ob_get_clean())) {
         $currentContent .= $content;
     }
     ob_start();
     require $template;
     $content = ob_get_clean();
     $response = $this->container->getResponseService();
     $response->setStatusCode($code);
     $response->setContent($content);
     return $response;
 }
 /**
  * @dataProvider getFormat
  * @runInSeparateProcess
  */
 public function testExecution($format)
 {
     $tmpDir = sys_get_temp_dir() . '/sf_hello';
     $filesystem = new Filesystem();
     $filesystem->remove($tmpDir);
     $tester = new CommandTester(new InitApplicationCommand());
     $tester->execute(array('name' => 'Hello' . $format, 'path' => $tmpDir . '/hello' . $format, 'web_path' => $tmpDir . '/web', '--format' => $format));
     $filesystem->mkdirs($tmpDir . '/src');
     $filesystem->touch($tmpDir . '/src/autoload.php');
     $class = 'Hello' . $format . 'Kernel';
     $file = $tmpDir . '/hello' . $format . '/' . $class . '.php';
     $this->assertTrue(file_exists($file));
     $content = file_get_contents($file);
     $content = str_replace("__DIR__.'/../src/vendor/Symfony/src/Symfony/Bundle'", "'" . __DIR__ . "/../../..'", $content);
     file_put_contents($file, $content);
     require_once $file;
     $kernel = new $class('dev', true);
     $response = $kernel->handle(Request::create('/'));
     $this->assertRegExp('/successfully/', $response->getContent());
     $filesystem->remove($tmpDir);
 }
Example #5
0
 /**
  * Renders a Controller and returns the Response content.
  *
  * Note that this method generates an esi:include tag only when both the standalone
  * option is set to true and the request has ESI capability (@see Symfony\Components\HttpKernel\Cache\ESI).
  *
  * Available options:
  *
  *  * path: An array of path parameters (only when the first argument is a controller)
  *  * query: An array of query parameters (only when the first argument is a controller)
  *  * ignore_errors: true to return an empty string in case of an error
  *  * alt: an alternative controller to execute in case of an error (can be a controller, a URI, or an array with the controller, the path arguments, and the query arguments)
  *  * standalone: whether to generate an esi:include tag or not when ESI is supported
  *  * comment: a comment to add when returning an esi:include tag
  *
  * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
  * @param array  $options    An array of options
  *
  * @return string The Response content
  */
 public function render($controller, array $options = array())
 {
     $options = array_merge(array('path' => array(), 'query' => array(), 'ignore_errors' => true, 'alt' => array(), 'standalone' => false, 'comment' => ''), $options);
     if (!is_array($options['alt'])) {
         $options['alt'] = array($options['alt']);
     }
     if ($this->esiSupport && $options['standalone']) {
         $uri = $this->generateInternalUri($controller, $options['path'], $options['query']);
         $alt = '';
         if ($options['alt']) {
             $alt = $this->generateInternalUri($options['alt'][0], isset($options['alt'][1]) ? $options['alt'][1] : array(), isset($options['alt'][2]) ? $options['alt'][2] : array());
         }
         return $this->container->getEsiService()->renderTag($uri, $alt, $options['ignore_errors'], $options['comment']);
     }
     $request = $this->container->getRequestService();
     // controller or URI?
     if (0 === strpos($controller, '/')) {
         $subRequest = Request::create($controller, 'get', array(), $request->cookies->all(), array(), $request->server->all());
     } else {
         $options['path']['_controller'] = $controller;
         $options['path']['_format'] = $request->getRequestFormat();
         $subRequest = $request->duplicate($options['query'], null, $options['path']);
     }
     try {
         return $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::EMBEDDED_REQUEST, true);
     } catch (\Exception $e) {
         if ($options['alt']) {
             $alt = $options['alt'];
             unset($options['alt']);
             $options['path'] = isset($alt[1]) ? $alt[1] : array();
             $options['query'] = isset($alt[2]) ? $alt[2] : array();
             return $this->render($alt[0], $options);
         }
         if (!$options['ignore_errors']) {
             throw $e;
         }
     }
 }
Example #6
0
 /**
  * Handles an ESI from the cache.
  *
  * @param Symfony\Components\HttpKernel\Cache\Cache $cache        A Cache instance
  * @param string                                    $uri          The main URI
  * @param string                                    $alt          An alternative URI
  * @param Boolean                                   $ignoreErrors Whether to ignore errors or not
  */
 public function handle(Cache $cache, $uri, $alt, $ignoreErrors)
 {
     $subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
     try {
         return $cache->handle($subRequest, HttpKernelInterface::EMBEDDED_REQUEST, true);
     } catch (\Exception $e) {
         if ($alt) {
             return $this->handle($cache, $alt, '', $ignoreErrors);
         }
         if (!$ignoreErrors) {
             throw $e;
         }
     }
 }
Example #7
0
 /**
  * Returns a cache key for the given Request.
  *
  * @param Symfony\Components\HttpFoundation\Request $request A Request instance
  *
  * @return string A key for the given Request
  */
 public function getCacheKey(Request $request)
 {
     if (isset($this->keyCache[$request])) {
         return $this->keyCache[$request];
     }
     return $this->keyCache[$request] = 'md' . sha1($request->getUri());
 }
Example #8
0
 /**
  * Constructor.
  *
  * @param Symfony\Components\HttpFoundation\Request $request A Request instance
  * @param string|array                              $baseURLs The domain URL or an array of domain URLs
  * @param string                                    $version  The version
  */
 public function __construct(Request $request, $baseURLs = array(), $version = null)
 {
     parent::__construct($request->getBasePath(), $baseURLs, $version);
 }
Example #9
0
 /**
  * Constructor.
  *
  * @param Request $request A Request instance
  */
 public function __construct(Request $request)
 {
     $this->session = $request->getSession();
 }
Example #10
0
 protected function storeSimpleEntry($path = null, $headers = array())
 {
     if (null === $path) {
         $path = '/test';
     }
     $this->request = Request::create($path, 'get', array(), array(), array(), $headers);
     $this->response = new Response('test', 200, array('Cache-Control' => 'max-age=420'));
     return $this->store->write($this->request, $this->response);
 }
Example #11
0
 public function callController(Request $request)
 {
     return new Response('Request: ' . $request->getRequestUri());
 }
Example #12
0
 /**
  * Converts the BrowserKit request to a HttpKernel request.
  *
  * @param Symfony\Components\BrowserKit\Request $request A Request instance
  *
  * @return Symfony\Components\HttpFoundation\Request A Request instance
  */
 protected function filterRequest(DomRequest $request)
 {
     $uri = $request->getUri();
     if (preg_match('#^https?\\://([^/]+)/(.*)$#', $uri, $matches)) {
         $uri = '/' . $matches[2];
     }
     return Request::create($uri, $request->getMethod(), $request->getParameters(), $request->getFiles(), $request->getCookies(), $request->getServer());
 }
Example #13
0
 /**
  * Records that an event took place.
  *
  * @param string $event The event name
  */
 protected function record(Request $request, $event)
 {
     $this->traces[$request->getMethod() . ' ' . $request->getPathInfo()][] = $event;
 }