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; }
/** * @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); }
/** * 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; } } }
/** * 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; } } }
/** * Purges data for the given URL. * * @param string $url A URL */ public function purge($url) { if (file_exists($path = $this->getPath($this->getCacheKey(Request::create($url))))) { unlink($path); } }
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); }
/** * 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()); }