class TestDiscoveryCacheContext

Defines the TestDiscoveryCacheContext service.

Cache context ID: 'test_discovery'.

Hierarchy

Expanded class hierarchy of TestDiscoveryCacheContext

1 file declares its use of TestDiscoveryCacheContext
TestDiscoveryCacheContextTest.php in core/modules/simpletest/tests/src/Kernel/Cache/Context/TestDiscoveryCacheContextTest.php
1 string reference to 'TestDiscoveryCacheContext'
simpletest.services.yml in core/modules/simpletest/simpletest.services.yml
core/modules/simpletest/simpletest.services.yml
1 service uses TestDiscoveryCacheContext
cache_context.test_discovery in core/modules/simpletest/simpletest.services.yml
Drupal\simpletest\Cache\Context\TestDiscoveryCacheContext

File

core/modules/simpletest/src/Cache/Context/TestDiscoveryCacheContext.php, line 16

Namespace

Drupal\simpletest\Cache\Context
View source
class TestDiscoveryCacheContext implements CacheContextInterface {
    
    /**
     * The test discovery service.
     *
     * @var \Drupal\simpletest\TestDiscovery
     */
    protected $testDiscovery;
    
    /**
     * The private key service.
     *
     * @var \Drupal\Core\PrivateKey
     */
    protected $privateKey;
    
    /**
     * The hash of discovered test information.
     *
     * Services should not be stateful, but we only keep this information per
     * request. That way we don't perform a file scan every time we need this
     * hash. The test scan results are unlikely to change during the request.
     *
     * @var string
     */
    protected $hash;
    
    /**
     * Construct a test discovery cache context.
     *
     * @param \Drupal\simpletest\TestDiscovery $test_discovery
     *   The test discovery service.
     * @param \Drupal\Core\PrivateKey $private_key
     *   The private key service.
     */
    public function __construct(TestDiscovery $test_discovery, PrivateKey $private_key) {
        $this->testDiscovery = $test_discovery;
        $this->privateKey = $private_key;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getLabel() {
        return t('Test discovery');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContext() {
        if (empty($this->hash)) {
            $tests = $this->testDiscovery
                ->getTestClasses();
            $this->hash = $this->hash(serialize($tests));
        }
        return $this->hash;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheableMetadata() {
        return new CacheableMetadata();
    }
    
    /**
     * Hashes the given string.
     *
     * @param string $identifier
     *   The string to be hashed.
     *
     * @return string
     *   The hash.
     */
    protected function hash($identifier) {
        return hash('sha256', $this->privateKey
            ->get() . Settings::getHashSalt() . $identifier);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
TestDiscoveryCacheContext::$hash protected property The hash of discovered test information.
TestDiscoveryCacheContext::$privateKey protected property The private key service.
TestDiscoveryCacheContext::$testDiscovery protected property The test discovery service.
TestDiscoveryCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
TestDiscoveryCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
TestDiscoveryCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
TestDiscoveryCacheContext::hash protected function Hashes the given string.
TestDiscoveryCacheContext::__construct public function Construct a test discovery cache context.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.