예제 #1
0
파일: TagSubscriber.php 프로젝트: ataxel/tp
    /**
     * Process the _tags request attribute, which is set when using the Tag
     * annotation
     *
     * - For a safe (GET or HEAD) request, the tags are set on the response.
     * - For a non-safe request, the tags will be invalidated.
     *
     * @param FilterResponseEvent $event
     */
    public function onKernelResponse(FilterResponseEvent $event)
    {
        $request = $event->getRequest();
        $response = $event->getResponse();

        $tags = array();

        // Only set cache tags or invalidate them if response is successful
        if ($response->isSuccessful()) {
            $tags = $this->getAnnotationTags($request);
        }

        $configuredTags = $this->matchRule($request, $response);
        if ($configuredTags) {
            foreach ($configuredTags['tags'] as $tag) {
                $tags[] = $tag;
            }
            foreach ($configuredTags['expressions'] as $expression) {
                $tags[] = $this->evaluateTag($expression, $request);
            }
        }

        if (!count($tags)) {
            return;
        }

        if ($request->isMethodSafe()) {
            // For safe requests (GET and HEAD), set cache tags on response
            $this->cacheManager->tagResponse($response, $tags);
        } else {
            // For non-safe methods, invalidate the tags
            $this->cacheManager->invalidateTags($tags);
        }
    }
예제 #2
0
    public function testTagResponse()
    {
        $ban = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\BanInterface');
        $router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface');

        $tags1 = array('post-1', 'posts');
        $tags2 = array('post-2');
        $tags3 = array('different');

        $cacheManager = new CacheManager($ban, $router);
        $response = new Response();
        $response->headers->set($cacheManager->getTagsHeader(), '');
        $cacheManager->tagResponse($response, $tags1);
        $this->assertTrue($response->headers->has($cacheManager->getTagsHeader()));
        $this->assertEquals(implode(',', $tags1), $response->headers->get($cacheManager->getTagsHeader()));

        $cacheManager->tagResponse($response, $tags2);
        $this->assertEquals(implode(',', array_merge($tags1, $tags2)), $response->headers->get($cacheManager->getTagsHeader()));

        $cacheManager->tagResponse($response, $tags3, true);
        $this->assertEquals(implode(',', $tags3), $response->headers->get($cacheManager->getTagsHeader()));
    }