/** * {@inheritdoc} */ public function process($text, $langcode) { $result = new FilterProcessResult($text); $metadata = new CacheableMetadata(); $metadata->addCacheTags(['merge:tag']); $metadata->addCacheContexts(['user.permissions']); $result = $result->merge($metadata); return $result; }
/** * This delegates to Cache::mergeTags(), so just a basic test. * * @covers ::addCacheTags */ public function testAddCacheTags() { $metadata = new CacheableMetadata(); $add_expected = [[[], []], [['foo:bar'], ['foo:bar']], [['foo:baz'], ['foo:bar', 'foo:baz']], [['axx:first', 'foo:baz'], ['axx:first', 'foo:bar', 'foo:baz']], [[], ['axx:first', 'foo:bar', 'foo:baz']], [['axx:first'], ['axx:first', 'foo:bar', 'foo:baz']]]; foreach ($add_expected as $data) { list($add, $expected) = $data; $metadata->addCacheTags($add); $this->assertEquals($expected, $metadata->getCacheTags()); } }
/** * Handles a page request for our forced login route. */ public function forceLogin() { // TODO: What if CAS is not configured? need to handle that case. $query_params = $this->requestStack->getCurrentRequest()->query->all(); $cas_login_url = $this->casHelper->getServerLoginUrl($query_params); $this->casHelper->log("Cas forced login route, redirecting to: {$cas_login_url}"); // This response is OK to cache, but since the redirect URL is dependent on // the configured server settings, we need to add some cache metadata tied // to the settings. $cacheable_metadata = new CacheableMetadata(); $cacheable_metadata->addCacheTags(array('config:cas.settings')); $response = TrustedRedirectResponse::create($cas_login_url, 302); $response->addCacheableDependency($cacheable_metadata); return $response; }
/** * {@inheritdoc} */ public function getCacheableMetadata() { // The book active trail depends on the node and data attached to it. // That information is however not stored as part of the node. $cacheable_metadata = new CacheableMetadata(); if ($node = $this->requestStack->getCurrentRequest()->get('node')) { // If the node is part of a book then we can use the cache tag for that // book. If not, then it can't be optimized away. if (!empty($node->book['bid'])) { $cacheable_metadata->addCacheTags(['bid:' . $node->book['bid']]); } else { $cacheable_metadata->setCacheMaxAge(0); } } return $cacheable_metadata; }
/** * Test the forcedLogin redirect. * * @covers ::forceLogin * @covers ::__construct */ public function testForceLogin() { $request = $this->getMock('\\Symfony\\Component\\HttpFoundation\\Request'); $query = $this->getMock('\\Symfony\\Component\\HttpFoundation\\ParameterBag'); $request->query = $query; $this->requestStack->expects($this->once())->method('getCurrentRequest')->will($this->returnValue($request)); $parameters = array('returnto' => 'node/1', 'foo' => 'bar'); $query->expects($this->once())->method('all')->will($this->returnValue($parameters)); $this->casHelper->expects($this->once())->method('getServerLoginUrl')->with($this->equalTo($parameters))->will($this->returnValue('https://example.com')); $cacheableMetadata = new CacheableMetadata(); $cacheableMetadata->addCacheTags(array('config:cas.settings')); $expected_response = new TrustedRedirectResponse('https://example.com', 302); $expected_response->addCacheableDependency($cacheableMetadata); $force_login_controller = new ForceLoginController($this->casHelper, $this->requestStack); $response = $force_login_controller->forceLogin(); $this->assertEquals($expected_response, $response); }
/** * Implements Drupal\Core\PathProcessor\OutboundPathProcessorInterface::processOutbound(). */ public function processOutbound($path, &$options = array(), Request $request = NULL, CacheableMetadata $cacheable_metadata = NULL) { // Rewrite user/uid to user/username. if (preg_match('!^/user/([0-9]+)(/.*)?!', $path, $matches)) { if ($account = User::load($matches[1])) { $matches += array(2 => ''); $path = '/user/' . $account->getUsername() . $matches[2]; if ($cacheable_metadata) { $cacheable_metadata->addCacheTags($account->getCacheTags()); } } } // Rewrite forum/ to community/. if ($path == '/forum' || strpos($path, '/forum/') === 0) { $path = '/community' . substr($path, 5); } return $path; }
/** * {@inheritdoc} */ public function processOutbound($path, &$options = array(), Request $request = NULL, CacheableMetadata $cacheable_metadata = NULL) { if ($request) { // The following values are not supposed to change during a single page // request processing. if (!isset($this->queryRewrite)) { if ($this->currentUser->isAnonymous()) { $languages = $this->languageManager->getLanguages(); $config = $this->config->get('language.negotiation')->get('session'); $this->queryParam = $config['parameter']; $this->queryValue = $request->query->has($this->queryParam) ? $request->query->get($this->queryParam) : NULL; $this->queryRewrite = isset($languages[$this->queryValue]); } else { $this->queryRewrite = FALSE; } } // If the user is anonymous, the user language negotiation method is // enabled, and the corresponding option has been set, we must preserve // any explicit user language preference even with cookies disabled. if ($this->queryRewrite) { if (isset($options['query']) && is_string($options['query'])) { $query = array(); parse_str($options['query'], $query); $options['query'] = $query; } if (!isset($options['query'][$this->queryParam])) { $options['query'][$this->queryParam] = $this->queryValue; } if ($cacheable_metadata) { // Cached URLs that have been processed by this outbound path // processor must be: $cacheable_metadata->addCacheTags($this->config->get('language.negotiation')->getCacheTags())->addCacheContexts(['url.query_args:' . $this->queryParam]); } } } return $path; }