Esempio n. 1
0
 /**
  * Intervenes before a request starts to add cache headers.
  *
  * @param \Guzzle\Common\Event $event
  *   The Guzzle event object.
  */
 public function onRequestBeforeSend(Event $event)
 {
     $request = $event['request'];
     // We're only handling GET requests for now. That's all we do anyway.
     if ($request->getMethod() != RequestInterface::GET) {
         return;
     }
     $url = $request->getUrl();
     // In-memory download cache. Sometimes we fetch the same URL more than
     // once in a page load.
     // @todo Be smarter.
     if (isset(static::$downloadCache[$url])) {
         $request->setResponse(static::$downloadCache[$url]);
         return;
     }
     if ($cache = $this->cacheBackend->get($this->getCacheKey($url))) {
         // Add any headers that could be useful.
         // @todo Look at Guzzle's own cache plugin, or add a smarter cache here.
         if (!empty($cache->data->headers['etag'])) {
             $request->addHeader('If-None-Match', $cache->data->headers['etag']);
         }
         if (!empty($cache->data->headers['last-modified'])) {
             $request->addHeader('If-Modified-Since', $cache->data->headers['last-modified']);
         }
     }
 }
Esempio n. 2
0
 /**
  * User object.
  *
  * @return \Drupal\moodle\Sql\User
  */
 public function user()
 {
     // Static cache of already retrieved user data.
     $data =& drupal_static(__METHOD__, array());
     $user_cid = "moodle-user:{$this->user->id()}";
     // If we do not have this user id in the static cache, check {cache_data}.
     if (!isset($data[$user_cid])) {
         $cache = $this->cacheBackend->get($user_cid);
         if ($cache && $cache->data && isset($cache->data[$user_cid])) {
             $data[$user_cid] = $cache->data[$user_cid];
         }
     }
     // If nothing in the cache then retrieve it from the database.
     if (!isset($data[$user_cid])) {
         $user = new User();
         $this->query();
         $this->addFields();
         $statement = $this->query->execute();
         $statement->setFetchMode(\PDO::FETCH_INTO, $user);
         $data[$user_cid] = $statement->fetch();
         // Store the results for a day.
         $this->cacheBackend->set($user_cid, $data, REQUEST_TIME + 86400);
     }
     return $data[$user_cid];
 }
 /**
  * Checks if the compiled template needs an update.
  */
 protected function isFresh($cache_filename, $name)
 {
     $cid = 'twig:' . $cache_filename;
     $obj = $this->cache_object->get($cid);
     $mtime = isset($obj->data) ? $obj->data : FALSE;
     return $mtime === FALSE || $this->isTemplateFresh($name, $mtime);
 }
 /**
  * {@inheritdoc}
  *
  * Cached by role, invalidated whenever permissions change.
  */
 public function generate(AccountInterface $account)
 {
     // User 1 is the super user, and can always access all permissions. Use a
     // different, unique identifier for the hash.
     if ($account->id() == 1) {
         return $this->hash('is-super-user');
     }
     $sorted_roles = $account->getRoles();
     sort($sorted_roles);
     $role_list = implode(',', $sorted_roles);
     $cid = "user_permissions_hash:{$role_list}";
     if ($static_cache = $this->static->get($cid)) {
         return $static_cache->data;
     } else {
         $tags = Cache::buildTags('config:user.role', $sorted_roles, '.');
         if ($cache = $this->cache->get($cid)) {
             $permissions_hash = $cache->data;
         } else {
             $permissions_hash = $this->doGenerate($sorted_roles);
             $this->cache->set($cid, $permissions_hash, Cache::PERMANENT, $tags);
         }
         $this->static->set($cid, $permissions_hash, Cache::PERMANENT, $tags);
     }
     return $permissions_hash;
 }
 /**
  * Fetches from the cache backend, respecting the use caches flag.
  *
  * @param string $cid
  *   The cache ID of the data to retrieve.
  *
  * @return object|false
  *   The cache item or FALSE on failure.
  *
  * @see \Drupal\Core\Cache\CacheBackendInterface::get()
  */
 protected function cacheGet($cid)
 {
     if ($this->useCaches && $this->cacheBackend) {
         return $this->cacheBackend->get($cid);
     }
     return FALSE;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     // Log execution time.
     $start_time = microtime(TRUE);
     // Try to load the files count from cache. This function will accept two
     // arguments:
     // - cache object name (cid)
     // - cache bin, the (optional) cache bin (most often a database table) where
     //   the object is to be saved.
     //
     // cache_get() returns the cached object or FALSE if object does not exist.
     if ($cache = $this->cacheBackend->get('cache_example_files_count')) {
         /*
          * Get cached data. Complex data types will be unserialized automatically.
          */
         $files_count = $cache->data;
     } else {
         // If there was no cached data available we have to search filesystem.
         // Recursively get all files from Drupal's folder.
         $files_count = count(file_scan_directory('.', '/.*/'));
         // Since we have recalculated, we now need to store the new data into
         // cache. Complex data types will be automatically serialized before
         // being saved into cache.
         // Here we use the default setting and create an unexpiring cache item.
         // See below for an example that creates an expiring cache item.
         $this->cacheBackend->set('cache_example_files_count', $files_count, CacheBackendInterface::CACHE_PERMANENT);
     }
     $end_time = microtime(TRUE);
     $duration = $end_time - $start_time;
     // Format intro message.
     $intro_message = '<p>' . $this->t('This example will search the entire drupal folder and display a count of the files in it.') . ' ';
     $intro_message .= $this->t('This can take a while, since there are a lot of files to be searched.') . ' ';
     $intro_message .= $this->t('We will search filesystem just once and save output to the cache. We will use cached data for later requests.') . '</p>';
     $intro_message .= '<p>' . $this->t('<a href="@url">Reload this page</a> to see cache in action.', array('@url' => $this->getRequest()->getRequestUri())) . ' ';
     $intro_message .= $this->t('You can use the button below to remove cached data.') . '</p>';
     $form['file_search'] = array('#type' => 'fieldset', '#title' => $this->t('File search caching'));
     $form['file_search']['introduction'] = array('#markup' => $intro_message);
     $color = empty($cache) ? 'red' : 'green';
     $retrieval = empty($cache) ? $this->t('calculated by traversing the filesystem') : $this->t('retrieved from cache');
     $form['file_search']['statistics'] = array('#type' => 'item', '#markup' => $this->t('%count files exist in this Drupal installation; @retrieval in @time ms. <br/>(Source: <span style="color:@color;">@source</span>)', array('%count' => $files_count, '@retrieval' => $retrieval, '@time' => number_format($duration * 1000, 2), '@color' => $color, '@source' => empty($cache) ? $this->t('actual file search') : $this->t('cached'))));
     $form['file_search']['remove_file_count'] = array('#type' => 'submit', '#submit' => array(array($this, 'expireFiles')), '#value' => $this->t('Explicitly remove cached file count'));
     $form['expiration_demo'] = array('#type' => 'fieldset', '#title' => $this->t('Cache expiration settings'));
     $form['expiration_demo']['explanation'] = array('#markup' => $this->t('A cache item can be set as CACHE_PERMANENT, meaning that it will only be removed when explicitly cleared, or it can have an expiration time (a Unix timestamp).'));
     $item = $this->cacheBackend->get('cache_example_expiring_item', TRUE);
     if ($item == FALSE) {
         $item_status = $this->t('Cache item does not exist');
     } else {
         $item_status = $item->valid ? $this->t('Cache item exists and is set to expire at %time', array('%time' => $item->data)) : $this->t('Cache_item is invalid');
     }
     $form['expiration_demo']['current_status'] = array('#type' => 'item', '#title' => $this->t('Current status of cache item "cache_example_expiring_item"'), '#markup' => $item_status);
     $form['expiration_demo']['expiration'] = array('#type' => 'select', '#title' => $this->t('Time before cache expiration'), '#options' => array('never_remove' => $this->t('CACHE_PERMANENT'), -10 => $this->t('Immediate expiration'), 10 => $this->t('10 seconds from form submission'), 60 => $this->t('1 minute from form submission'), 300 => $this->t('5 minutes from form submission')), '#default_value' => -10, '#description' => $this->t('Any cache item can be set to only expire when explicitly cleared, or to expire at a given time.'));
     $form['expiration_demo']['create_cache_item'] = array('#type' => 'submit', '#value' => $this->t('Create a cache item with this expiration'), '#submit' => array(array($this, 'createExpiringItem')));
     $form['cache_clearing'] = array('#type' => 'fieldset', '#title' => $this->t('Expire and remove options'), '#description' => $this->t("We have APIs to expire cached items and also to just remove them. Unfortunately, they're all the same API, cache_clear_all"));
     $form['cache_clearing']['cache_clear_type'] = array('#type' => 'radios', '#title' => $this->t('Type of cache clearing to do'), '#options' => array('expire' => $this->t('Remove items from the "cache" bin that have expired'), 'remove_all' => $this->t('Remove all items from the "cache" bin regardless of expiration'), 'remove_tag' => $this->t('Remove all items in the "cache" bin with the tag "cache_example" set to 1')), '#default_value' => 'expire');
     // Submit button to clear cached data.
     $form['cache_clearing']['clear_expired'] = array('#type' => 'submit', '#value' => $this->t('Clear or expire cache'), '#submit' => array(array($this, 'cacheClearing')), '#access' => $this->currentUser->hasPermission('administer site configuration'));
     return $form;
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  *
  * Cached by role, invalidated whenever permissions change.
  */
 public function generate(AccountInterface $account)
 {
     $sorted_roles = $account->getRoles();
     sort($sorted_roles);
     $role_list = implode(',', $sorted_roles);
     if ($cache = $this->cache->get("user_permissions_hash:{$role_list}")) {
         $permissions_hash = $cache->data;
     } else {
         $permissions_hash = $this->doGenerate($sorted_roles);
         $this->cache->set("user_permissions_hash:{$role_list}", $permissions_hash, Cache::PERMANENT, array('user_role' => $sorted_roles));
     }
     return $permissions_hash;
 }
Esempio n. 8
0
 /**
  * Loads the base country definitions.
  *
  * @return array
  */
 protected function loadBaseDefinitions()
 {
     if (!empty($this->baseDefinitions)) {
         return $this->baseDefinitions;
     }
     $cache_key = 'address.countries.base';
     if ($cached = $this->cache->get($cache_key)) {
         $this->baseDefinitions = $cached->data;
     } else {
         $this->baseDefinitions = json_decode(file_get_contents($this->definitionPath . 'base.json'), TRUE);
         $this->cache->set($cache_key, $this->baseDefinitions, CacheBackendInterface::CACHE_PERMANENT, ['countries']);
     }
     return $this->baseDefinitions;
 }
Esempio n. 9
0
 /**
  * {@inheritdoc}
  */
 public function get($cid, $allow_invalid = FALSE)
 {
     $cache = $this->cacheBackend->get($cid, $allow_invalid);
     if ($cache) {
         $cacheCopy = new \StdClass();
         $cacheCopy->cid = $cache->cid;
         $cacheCopy->expire = $cache->expire;
         $cacheCopy->tags = $cache->tags;
         $this->cacheDataCollector->registerCacheHit($this->bin, $cacheCopy);
     } else {
         $this->cacheDataCollector->registerCacheMiss($this->bin, $cid);
     }
     return $cache;
 }
 /**
  * {@inheritdoc}
  *
  * Cached by role, invalidated whenever permissions change.
  */
 public function generate(AccountInterface $account)
 {
     $sorted_roles = $account->getRoles();
     sort($sorted_roles);
     $role_list = implode(',', $sorted_roles);
     if ($cache = $this->cache->get("user_permissions_hash:{$role_list}")) {
         $permissions_hash = $cache->data;
     } else {
         $permissions_hash = $this->doGenerate($sorted_roles);
         $tags = Cache::buildTags('config:user.role', $sorted_roles, '.');
         $this->cache->set("user_permissions_hash:{$role_list}", $permissions_hash, Cache::PERMANENT, $tags);
     }
     return $permissions_hash;
 }
Esempio n. 11
0
 /**
  * {@inheritdoc}
  */
 public function read($name)
 {
     $cache_key = $this->getCacheKey($name);
     if ($cache = $this->cache->get($cache_key)) {
         // The cache contains either the cached configuration data or FALSE
         // if the configuration file does not exist.
         return $cache->data;
     }
     // Read from the storage on a cache miss and cache the data. Also cache
     // information about missing configuration objects.
     $data = $this->storage->read($name);
     $this->cache->set($cache_key, $data);
     return $data;
 }
Esempio n. 12
0
 /**
  * Gets data from the cache backend.
  *
  * @param string $cid
  *   The cache ID to return.
  *
  * @return mixed
  *   The cached data, if any. This will immediately return FALSE if the
  *   $skipCache property is TRUE.
  */
 protected function cacheGet($cid)
 {
     if ($this->skipCache) {
         return FALSE;
     }
     return $this->cacheBackend->get($this->prepareCid($cid));
 }
Esempio n. 13
0
 /**
  * Builds the "format_tags" configuration part of the CKEditor JS settings.
  *
  * @see getConfig()
  *
  * @param \Drupal\editor\Entity\Editor $editor
  *   A configured text editor object.
  *
  * @return array
  *   An array containing the "format_tags" configuration.
  */
 protected function generateFormatTagsSetting(Editor $editor)
 {
     // When no text format is associated yet, assume no tag is allowed.
     // @see \Drupal\Editor\EditorInterface::hasAssociatedFilterFormat()
     if (!$editor->hasAssociatedFilterFormat()) {
         return array();
     }
     $format = $editor->getFilterFormat();
     $cid = 'ckeditor_internal_format_tags:' . $format->id();
     if ($cached = $this->cache->get($cid)) {
         $format_tags = $cached->data;
     } else {
         // The <p> tag is always allowed — HTML without <p> tags is nonsensical.
         $format_tags = ['p'];
         // Given the list of possible format tags, automatically determine whether
         // the current text format allows this tag, and thus whether it should show
         // up in the "Format" dropdown.
         $possible_format_tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'pre'];
         foreach ($possible_format_tags as $tag) {
             $input = '<' . $tag . '>TEST</' . $tag . '>';
             $output = trim(check_markup($input, $editor->id()));
             if ($input == $output) {
                 $format_tags[] = $tag;
             }
         }
         $format_tags = implode(';', $format_tags);
         // Cache the "format_tags" configuration. This cache item is infinitely
         // valid; it only changes whenever the text format is changed, hence it's
         // tagged with the text format's cache tag.
         $this->cache->set($cid, $format_tags, Cache::PERMANENT, $format->getCacheTags());
     }
     return $format_tags;
 }
