class PrivateKey

Same name in other branches
  1. 9 core/lib/Drupal/Core/PrivateKey.php \Drupal\Core\PrivateKey
  2. 10 core/lib/Drupal/Core/PrivateKey.php \Drupal\Core\PrivateKey
  3. 11.x core/lib/Drupal/Core/PrivateKey.php \Drupal\Core\PrivateKey

Manages the Drupal private key.

Hierarchy

Expanded class hierarchy of PrivateKey

7 files declare their use of PrivateKey
CsrfTokenGenerator.php in core/lib/Drupal/Core/Access/CsrfTokenGenerator.php
IFrameUrlHelper.php in core/modules/media/src/IFrameUrlHelper.php
IFrameUrlHelperTest.php in core/modules/media/tests/src/Unit/IFrameUrlHelperTest.php
PermissionsHashGenerator.php in core/lib/Drupal/Core/Session/PermissionsHashGenerator.php
PrivateKeyTest.php in core/tests/Drupal/Tests/Core/PrivateKeyTest.php

... See full list

1 string reference to 'PrivateKey'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses PrivateKey
private_key in core/core.services.yml
Drupal\Core\PrivateKey

File

core/lib/Drupal/Core/PrivateKey.php, line 11

Namespace

Drupal\Core
View source
class PrivateKey {
    
    /**
     * The state service.
     *
     * @var \Drupal\Core\State\StateInterface
     */
    protected $state;
    
    /**
     * Constructs the private key object.
     *
     * @param \Drupal\Core\State\StateInterface $state
     *   The state service.
     */
    public function __construct(StateInterface $state) {
        $this->state = $state;
    }
    
    /**
     * Gets the private key.
     *
     * @return string
     *   The private key.
     */
    public function get() {
        if (!($key = $this->state
            ->get('system.private_key'))) {
            $key = $this->create();
            $this->set($key);
        }
        return $key;
    }
    
    /**
     * Sets the private key.
     *
     * @param string $key
     *   The private key to set.
     */
    public function set($key) {
        return $this->state
            ->set('system.private_key', $key);
    }
    
    /**
     * Creates a new private key.
     *
     * @return string
     *   The private key.
     */
    protected function create() {
        return Crypt::randomBytesBase64(55);
    }

}

Members

Title Sort descending Modifiers Object type Summary
PrivateKey::$state protected property The state service.
PrivateKey::create protected function Creates a new private key.
PrivateKey::get public function Gets the private key.
PrivateKey::set public function Sets the private key.
PrivateKey::__construct public function Constructs the private key object.

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