comparison core/lib/Drupal/Core/PrivateKey.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core;
4
5 use Drupal\Core\State\StateInterface;
6 use Drupal\Component\Utility\Crypt;
7
8 /**
9 * Manages the Drupal private key.
10 */
11 class PrivateKey {
12
13 /**
14 * The state service.
15 *
16 * @var \Drupal\Core\State\StateInterface
17 */
18 protected $state;
19
20 /**
21 * Constructs the token generator.
22 *
23 * @param \Drupal\Core\State\StateInterface $state
24 * The state service.
25 */
26 public function __construct(StateInterface $state) {
27 $this->state = $state;
28 }
29
30 /**
31 * Gets the private key.
32 *
33 * @return string
34 * The private key.
35 */
36 public function get() {
37 if (!$key = $this->state->get('system.private_key')) {
38 $key = $this->create();
39 $this->set($key);
40 }
41
42 return $key;
43 }
44
45 /**
46 * Sets the private key.
47 *
48 * @param string $key
49 * The private key to set.
50 */
51 public function set($key) {
52 return $this->state->set('system.private_key', $key);
53 }
54
55 /**
56 * Creates a new private key.
57 *
58 * @return string
59 * The private key.
60 */
61 protected function create() {
62 return Crypt::randomBytesBase64(55);
63 }
64
65 }