Esempio n. 14
0
 /**
  * {@inheritdoc}
  */
 public function loadTreeData($menu_name, MenuTreeParameters $parameters)
 {
     // Build the cache ID; sort 'expanded' and 'conditions' to prevent duplicate
     // cache items.
     sort($parameters->expandedParents);
     asort($parameters->conditions);
     $tree_cid = "tree-data:{$menu_name}:" . serialize($parameters);
     $cache = $this->menuCacheBackend->get($tree_cid);
     if ($cache && isset($cache->data)) {
         $data = $cache->data;
         // Cache the definitions in memory so they don't need to be loaded again.
         $this->definitions += $data['definitions'];
         unset($data['definitions']);
     } else {
         $links = $this->loadLinks($menu_name, $parameters);
         $data['tree'] = $this->doBuildTreeData($links, $parameters->activeTrail, $parameters->minDepth);
         $data['definitions'] = array();
         $data['route_names'] = $this->collectRoutesAndDefinitions($data['tree'], $data['definitions']);
         $this->menuCacheBackend->set($tree_cid, $data, Cache::PERMANENT, ['config:system.menu.' . $menu_name]);
         // The definitions were already added to $this->definitions in
         // $this->doBuildTreeData()
         unset($data['definitions']);
     }
     return $data;
 }
 /**
  * Test that the delete tags operation is propagated to all backends
  * in the chain.
  */
 public function testDeleteTagsPropagation()
 {
     // Create two cache entries with the same tag and tag value.
     $this->chain->set('test_cid_clear1', 'foo', Cache::PERMANENT, array('test_tag:2'));
     $this->chain->set('test_cid_clear2', 'foo', Cache::PERMANENT, array('test_tag:2'));
     $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two cache items were created in all backends.');
     // Invalidate test_tag of value 1. This should invalidate both entries.
     $this->chain->invalidateTags(array('test_tag:2'));
     $this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two caches removed from all backends after clearing a cache tag.');
     // Create two cache entries with the same tag and an array tag value.
     $this->chain->set('test_cid_clear1', 'foo', Cache::PERMANENT, array('test_tag:1'));
     $this->chain->set('test_cid_clear2', 'foo', Cache::PERMANENT, array('test_tag:1'));
     $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two cache items were created in all backends.');
     // Invalidate test_tag of value 1. This should invalidate both entries.
     $this->chain->invalidateTags(array('test_tag:1'));
     $this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two caches removed from all backends after clearing a cache tag.');
     // Create three cache entries with a mix of tags and tag values.
     $this->chain->set('test_cid_clear1', 'foo', Cache::PERMANENT, array('test_tag:1'));
     $this->chain->set('test_cid_clear2', 'foo', Cache::PERMANENT, array('test_tag:2'));
     $this->chain->set('test_cid_clear3', 'foo', Cache::PERMANENT, array('test_tag_foo:3'));
     $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->firstBackend->get('test_cid_clear3') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear3') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear3'), 'Three cached items were created in all backends.');
     $this->chain->invalidateTags(array('test_tag_foo:3'));
     $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Cached items not matching the tag were not cleared from any of the backends.');
     $this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear3') && $this->secondBackend->get('test_cid_clear3') && $this->thirdBackend->get('test_cid_clear3'), 'Cached item matching the tag was removed from all backends.');
 }
