From 81a30d34ef4e249e6534176bcca0f1dabc92df2c Mon Sep 17 00:00:00 2001 From: catch Date: Sun, 28 Apr 2024 07:34:10 +0100 Subject: [PATCH] =?UTF-8?q?Revert=20"Issue=20#3443493=20by=20smustgrave,?= =?UTF-8?q?=20G=C3=A1bor=20Hojtsy,=20mikelutz:=20Remove=20deprecated=20cod?= =?UTF-8?q?e=20from=20lib/State=20and=20lib/Session"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit d494e6673ea12878d272f708192871c2e66329d3. --- .../Drupal/Core/Session/PermissionChecker.php | 8 ++- .../Core/Session/PermissionsHashGenerator.php | 59 ++++++++++++++++--- .../Drupal/Core/Session/SessionHandler.php | 30 +++++++--- .../Drupal/Core/Session/SessionManager.php | 52 +++++++++++++--- core/lib/Drupal/Core/Session/UserSession.php | 6 +- core/lib/Drupal/Core/State/State.php | 29 +++++++++ .../Core/State/LegacyStateTest.php | 31 ++++++++++ 7 files changed, 190 insertions(+), 25 deletions(-) create mode 100644 core/tests/Drupal/KernelTests/Core/State/LegacyStateTest.php diff --git a/core/lib/Drupal/Core/Session/PermissionChecker.php b/core/lib/Drupal/Core/Session/PermissionChecker.php index 4fb01b51519..e46b546bc8d 100644 --- a/core/lib/Drupal/Core/Session/PermissionChecker.php +++ b/core/lib/Drupal/Core/Session/PermissionChecker.php @@ -2,12 +2,18 @@ namespace Drupal\Core\Session; +use Drupal\Core\Entity\EntityTypeManagerInterface; + /** * Checks permissions for an account. */ class PermissionChecker implements PermissionCheckerInterface { - public function __construct(protected AccessPolicyProcessorInterface $processor) { + public function __construct(protected EntityTypeManagerInterface|AccessPolicyProcessorInterface $processor) { + if ($this->processor instanceof EntityTypeManagerInterface) { + @trigger_error('Calling ' . __METHOD__ . '() without the $processor argument is deprecated in drupal:10.3.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3402107', E_USER_DEPRECATED); + $this->processor = \Drupal::service('access_policy_processor'); + } } /** diff --git a/core/lib/Drupal/Core/Session/PermissionsHashGenerator.php b/core/lib/Drupal/Core/Session/PermissionsHashGenerator.php index 77b354b29c0..c15afc3ca1d 100644 --- a/core/lib/Drupal/Core/Session/PermissionsHashGenerator.php +++ b/core/lib/Drupal/Core/Session/PermissionsHashGenerator.php @@ -13,21 +13,47 @@ use Drupal\Core\Site\Settings; */ class PermissionsHashGenerator implements PermissionsHashGeneratorInterface { + /** + * The private key service. + * + * @var \Drupal\Core\PrivateKey + */ + protected $privateKey; + + /** + * The cache backend interface to use for the static cache. + * + * @var \Drupal\Core\Cache\CacheBackendInterface + */ + protected $static; + + /** + * The access policy processor. + * + * @var \Drupal\Core\Session\AccessPolicyProcessorInterface + */ + protected $processor; + /** * Constructs a PermissionsHashGenerator object. * - * @param \Drupal\Core\PrivateKey $privateKey + * @param \Drupal\Core\PrivateKey $private_key * The private key service. * @param \Drupal\Core\Cache\CacheBackendInterface $static * The cache backend interface to use for the static cache. - * @param \Drupal\Core\Session\AccessPolicyProcessorInterface $processor + * @param \Drupal\Core\Session\AccessPolicyProcessorInterface|\Drupal\Core\Cache\CacheBackendInterface $processor * The access policy processor. */ - public function __construct( - protected PrivateKey $privateKey, - protected CacheBackendInterface $static, - protected AccessPolicyProcessorInterface $processor, - ) { + public function __construct(PrivateKey $private_key, CacheBackendInterface $static, AccessPolicyProcessorInterface|CacheBackendInterface $processor) { + $this->privateKey = $private_key; + if ($processor instanceof CacheBackendInterface) { + @trigger_error('Calling ' . __METHOD__ . '() without the $processor argument is deprecated in drupal:10.3.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3402110', E_USER_DEPRECATED); + $this->static = $processor; + $this->processor = \Drupal::service('access_policy_processor'); + return; + } + $this->static = $static; + $this->processor = $processor; } /** @@ -74,6 +100,25 @@ class PermissionsHashGenerator implements PermissionsHashGeneratorInterface { return $hash; } + /** + * Generates a hash that uniquely identifies the user's permissions. + * + * @param string[] $roles + * The user's roles. + * + * @return string + * The permissions hash. + * + * @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. There is no + * replacement. + * + * @see https://www.drupal.org/node/3435842 + */ + protected function doGenerate(array $roles) { + @trigger_error(__METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3435842', E_USER_DEPRECATED); + return ''; + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Session/SessionHandler.php b/core/lib/Drupal/Core/Session/SessionHandler.php index 0c33bb8d691..f6fe3b28f58 100644 --- a/core/lib/Drupal/Core/Session/SessionHandler.php +++ b/core/lib/Drupal/Core/Session/SessionHandler.php @@ -17,21 +17,37 @@ class SessionHandler extends AbstractProxy implements \SessionHandlerInterface { use DependencySerializationTrait; + /** + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $requestStack; + + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + /** * Constructs a new SessionHandler instance. * - * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. * @param \Drupal\Core\Database\Connection $connection * The database connection. - * @param \Drupal\Component\Datetime\TimeInterface $time + * @param \Drupal\Component\Datetime\TimeInterface|null $time * The time service. */ - public function __construct( - protected RequestStack $requestStack, - protected Connection $connection, - protected TimeInterface $time, - ) { + public function __construct(RequestStack $request_stack, Connection $connection, protected ?TimeInterface $time = NULL) { + $this->requestStack = $request_stack; + $this->connection = $connection; + if (!$time) { + @trigger_error('Calling ' . __METHOD__ . '() without the $time argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3387233', E_USER_DEPRECATED); + $this->time = \Drupal::service(TimeInterface::class); + } } /** diff --git a/core/lib/Drupal/Core/Session/SessionManager.php b/core/lib/Drupal/Core/Session/SessionManager.php index cdcbfe14e1b..f43db06b29b 100644 --- a/core/lib/Drupal/Core/Session/SessionManager.php +++ b/core/lib/Drupal/Core/Session/SessionManager.php @@ -7,6 +7,7 @@ use Drupal\Core\Database\Connection; use Drupal\Core\DependencyInjection\DependencySerializationTrait; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; +use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy; /** * Manages user sessions. @@ -29,6 +30,27 @@ class SessionManager extends NativeSessionStorage implements SessionManagerInter use DependencySerializationTrait; + /** + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $requestStack; + + /** + * The database connection to use. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + + /** + * The session configuration. + * + * @var \Drupal\Core\Session\SessionConfigurationInterface + */ + protected $sessionConfiguration; + /** * Whether a lazy session has been started. * @@ -49,15 +71,15 @@ class SessionManager extends NativeSessionStorage implements SessionManagerInter /** * Constructs a new session manager instance. * - * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. * @param \Drupal\Core\Database\Connection $connection * The database connection. * @param \Drupal\Core\Session\MetadataBag $metadata_bag * The session metadata bag. - * @param \Drupal\Core\Session\SessionConfigurationInterface $sessionConfiguration + * @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration * The session configuration interface. - * @param \Drupal\Component\Datetime\TimeInterface $time + * @param \Drupal\Component\Datetime\TimeInterface|null|\Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy|\SessionHandlerInterface $time * The time service. * @param \Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy|\SessionHandlerInterface|null $handler * The object to register as a PHP session handler. @@ -65,14 +87,26 @@ class SessionManager extends NativeSessionStorage implements SessionManagerInter * @see \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage::setSaveHandler() */ public function __construct( - protected RequestStack $requestStack, - protected Connection $connection, - protected MetadataBag $metadata_bag, - protected SessionConfigurationInterface $sessionConfiguration, - protected TimeInterface $time, + RequestStack $request_stack, + Connection $connection, + MetadataBag $metadata_bag, + SessionConfigurationInterface $session_configuration, + protected TimeInterface|AbstractProxy|\SessionHandlerInterface|null $time = NULL, $handler = NULL, ) { - parent::__construct([], $handler, $metadata_bag); + $options = []; + $this->sessionConfiguration = $session_configuration; + $this->requestStack = $request_stack; + $this->connection = $connection; + if (!$time || $time instanceof AbstractProxy || $time instanceof \SessionHandlerInterface) { + @trigger_error('Calling ' . __METHOD__ . '() without the $time argument is deprecated in drupal:10.3.0 and it will be the 5th argument in drupal:11.0.0. See https://www.drupal.org/node/3387233', E_USER_DEPRECATED); + if ($time instanceof AbstractProxy || $time instanceof \SessionHandlerInterface) { + $handler = $time; + } + $this->time = \Drupal::service(TimeInterface::class); + } + + parent::__construct($options, $handler, $metadata_bag); } /** diff --git a/core/lib/Drupal/Core/Session/UserSession.php b/core/lib/Drupal/Core/Session/UserSession.php index db39b79bc47..91787c0ba1c 100644 --- a/core/lib/Drupal/Core/Session/UserSession.php +++ b/core/lib/Drupal/Core/Session/UserSession.php @@ -119,7 +119,11 @@ class UserSession implements AccountInterface { /** * {@inheritdoc} */ - public function hasPermission(string $permission) { + public function hasPermission(/* string */$permission) { + if (!is_string($permission)) { + @trigger_error('Calling ' . __METHOD__ . '() with a $permission parameter of type other than string is deprecated in drupal:10.3.0 and will cause an error in drupal:11.0.0. See https://www.drupal.org/node/3411485', E_USER_DEPRECATED); + return FALSE; + } return \Drupal::service('permission_checker')->hasPermission($permission, $this); } diff --git a/core/lib/Drupal/Core/State/State.php b/core/lib/Drupal/Core/State/State.php index 96f0834bbdd..aa905e91cf3 100644 --- a/core/lib/Drupal/Core/State/State.php +++ b/core/lib/Drupal/Core/State/State.php @@ -2,6 +2,7 @@ namespace Drupal\Core\State; +use Drupal\Core\Asset\AssetQueryString; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheCollector; use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; @@ -12,6 +13,22 @@ use Drupal\Core\Lock\LockBackendInterface; */ class State extends CacheCollector implements StateInterface { + /** + * Information about all deprecated state, keyed by legacy state key. + * + * Each entry should be an array that defines the following keys: + * - 'replacement': The new name for the state. + * - 'message': The deprecation message to use for trigger_error(). + * + * @var array + */ + private static array $deprecatedState = [ + 'system.css_js_query_string' => [ + 'replacement' => AssetQueryString::STATE_KEY, + 'message' => 'The \'system.css_js_query_string\' state is deprecated in drupal:10.2.0. Use \Drupal\Core\Asset\AssetQueryStringInterface::get() and ::reset() instead. See https://www.drupal.org/node/3358337.', + ], + ]; + /** * The key value store to use. * @@ -38,6 +55,13 @@ class State extends CacheCollector implements StateInterface { * {@inheritdoc} */ public function get($key, $default = NULL) { + // If the caller is asking for the value of a deprecated state, trigger a + // deprecation message about it. + if (isset(self::$deprecatedState[$key])) { + // phpcs:ignore Drupal.Semantics.FunctionTriggerError + @trigger_error(self::$deprecatedState[$key]['message'], E_USER_DEPRECATED); + $key = self::$deprecatedState[$key]['replacement']; + } return parent::get($key) ?? $default; } @@ -67,6 +91,11 @@ class State extends CacheCollector implements StateInterface { * {@inheritdoc} */ public function set($key, $value) { + if (isset(self::$deprecatedState[$key])) { + // phpcs:ignore Drupal.Semantics.FunctionTriggerError + @trigger_error(self::$deprecatedState[$key]['message'], E_USER_DEPRECATED); + $key = self::$deprecatedState[$key]['replacement']; + } $this->keyValueStore->set($key, $value); // If another request had a cache miss before this request, and also hasn't // written to cache yet, then it may already have read this value from the diff --git a/core/tests/Drupal/KernelTests/Core/State/LegacyStateTest.php b/core/tests/Drupal/KernelTests/Core/State/LegacyStateTest.php new file mode 100644 index 00000000000..fbd5d2ac173 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/State/LegacyStateTest.php @@ -0,0 +1,31 @@ +container->get('state'); + $this->expectDeprecation('The \'system.css_js_query_string\' state is deprecated in drupal:10.2.0. Use \Drupal\Core\Asset\AssetQueryStringInterface::get() and ::reset() instead. See https://www.drupal.org/node/3358337.'); + $state->set('system.css_js_query_string', 'foo'); + $this->expectDeprecation('The \'system.css_js_query_string\' state is deprecated in drupal:10.2.0. Use \Drupal\Core\Asset\AssetQueryStringInterface::get() and ::reset() instead. See https://www.drupal.org/node/3358337.'); + $this->assertEquals('foo', $state->get('system.css_js_query_string')); + $this->assertEquals('foo', \Drupal::service('asset.query_string')->get()); + } + +}