Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / simpletest / src / Cache / Context / TestDiscoveryCacheContext.php
1 <?php
2
3 namespace Drupal\simpletest\Cache\Context;
4
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;
10
11 /**
12  * Defines the TestDiscoveryCacheContext service.
13  *
14  * Cache context ID: 'test_discovery'.
15  */
16 class TestDiscoveryCacheContext implements CacheContextInterface {
17
18   /**
19    * The test discovery service.
20    *
21    * @var \Drupal\simpletest\TestDiscovery
22    */
23   protected $testDiscovery;
24
25   /**
26    * The private key service.
27    *
28    * @var \Drupal\Core\PrivateKey
29    */
30   protected $privateKey;
31
32   /**
33    * The hash of discovered test information.
34    *
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.
38    *
39    * @var string
40    */
41   protected $hash;
42
43   /**
44    * Construct a test discovery cache context.
45    *
46    * @param \Drupal\simpletest\TestDiscovery $test_discovery
47    *   The test discovery service.
48    * @param \Drupal\Core\PrivateKey $private_key
49    *   The private key service.
50    */
51   public function __construct(TestDiscovery $test_discovery, PrivateKey $private_key) {
52     $this->testDiscovery = $test_discovery;
53     $this->privateKey = $private_key;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public static function getLabel() {
60     return t('Test discovery');
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function getContext() {
67     if (empty($this->hash)) {
68       $tests = $this->testDiscovery->getTestClasses();
69       $this->hash = $this->hash(serialize($tests));
70     }
71     return $this->hash;
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function getCacheableMetadata() {
78     return new CacheableMetadata();
79   }
80
81   /**
82    * Hashes the given string.
83    *
84    * @param string $identifier
85    *   The string to be hashed.
86    *
87    * @return string
88    *   The hash.
89    */
90   protected function hash($identifier) {
91     return hash('sha256', $this->privateKey->get() . Settings::getHashSalt() . $identifier);
92   }
93
94 }