3 namespace Drupal\simpletest\Cache\Context;
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Cache\Context\CacheContextInterface;
7 use Drupal\Core\PrivateKey;
8 use Drupal\Core\Site\Settings;
9 use Drupal\simpletest\TestDiscovery;
12 * Defines the TestDiscoveryCacheContext service.
14 * Cache context ID: 'test_discovery'.
16 class TestDiscoveryCacheContext implements CacheContextInterface {
19 * The test discovery service.
21 * @var \Drupal\simpletest\TestDiscovery
23 protected $testDiscovery;
26 * The private key service.
28 * @var \Drupal\Core\PrivateKey
30 protected $privateKey;
33 * The hash of discovered test information.
35 * Services should not be stateful, but we only keep this information per
36 * request. That way we don't perform a file scan every time we need this
37 * hash. The test scan results are unlikely to change during the request.
44 * Construct a test discovery cache context.
46 * @param \Drupal\simpletest\TestDiscovery $test_discovery
47 * The test discovery service.
48 * @param \Drupal\Core\PrivateKey $private_key
49 * The private key service.
51 public function __construct(TestDiscovery $test_discovery, PrivateKey $private_key) {
52 $this->testDiscovery = $test_discovery;
53 $this->privateKey = $private_key;
59 public static function getLabel() {
60 return t('Test discovery');
66 public function getContext() {
67 if (empty($this->hash)) {
68 $tests = $this->testDiscovery->getTestClasses();
69 $this->hash = $this->hash(serialize($tests));
77 public function getCacheableMetadata() {
78 return new CacheableMetadata();
82 * Hashes the given string.
84 * @param string $identifier
85 * The string to be hashed.
90 protected function hash($identifier) {
91 return hash('sha256', $this->privateKey->get() . Settings::getHashSalt() . $identifier);