Esempio n. 16
0
 /**
  * {@inheritdoc}
  */
 public function getLineItemTypeId($product_type_id)
 {
     if (!isset($this->map)) {
         if ($cached_map = $this->cache->get('commerce_product.line_item_type_map')) {
             $this->map = $cached_map->data;
         } else {
             $this->map = $this->buildMap();
             $this->cache->set('commerce_product.line_item_type_map', $this->map);
         }
     }
     // A valid product type ID should always have a matching line item type ID.
     if (empty($this->map[$product_type_id])) {
         throw new \InvalidArgumentException(sprintf('No line item type found for the "%s" product type.', $product_type_id));
     }
     return $this->map[$product_type_id];
 }
 /**
  * Tests the getAllBundleInfo() method.
  *
  * @covers ::getAllBundleInfo
  */
 public function testGetAllBundleInfo()
 {
     $this->moduleHandler->invokeAll('entity_bundle_info')->willReturn([]);
     $this->moduleHandler->alter('entity_bundle_info', Argument::type('array'))->willReturn(NULL);
     $apple = $this->prophesize(EntityTypeInterface::class);
     $apple->getLabel()->willReturn('Apple');
     $apple->getBundleOf()->willReturn(NULL);
     $banana = $this->prophesize(EntityTypeInterface::class);
     $banana->getLabel()->willReturn('Banana');
     $banana->getBundleOf()->willReturn(NULL);
     $this->setUpEntityTypeDefinitions(['apple' => $apple, 'banana' => $banana]);
     $this->cacheBackend->get('entity_bundle_info:en')->willReturn(FALSE);
     $this->cacheBackend->set('entity_bundle_info:en', Argument::any(), Cache::PERMANENT, ['entity_types', 'entity_bundles'])->will(function () {
         $this->get('entity_bundle_info:en')->willReturn((object) ['data' => 'cached data'])->shouldBeCalled();
     })->shouldBeCalled();
     $this->cacheTagsInvalidator->invalidateTags(['entity_bundles'])->shouldBeCalled();
     $this->typedDataManager->clearCachedDefinitions()->shouldBeCalled();
     $expected = ['apple' => ['apple' => ['label' => 'Apple']], 'banana' => ['banana' => ['label' => 'Banana']]];
     $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo();
     $this->assertSame($expected, $bundle_info);
     $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo();
     $this->assertSame($expected, $bundle_info);
     $this->entityTypeBundleInfo->clearCachedBundles();
     $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo();
     $this->assertSame('cached data', $bundle_info);
 }
 /**
  * Returns the cached plugin definitions of the decorated discovery class.
  *
  * @return array|null
  *   On success this will return an array of plugin definitions. On failure
  *   this should return NULL, indicating to other methods that this has not
  *   yet been defined. Success with no values should return as an empty array
  *   and would actually be returned by the getDefinitions() method.
  */
 protected function getCachedDefinitions()
 {
     if (!isset($this->definitions) && $this->cacheBackend && ($cache = $this->cacheBackend->get($this->cacheKey))) {
         $this->definitions = $cache->data;
     }
     return $this->definitions;
 }
