• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP Cache\CacheableMetadata类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了PHP中Drupal\Core\Cache\CacheableMetadata的典型用法代码示例。如果您正苦于以下问题:PHP CacheableMetadata类的具体用法?PHP CacheableMetadata怎么用?PHP CacheableMetadata使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了CacheableMetadata类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的PHP代码示例。

示例1: onPageContext

 /**
  * Adds in the current user as a context.
  *
  * @param \Drupal\page_manager\Event\PageManagerContextEvent $event
  *   The page entity context event.
  */
 public function onPageContext(PageManagerContextEvent $event)
 {
     $request = $this->requestStack->getCurrentRequest();
     $page = $event->getPage();
     $routes = $this->routeProvider->getRoutesByPattern($page->getPath())->all();
     $route = reset($routes);
     if ($route && ($route_contexts = $route->getOption('parameters'))) {
         foreach ($route_contexts as $route_context_name => $route_context) {
             // Skip this parameter.
             if ($route_context_name == 'page_manager_page_variant' || $route_context_name == 'page_manager_page') {
                 continue;
             }
             $context_name = $this->t('{@name} from route', ['@name' => $route_context_name]);
             if ($request->attributes->has($route_context_name)) {
                 $value = $request->attributes->get($route_context_name);
             } else {
                 // @todo Find a way to add in a fake value for configuration.
                 $value = NULL;
             }
             $cacheability = new CacheableMetadata();
             $cacheability->setCacheContexts(['route']);
             $context = new Context(new ContextDefinition($route_context['type'], $context_name, FALSE), $value);
             $context->addCacheableDependency($cacheability);
             $page->addContext($route_context_name, $context);
         }
     }
 }
开发者ID:ns-oxit-study,项目名称:drupal-8-training,代码行数:33,代码来源:RouteParamContext.php


示例2: helpMain

 /**
  * Prints a page listing various types of help.
  *
  * The page has sections defined by \Drupal\help\HelpSectionPluginInterface
  * plugins.
  *
  * @return array
  *   A render array for the help page.
  */
 public function helpMain()
 {
     $output = [];
     // We are checking permissions, so add the user.permissions cache context.
     $cacheability = new CacheableMetadata();
     $cacheability->addCacheContexts(['user.permissions']);
     $plugins = $this->helpManager->getDefinitions();
     $cacheability->addCacheableDependency($this->helpManager);
     foreach ($plugins as $plugin_id => $plugin_definition) {
         // Check the provided permission.
         if (!empty($plugin_definition['permission']) && !$this->currentuser()->hasPermission($plugin_definition['permission'])) {
             continue;
         }
         // Add the section to the page.
         /** @var \Drupal\help\HelpSectionPluginInterface $plugin */
         $plugin = $this->helpManager->createInstance($plugin_id);
         $this_output = ['#theme' => 'help_section', '#title' => $plugin->getTitle(), '#description' => $plugin->getDescription(), '#empty' => $this->t('There is currently nothing in this section.'), '#links' => []];
         $links = $plugin->listTopics();
         if (is_array($links) && count($links)) {
             $this_output['#links'] = $links;
         }
         $cacheability->addCacheableDependency($plugin);
         $output[$plugin_id] = $this_output;
     }
     $cacheability->applyTo($output);
     return $output;
 }
开发者ID:ravibarnwal,项目名称:laraitassociate.in,代码行数:36,代码来源:HelpController.php


示例3: getRuntimeContexts

 /**
  * {@inheritdoc}
  */
 public function getRuntimeContexts(array $unqualified_context_ids)
 {
     // Add a context for each language type.
     $language_types = $this->languageManager->getLanguageTypes();
     $info = $this->languageManager->getDefinedLanguageTypesInfo();
     if ($unqualified_context_ids) {
         foreach ($unqualified_context_ids as $unqualified_context_id) {
             if (array_search($unqualified_context_id, $language_types) === FALSE) {
                 unset($language_types[$unqualified_context_id]);
             }
         }
     }
     $result = [];
     foreach ($language_types as $type_key) {
         if (isset($info[$type_key]['name'])) {
             $context = new Context(new ContextDefinition('language', $info[$type_key]['name']));
             $context->setContextValue($this->languageManager->getCurrentLanguage($type_key));
             $cacheability = new CacheableMetadata();
             $cacheability->setCacheContexts(['languages:' . $type_key]);
             $context->addCacheableDependency($cacheability);
             $result[$type_key] = $context;
         }
     }
     return $result;
 }
