Example #1
0
    /**
     * 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);
        }
    }