Esempio n. 19
0
 /**
  * Provides information about modules' implementations of a hook.
  *
  * @param string $hook
  *   The name of the hook (e.g. "help" or "menu").
  *
  * @return array
  *   An array whose keys are the names of the modules which are implementing
  *   this hook and whose values are either an array of information from
  *   hook_hook_info() or FALSE if the implementation is in the module file.
  */
 protected function getImplementationInfo($hook)
 {
     if (!isset($this->implementations)) {
         $this->implementations = array();
         if ($cache = $this->cacheBackend->get('module_implements')) {
             $this->implementations = $cache->data;
         }
     }
     if (!isset($this->implementations[$hook])) {
         // The hook is not cached, so ensure that whether or not it has
         // implementations, the cache is updated at the end of the request.
         $this->cacheNeedsWriting = TRUE;
         $this->implementations[$hook] = $this->buildImplementationInfo($hook);
     } else {
         foreach ($this->implementations[$hook] as $module => $group) {
             // If this hook implementation is stored in a lazy-loaded file, include
             // that file first.
             if ($group) {
                 $this->loadInclude($module, 'inc', "{$module}.{$group}");
             }
             // It is possible that a module removed a hook implementation without
             // the implementations cache being rebuilt yet, so we check whether the
             // function exists on each request to avoid undefined function errors.
             // Since ModuleHandler::implementsHook() may needlessly try to
             // load the include file again, function_exists() is used directly here.
             if (!function_exists($module . '_' . $hook)) {
                 // Clear out the stale implementation from the cache and force a cache
                 // refresh to forget about no longer existing hook implementations.
                 unset($this->implementations[$hook][$module]);
                 $this->cacheNeedsWriting = TRUE;
             }
         }
     }
     return $this->implementations[$hook];
 }