开发者ID:nsp15,项目名称:Drupal8,代码行数:28,代码来源:CurrentLanguageContext.php


示例4: onRespond

 /**
  * Adds a cache tag if the 'user.permissions' cache context is present.
  *
  * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
  *   The event to process.
  */
 public function onRespond(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     if (!$this->currentUser->isAnonymous()) {
         return;
     }
     $response = $event->getResponse();
     if (!$response instanceof CacheableResponseInterface) {
         return;
     }
     // The 'user.permissions' cache context ensures that if the permissions for
     // a role are modified, users are not served stale render cache content.
     // But, when entire responses are cached in reverse proxies, the value for
     // the cache context is never calculated, causing the stale response to not
     // be invalidated. Therefore, when varying by permissions and the current
     // user is the anonymous user, also add the cache tag for the 'anonymous'
     // role.
     if (in_array('user.permissions', $response->getCacheableMetadata()->getCacheContexts())) {
         $per_permissions_response_for_anon = new CacheableMetadata();
         $per_permissions_response_for_anon->setCacheTags(['config:user.role.anonymous']);
         $response->addCacheableDependency($per_permissions_response_for_anon);
     }
 }
开发者ID:318io,项目名称:318-io,代码行数:31,代码来源:AnonymousUserResponseSubscriber.php


示例5: processOutbound

 /**
  * {@inheritdoc}
  */
 public function processOutbound($route_name, Route $route, array &$parameters, CacheableMetadata $cacheable_metadata = NULL)
 {
     if ($route_name === '<current>') {
         if ($current_route = $this->routeMatch->getRouteObject()) {
             $requirements = $current_route->getRequirements();
             // Setting _method and _schema is deprecated since 2.7. Using
             // setMethods() and setSchemes() are now the recommended ways.
             unset($requirements['_method']);
             unset($requirements['_schema']);
             $route->setRequirements($requirements);
             $route->setPath($current_route->getPath());
             $route->setSchemes($current_route->getSchemes());
             $route->setMethods($current_route->getMethods());
             $route->setOptions($current_route->getOptions());
             $route->setDefaults($current_route->getDefaults());
             $parameters = array_merge($parameters, $this->routeMatch->getRawParameters()->all());
             if ($cacheable_metadata) {
                 $cacheable_metadata->addCacheContexts(['route']);
             }
         } else {
             // If we have no current route match available, point to the frontpage.
             $route->setPath('/');
         }
     }
 }
开发者ID:RealLukeMartin,项目名称:drupal8tester,代码行数:28,代码来源:RouteProcessorCurrent.php


示例6: getCacheableMetadata

 /**
  * {@inheritdoc}
  */
 public function getCacheableMetadata($menu_name = NULL)
 {
     if (!$menu_name) {
         throw new \LogicException('No menu name provided for menu.active_trails cache context.');
     }
     $cacheable_metadata = new CacheableMetadata();
     return $cacheable_metadata->setCacheTags(["config:system.menu.{$menu_name}"]);
 }
开发者ID:eigentor,项目名称:tommiblog,代码行数:11,代码来源:MenuActiveTrailsCacheContext.php


示例7: getCacheableMetadata

 /**
  * {@inheritdoc}
  */
 public function getCacheableMetadata()
 {
     // Since this depends on State this can change at any time and is not
     // cacheable.
     $metadata = new CacheableMetadata();
     $metadata->setCacheMaxAge(0);
     return $metadata;
 }
开发者ID:ddrozdik,项目名称:dmaps,代码行数:11,代码来源:ConfigOverrideIntegrationTestCacheContext.php


示例8: getCacheableMetadata

 /**
  * {@inheritdoc}
  */
 public function getCacheableMetadata($name)
 {
     $metadata = new CacheableMetadata();
     if ($name === 'block.block.config_override_test') {
         $metadata->setCacheContexts(['config_override_integration_test'])->setCacheTags(['config_override_integration_test_tag']);
     }
     return $metadata;
 }
