Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\simpletest\Cache\Context;
|
Chris@17
|
4
|
Chris@17
|
5 use Drupal\Core\Cache\CacheableMetadata;
|
Chris@17
|
6 use Drupal\Core\Cache\Context\CacheContextInterface;
|
Chris@17
|
7 use Drupal\Core\PrivateKey;
|
Chris@17
|
8 use Drupal\Core\Site\Settings;
|
Chris@17
|
9 use Drupal\simpletest\TestDiscovery;
|
Chris@17
|
10
|
Chris@17
|
11 /**
|
Chris@17
|
12 * Defines the TestDiscoveryCacheContext service.
|
Chris@17
|
13 *
|
Chris@17
|
14 * Cache context ID: 'test_discovery'.
|
Chris@17
|
15 */
|
Chris@17
|
16 class TestDiscoveryCacheContext implements CacheContextInterface {
|
Chris@17
|
17
|
Chris@17
|
18 /**
|
Chris@17
|
19 * The test discovery service.
|
Chris@17
|
20 *
|
Chris@17
|
21 * @var \Drupal\simpletest\TestDiscovery
|
Chris@17
|
22 */
|
Chris@17
|
23 protected $testDiscovery;
|
Chris@17
|
24
|
Chris@17
|
25 /**
|
Chris@17
|
26 * The private key service.
|
Chris@17
|
27 *
|
Chris@17
|
28 * @var \Drupal\Core\PrivateKey
|
Chris@17
|
29 */
|
Chris@17
|
30 protected $privateKey;
|
Chris@17
|
31
|
Chris@17
|
32 /**
|
Chris@17
|
33 * The hash of discovered test information.
|
Chris@17
|
34 *
|
Chris@17
|
35 * Services should not be stateful, but we only keep this information per
|
Chris@17
|
36 * request. That way we don't perform a file scan every time we need this
|
Chris@17
|
37 * hash. The test scan results are unlikely to change during the request.
|
Chris@17
|
38 *
|
Chris@17
|
39 * @var string
|
Chris@17
|
40 */
|
Chris@17
|
41 protected $hash;
|
Chris@17
|
42
|
Chris@17
|
43 /**
|
Chris@17
|
44 * Construct a test discovery cache context.
|
Chris@17
|
45 *
|
Chris@17
|
46 * @param \Drupal\simpletest\TestDiscovery $test_discovery
|
Chris@17
|
47 * The test discovery service.
|
Chris@17
|
48 * @param \Drupal\Core\PrivateKey $private_key
|
Chris@17
|
49 * The private key service.
|
Chris@17
|
50 */
|
Chris@17
|
51 public function __construct(TestDiscovery $test_discovery, PrivateKey $private_key) {
|
Chris@17
|
52 $this->testDiscovery = $test_discovery;
|
Chris@17
|
53 $this->privateKey = $private_key;
|
Chris@17
|
54 }
|
Chris@17
|
55
|
Chris@17
|
56 /**
|
Chris@17
|
57 * {@inheritdoc}
|
Chris@17
|
58 */
|
Chris@17
|
59 public static function getLabel() {
|
Chris@17
|
60 return t('Test discovery');
|
Chris@17
|
61 }
|
Chris@17
|
62
|
Chris@17
|
63 /**
|
Chris@17
|
64 * {@inheritdoc}
|
Chris@17
|
65 */
|
Chris@17
|
66 public function getContext() {
|
Chris@17
|
67 if (empty($this->hash)) {
|
Chris@17
|
68 $tests = $this->testDiscovery->getTestClasses();
|
Chris@17
|
69 $this->hash = $this->hash(serialize($tests));
|
Chris@17
|
70 }
|
Chris@17
|
71 return $this->hash;
|
Chris@17
|
72 }
|
Chris@17
|
73
|
Chris@17
|
74 /**
|
Chris@17
|
75 * {@inheritdoc}
|
Chris@17
|
76 */
|
Chris@17
|
77 public function getCacheableMetadata() {
|
Chris@17
|
78 return new CacheableMetadata();
|
Chris@17
|
79 }
|
Chris@17
|
80
|
Chris@17
|
81 /**
|
Chris@17
|
82 * Hashes the given string.
|
Chris@17
|
83 *
|
Chris@17
|
84 * @param string $identifier
|
Chris@17
|
85 * The string to be hashed.
|
Chris@17
|
86 *
|
Chris@17
|
87 * @return string
|
Chris@17
|
88 * The hash.
|
Chris@17
|
89 */
|
Chris@17
|
90 protected function hash($identifier) {
|
Chris@17
|
91 return hash('sha256', $this->privateKey->get() . Settings::getHashSalt() . $identifier);
|
Chris@17
|
92 }
|
Chris@17
|
93
|
Chris@17
|
94 }
|