Esempio n. 20
0
 /**
  * Provides information about modules' implementations of a hook.
  *
  * @param string $hook
  *   The name of the hook (e.g. "help" or "menu").
  *
  * @return mixed[]
  *   An array whose keys are the names of the modules which are implementing
  *   this hook and whose values are either a string identifying a file in
  *   which the implementation is to be found, or FALSE, if the implementation
  *   is in the module file.
  */
 protected function getImplementationInfo($hook)
 {
     if (!isset($this->implementations)) {
         $this->implementations = array();
         $this->verified = array();
         if ($cache = $this->cacheBackend->get('module_implements')) {
             $this->implementations = $cache->data;
         }
     }
     if (!isset($this->implementations[$hook])) {
         // The hook is not cached, so ensure that whether or not it has
         // implementations, the cache is updated at the end of the request.
         $this->cacheNeedsWriting = TRUE;
         // Discover implementations.
         $this->implementations[$hook] = $this->buildImplementationInfo($hook);
         // Implementations are always "verified" as part of the discovery.
         $this->verified[$hook] = TRUE;
     } elseif (!isset($this->verified[$hook])) {
         if (!$this->verifyImplementations($this->implementations[$hook], $hook)) {
             // One or more of the implementations did not exist and need to be
             // removed in the cache.
             $this->cacheNeedsWriting = TRUE;
         }
         $this->verified[$hook] = TRUE;
     }
     return $this->implementations[$hook];
 }
 /**
  * {@inheritdoc}
  */
 public function getActiveThemeByName($theme_name)
 {
     if ($cached = $this->cache->get('theme.active_theme.' . $theme_name)) {
         return $cached->data;
     }
     $themes = $this->themeHandler->listInfo();
     // If no theme could be negotiated, or if the negotiated theme is not within
     // the list of installed themes, fall back to the default theme output of
     // core and modules (like Stark, but without a theme extension at all). This
     // is possible, because loadActiveTheme() always loads the Twig theme
     // engine. This is desired, because missing or malformed theme configuration
     // should not leave the application in a broken state. By falling back to
     // default output, the user is able to reconfigure the theme through the UI.
     // Lastly, tests are expected to operate with no theme by default, so as to
     // only assert the original theme output of modules (unless a test manually
     // installs a specific theme).
     if (empty($themes) || !$theme_name || !isset($themes[$theme_name])) {
         $theme_name = 'core';
         // /core/core.info.yml does not actually exist, but is required because
         // Extension expects a pathname.
         $active_theme = $this->getActiveTheme(new Extension($this->root, 'theme', 'core/core.info.yml'));
         // Early-return and do not set state, because the initialized $theme_name
         // differs from the original $theme_name.
         return $active_theme;
     }
     // Find all our ancestor themes and put them in an array.
     $base_themes = array();
     $ancestor = $theme_name;
     while ($ancestor && isset($themes[$ancestor]->base_theme)) {
         $ancestor = $themes[$ancestor]->base_theme;
         if (!$this->themeHandler->themeExists($ancestor)) {
             if ($ancestor == 'stable') {
                 // Themes that depend on Stable will be fixed by system_update_8014().
                 // There is no harm in not adding it as an ancestor since at worst
                 // some people might experience slight visual regressions on
                 // update.php.
                 continue;
             }
             throw new MissingThemeDependencyException(sprintf('Base theme %s has not been installed.', $ancestor), $ancestor);
         }
         $base_themes[] = $themes[$ancestor];
     }
     $active_theme = $this->getActiveTheme($themes[$theme_name], $base_themes);
     $this->cache->set('theme.active_theme.' . $theme_name, $active_theme);
     return $active_theme;
 }
Esempio n. 22
0
 /**
  * Gets the last write timestamp.
  */
 protected function getLastWriteTimestamp()
 {
     if ($this->lastWriteTimestamp === NULL) {
         $cache = $this->consistentBackend->get(self::LAST_WRITE_TIMESTAMP_PREFIX . $this->bin);
         $this->lastWriteTimestamp = $cache ? $cache->data : 0;
     }
     return $this->lastWriteTimestamp;
 }
Esempio n. 23
0
 /**
  * Discovers all available tests in all extensions.
  *
  * @param string $extension
  *   (optional) The name of an extension to limit discovery to; e.g., 'node'.
  *
  * @return array
  *   An array of tests keyed by the first @group specified in each test's
  *   PHPDoc comment block, and then keyed by class names. For example:
  *   @code
  *     $groups['block'] => array(
  *       'Drupal\block\Tests\BlockTest' => array(
  *         'name' => 'Drupal\block\Tests\BlockTest',
  *         'description' => 'Tests block UI CRUD functionality.',
  *         'group' => 'block',
  *       ),
  *     );
  *   @endcode
  *
  * @throws \ReflectionException
  *   If a discovered test class does not match the expected class name.
  *
  * @todo Remove singular grouping; retain list of groups in 'group' key.
  * @see https://www.drupal.org/node/2296615
  * @todo Add base class groups 'Kernel' + 'Web', complementing 'PHPUnit'.
  */
 public function getTestClasses($extension = NULL)
 {
     if (!isset($extension)) {
         if ($this->cacheBackend && ($cache = $this->cacheBackend->get('simpletest:discovery:classes'))) {
             return $cache->data;
         }
     }
     $list = array();
     $classmap = $this->findAllClassFiles($extension);
     // Prevent expensive class loader lookups for each reflected test class by
     // registering the complete classmap of test classes to the class loader.
     // This also ensures that test classes are loaded from the discovered
     // pathnames; a namespace/classname mismatch will throw an exception.
     $this->classLoader->addClassMap($classmap);
     foreach ($classmap as $classname => $pathname) {
         try {
             $class = new \ReflectionClass($classname);
         } catch (\ReflectionException $e) {
             // Re-throw with expected pathname.
             $message = $e->getMessage() . " in expected {$pathname}";
             throw new \ReflectionException($message, $e->getCode(), $e);
         }
         // Skip interfaces, abstract classes, and traits.
         if (!$class->isInstantiable()) {
             continue;
         }
         // Skip non-test classes.
         if (!$class->isSubclassOf('Drupal\\simpletest\\TestBase') && !$class->isSubclassOf('PHPUnit_Framework_TestCase')) {
             continue;
         }
         $info = static::getTestInfo($class);
         // Skip this test class if it requires unavailable modules.
         // @todo PHPUnit skips tests with unmet requirements when executing a test
         //   (instead of excluding them upfront). Refactor test runner to follow
         //   that approach.
         // @see https://www.drupal.org/node/1273478
         if (!empty($info['requires']['module'])) {
             if (array_diff($info['requires']['module'], $this->availableExtensions['module'])) {
                 continue;
             }
         }
         $list[$info['group']][$classname] = $info;
     }
     // Sort the groups and tests within the groups by name.
     uksort($list, 'strnatcasecmp');
     foreach ($list as &$tests) {
         uksort($tests, 'strnatcasecmp');
     }
     // Allow modules extending core tests to disable originals.
     \Drupal::moduleHandler()->alter('simpletest', $list);
     if (!isset($extension)) {
         if ($this->cacheBackend) {
             $this->cacheBackend->set('simpletest:discovery:classes', $list);
         }
     }
     return $list;
 }
Esempio n. 24
0
 /**
  * @covers ::getFieldMap
  */
 public function testGetFieldMapFromCache()
 {
     $expected = array('test_entity_type' => array('id' => array('type' => 'integer', 'bundles' => array('first_bundle' => 'first_bundle', 'second_bundle' => 'second_bundle')), 'by_bundle' => array('type' => 'string', 'bundles' => array('second_bundle' => 'second_bundle'))));
     $this->setUpEntityManager();
     $this->cacheBackend->get('entity_field_map')->willReturn((object) array('data' => $expected));
     // Call the field map twice to make sure the static cache works.
     $this->assertEquals($expected, $this->entityManager->getFieldMap());
     $this->assertEquals($expected, $this->entityManager->getFieldMap());
 }
Esempio n. 25
0
 /**
  * {@inheritdoc}
  */
 public function getTimestamp($key)
 {
     $cid = 'twig:' . $key;
     if ($cache = $this->cache->get($cid)) {
         return $cache->data;
     } else {
         return 0;
     }
 }
Esempio n. 26
0
 /**
  * Asserts a render cache item.
  *
  * @param string $cid
  *   The expected cache ID.
  * @param mixed $data
  *   The expected data for that cache ID.
  */
 protected function assertRenderCacheItem($cid, $data)
 {
     $cached = $this->memoryCache->get($cid);
     $this->assertNotFalse($cached, sprintf('Expected cache item "%s" exists.', $cid));
     if ($cached !== FALSE) {
         $this->assertEquals($data, $cached->data, sprintf('Cache item "%s" has the expected data.', $cid));
         $this->assertSame(Cache::mergeTags($data['#cache']['tags'], ['rendered']), $cached->tags, "The cache item's cache tags also has the 'rendered' cache tag.");
     }
 }
Esempio n. 27
0
 /**
  * Discovers all available tests in all extensions.
  *
  * @param string $extension
  *   (optional) The name of an extension to limit discovery to; e.g., 'node'.
  *
  * @return array
  *   An array of tests keyed by the first @group specified in each test's
  *   PHPDoc comment block, and then keyed by class names. For example:
  *   @code
  *     $groups['block'] => array(
  *       'Drupal\block\Tests\BlockTest' => array(
  *         'name' => 'Drupal\block\Tests\BlockTest',
  *         'description' => 'Tests block UI CRUD functionality.',
  *         'group' => 'block',
  *       ),
  *     );
  *   @endcode
  *
  * @throws \ReflectionException
  *   If a discovered test class does not match the expected class name.
  *
  * @todo Remove singular grouping; retain list of groups in 'group' key.
  * @see https://www.drupal.org/node/2296615
  * @todo Add base class groups 'Kernel' + 'Web', complementing 'PHPUnit'.
  */
 public function getTestClasses($extension = NULL)
 {
     $reader = new SimpleAnnotationReader();
     $reader->addNamespace('Drupal\\simpletest\\Annotation');
     if (!isset($extension)) {
         if ($this->cacheBackend && ($cache = $this->cacheBackend->get('simpletest:discovery:classes'))) {
             return $cache->data;
         }
     }
     $list = array();
     $classmap = $this->findAllClassFiles($extension);
     // Prevent expensive class loader lookups for each reflected test class by
     // registering the complete classmap of test classes to the class loader.
     // This also ensures that test classes are loaded from the discovered
     // pathnames; a namespace/classname mismatch will throw an exception.
     $this->classLoader->addClassMap($classmap);
     foreach ($classmap as $classname => $pathname) {
         $finder = MockFileFinder::create($pathname);
         $parser = new StaticReflectionParser($classname, $finder, TRUE);
         try {
             $info = static::getTestInfo($classname, $parser->getDocComment());
         } catch (MissingGroupException $e) {
             // If the class name ends in Test and is not a migrate table dump.
             if (preg_match('/Test$/', $classname) && strpos($classname, 'migrate_drupal\\Tests\\Table') === FALSE) {
                 throw $e;
             }
             // If the class is @group annotation just skip it. Most likely it is an
             // abstract class, trait or test fixture.
             continue;
         }
         // Skip this test class if it requires unavailable modules.
         // @todo PHPUnit skips tests with unmet requirements when executing a test
         //   (instead of excluding them upfront). Refactor test runner to follow
         //   that approach.
         // @see https://www.drupal.org/node/1273478
         if (!empty($info['requires']['module'])) {
             if (array_diff($info['requires']['module'], $this->availableExtensions['module'])) {
                 continue;
             }
         }
         $list[$info['group']][$classname] = $info;
     }
     // Sort the groups and tests within the groups by name.
     uksort($list, 'strnatcasecmp');
     foreach ($list as &$tests) {
         uksort($tests, 'strnatcasecmp');
     }
     // Allow modules extending core tests to disable originals.
     \Drupal::moduleHandler()->alter('simpletest', $list);
     if (!isset($extension)) {
         if ($this->cacheBackend) {
             $this->cacheBackend->set('simpletest:discovery:classes', $list);
         }
     }
     return $list;
 }
Esempio n. 28
0
 /**
  * Loads all non-admin routes right before the actual page is rendered.
  *
  * @param \Symfony\Component\HttpKernel\Event\KernelEvent $event
  *   The event to process.
  */
 public function onRequest(KernelEvent $event)
 {
     // Only preload on normal HTML pages, as they will display menu links.
     if ($this->routeProvider instanceof PreloadableRouteProviderInterface && $event->getRequest()->getRequestFormat() == 'html') {
         // Ensure that the state query is cached to skip the database query, if
         // possible.
         $key = 'routing.non_admin_routes';
         if ($cache = $this->cache->get($key)) {
             $routes = $cache->data;
         } else {
             $routes = $this->state->get($key, []);
             $this->cache->set($key, $routes, Cache::PERMANENT, ['routes']);
         }
         if ($routes) {
             // Preload all the non-admin routes at once.
             $this->routeProvider->preLoadRoutes($routes);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function loadDefinitions($countryCode, $parentId = NULL)
 {
     $lookup_id = $parentId ?: $countryCode;
     if (isset($this->definitions[$lookup_id])) {
         return $this->definitions[$lookup_id];
     }
     // If there are predefined subdivisions at this level, try to load them.
     $this->definitions[$lookup_id] = [];
     if ($this->hasData($countryCode, $parentId)) {
         $cache_key = 'address.subdivisions.' . $lookup_id;
         $filename = $this->definitionPath . $lookup_id . '.json';
         if ($cached = $this->cache->get($cache_key)) {
             $this->definitions[$lookup_id] = $cached->data;
         } elseif ($raw_definition = @file_get_contents($filename)) {
             $this->definitions[$lookup_id] = json_decode($raw_definition, TRUE);
             $this->cache->set($cache_key, $this->definitions[$lookup_id], CacheBackendInterface::CACHE_PERMANENT, ['subdivisions']);
         }
     }
     return $this->definitions[$lookup_id];
 }
Esempio n. 30
0
 /**
  * Loads the cache if not already done.
  */
 protected function lazyLoadCache()
 {
     if ($this->cacheLoaded) {
         return;
     }
     // The cache was not yet loaded, set flag to TRUE.
     $this->cacheLoaded = TRUE;
     if ($cache = $this->cache->get($this->getCid())) {
         $this->cacheCreated = $cache->created;
         $this->storage = $cache->data;
     }
 }