开发者ID:aWEBoLabs,项目名称:taxi,代码行数:11,代码来源:CacheabilityMetadataConfigOverride.php


示例9: process

 /**
  * {@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;
 }
开发者ID:aWEBoLabs,项目名称:taxi,代码行数:12,代码来源:FilterTestCacheMerge.php


示例10: getCacheableMetadata

 /**
  * {@inheritdoc}
  */
 public function getCacheableMetadata()
 {
     $cacheable_metadata = new CacheableMetadata();
     $tags = [];
     foreach ($this->user->getRoles() as $rid) {
         $tags[] = "config:user.role.{$rid}";
     }
     return $cacheable_metadata->setCacheTags($tags);
 }
开发者ID:RealLukeMartin,项目名称:drupal8tester,代码行数:12,代码来源:AccountPermissionsCacheContext.php


示例11: addCacheableDependency

 /**
  * {@inheritdoc}
  */
 public function addCacheableDependency($dependency)
 {
     // A trait doesn't have a constructor, so initialize the cacheability
     // metadata if that hasn't happened yet.
     if (!isset($this->cacheabilityMetadata)) {
         $this->cacheabilityMetadata = new CacheableMetadata();
     }
     $this->cacheabilityMetadata = $this->cacheabilityMetadata->merge(CacheableMetadata::createFromObject($dependency));
     return $this;
 }
开发者ID:ddrozdik,项目名称:dmaps,代码行数:13,代码来源:CacheableResponseTrait.php


示例12: getRuntimeContexts

 /**
  * {@inheritdoc}
  */
 public function getRuntimeContexts(array $unqualified_context_ids)
 {
     $current_user = $this->userStorage->load($this->account->id());
     $context = new Context(new ContextDefinition('entity:user', $this->t('Current user')), $current_user);
     $cacheability = new CacheableMetadata();
     $cacheability->setCacheContexts(['user']);
     $context->addCacheableDependency($cacheability);
     $result = ['current_user' => $context];
     return $result;
 }
开发者ID:ddrozdik,项目名称:dmaps,代码行数:13,代码来源:CurrentUserContext.php


示例13: onPageContext

 /**
  * Adds in the current user as a context.
  *
  * @param \Drupal\page_manager\Event\PageManagerContextEvent $event
  *   The page entity context event.
  */
 public function onPageContext(PageManagerContextEvent $event)
 {
     $id = $this->account->id();
     $current_user = $this->userStorage->load($id);
     $context = new Context(new ContextDefinition('entity:user', $this->t('Current user')), $current_user);
     $cacheability = new CacheableMetadata();
     $cacheability->setCacheContexts(['user']);
     $context->addCacheableDependency($cacheability);
     $event->getPage()->addContext('current_user', $context);
 }
开发者ID:ns-oxit-study,项目名称:drupal-8-training,代码行数:16,代码来源:CurrentUserContext.php


示例14: getRuntimeContexts

 /**
  * {@inheritdoc}
  */
 public function getRuntimeContexts(array $unqualified_context_ids)
 {
     $current_user = $this->userStorage->load($this->account->id());
     $context1 = new Context(new ContextDefinition('entity:user', 'User 1'), $current_user);
     $context2 = new Context(new ContextDefinition('entity:user', 'User 2'), $current_user);
     $cacheability = new CacheableMetadata();
     $cacheability->setCacheContexts(['user']);
     $context1->addCacheableDependency($cacheability);
     $context2->addCacheableDependency($cacheability);
     return ['user1' => $context1, 'user2' => $context2];
 }
开发者ID:aWEBoLabs,项目名称:taxi,代码行数:14,代码来源:MultipleStaticContext.php


示例15: getCacheableMetadata

 /**
  * {@inheritdoc}
  */
 public function getCacheableMetadata()
 {
     $cacheable_metadata = new CacheableMetadata();
     // The permissions hash changes when:
     // - a user is updated to have different roles;
     $tags = ['user:' . $this->user->id()];
     // - a role is updated to have different permissions.
     foreach ($this->user->getRoles() as $rid) {
         $tags[] = "config:user.role.{$rid}";
     }
     return $cacheable_metadata->setCacheTags($tags);
 }
开发者ID:sarahwillem,项目名称:OD8,代码行数:15,代码来源:AccountPermissionsCacheContext.php


示例16: getRuntimeContexts

 /**
  * {@inheritdoc}
  */
 public function getRuntimeContexts(array $unqualified_context_ids)
 {
     // Load the current domain.
     $current_domain = $this->negotiator->getActiveDomain();
     // Set the context.
     $context = new Context(new ContextDefinition('entity:domain', $this->t('Active domain')), $current_domain);
     // Allow caching.
     $cacheability = new CacheableMetadata();
     $cacheability->setCacheContexts(['url.site']);
     $context->addCacheableDependency($cacheability);
     // Prepare the result.
     $result = ['current_domain' => $context];
     return $result;
 }
开发者ID:dropdog,项目名称:play,代码行数:17,代码来源:CurrentDomainContext.php


示例17: getCacheableMetadata

 /**
  * {@inheritdoc}
  */
 public function getCacheableMetadata($operation = NULL)
 {
     $cacheable_metadata = new CacheableMetadata();
     if (!\Drupal::moduleHandler()->getImplementations('node_grants')) {
         return $cacheable_metadata;
     }
     // The node grants may change if the user is updated. (The max-age is set to
     // zero below, but sites may override this cache context, and change it to a
     // non-zero value. In such cases, this cache tag is needed for correctness.)
     $cacheable_metadata->setCacheTags(['user:' . $this->user->id()]);
     // If the site is using node grants, this cache context can not be
     // optimized.
     return $cacheable_metadata->setCacheMaxAge(0);
 }
开发者ID:eigentor,项目名称:tommiblog,代码行数:17,代码来源:NodeAccessGrantsCacheContext.php


示例18: onRespond

 /**
  * Sets the '4xx-response' cache tag on 4xx responses.
  *
  * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
  *   The event to process.
  */
 public function onRespond(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $response = $event->getResponse();
     if (!$response instanceof CacheableResponseInterface) {
         return;
     }
     if ($response->isClientError()) {
         $http_4xx_response_cacheability = new CacheableMetadata();
         $http_4xx_response_cacheability->setCacheTags(['4xx-response']);
         $response->addCacheableDependency($http_4xx_response_cacheability);
     }
 }
开发者ID:ddrozdik,项目名称:dmaps,代码行数:21,代码来源:ClientErrorResponseSubscriber.php


示例19: forceLogin

 /**
  * 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;
 }
开发者ID:pulibrary,项目名称:recap,代码行数:18,代码来源:ForceLoginController.php


示例20: getVisibleBlocksPerRegion

 /**
  * {@inheritdoc}
  */
 public function getVisibleBlocksPerRegion(array &$cacheable_metadata = [])
 {
     $active_theme = $this->themeManager->getActiveTheme();
     // Build an array of the region names in the right order.
     $empty = array_fill_keys($active_theme->getRegions(), array());
     $full = array();
     foreach ($this->blockStorage->loadByProperties(array('theme' => $active_theme->getName())) as $block_id => $block) {
         /** @var \Drupal\block\BlockInterface $block */
         $access = $block->access('view', NULL, TRUE);
         $region = $block->getRegion();
         if (!isset($cacheable_metadata[$region])) {
             $cacheable_metadata[$region] = CacheableMetadata::createFromObject($access);
         } else {
             $cacheable_metadata[$region] = $cacheable_metadata[$region]->merge(CacheableMetadata::createFromObject($access));
         }
         // Set the contexts on the block before checking access.
         if ($access->isAllowed()) {
             $full[$region][$block_id] = $block;
         }
     }
     // Merge it with the actual values to maintain the region ordering.
     $assignments = array_intersect_key(array_merge($empty, $full), $empty);
     foreach ($assignments as &$assignment) {
         // Suppress errors because PHPUnit will indirectly modify the contents,
         // triggering https://bugs.php.net/bug.php?id=50688.
         @uasort($assignment, 'Drupal\\block\\Entity\\Block::sort');
     }
     return $assignments;
 }
开发者ID:ddrozdik,项目名称:dmaps,代码行数:32,代码来源:BlockRepository.php



注:本文中的Drupal\Core\Cache\CacheableMetadata类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP Config\Config类代码示例发布时间:2022-05-23
下一篇:
PHP Cache\CacheTagsInvalidatorInterface类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap