Commit 89d0409f authored by catch's avatar catch
Browse files

Issue #2928994 by Berdir, alexpott, JeroenT, markcarver: Remove...

Issue #2928994 by Berdir, alexpott, JeroenT, markcarver: Remove \Drupal\Core\Messenger\LegacyMessenger
parent 4098d2d5
......@@ -6,7 +6,6 @@
*/
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
use Drupal\Core\Messenger\LegacyMessenger;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -699,9 +698,7 @@ public static function time() {
* The messenger.
*/
public static function messenger() {
// @todo Replace with service once LegacyMessenger is removed in 9.0.0.
// @see https://www.drupal.org/node/2928994
return new LegacyMessenger();
return static::getContainer()->get('messenger');
}
}
......@@ -844,6 +844,7 @@ protected function getKernelParameters() {
protected function initializeContainer() {
$this->containerNeedsDumping = FALSE;
$session_started = FALSE;
$all_messages = [];
if (isset($this->container)) {
// Save the id of the currently logged in user.
if ($this->container->initialized('current_user')) {
......@@ -859,6 +860,8 @@ protected function initializeContainer() {
}
unset($session);
}
$all_messages = $this->container->get('messenger')->all();
}
// If we haven't booted yet but there is a container, then we're asked to
......@@ -919,6 +922,13 @@ protected function initializeContainer() {
$this->container->get('current_user')->setInitialAccountId($current_user_id);
}
// Re-add messages.
foreach ($all_messages as $type => $messages) {
foreach ($messages as $message) {
$this->container->get('messenger')->addMessage($message, $type);
}
}
\Drupal::setContainer($this->container);
// Allow other parts of the codebase to react on container initialization in
......
<?php
namespace Drupal\Core\Messenger;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Render\Markup;
/**
* Provides a LegacyMessenger implementation.
*
* This implementation is for handling messages in a backwards compatible way
* using core's previous $_SESSION storage method.
*
* You should not instantiate a new instance of this class directly. Instead,
* you should inject the "messenger" service into your own services or use
* \Drupal::messenger() in procedural functions.
*
* @see https://www.drupal.org/node/2774931
* @see https://www.drupal.org/node/2928994
*
* @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0.
* Use \Drupal\Core\Messenger\Messenger instead.
*/
class LegacyMessenger implements MessengerInterface {
/**
* The messages.
*
* Note: this property must remain static because it must behave in a
* persistent manner, similar to $_SESSION['messages']. Creating a new class
* each time would destroy any previously set messages.
*
* @var array
*/
protected static $messages;
/**
* {@inheritdoc}
*/
public function addError($message, $repeat = FALSE) {
return $this->addMessage($message, static::TYPE_ERROR, $repeat);
}
/**
* {@inheritdoc}
*/
public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {
// Proxy to the Messenger service, if it exists.
if ($messenger = $this->getMessengerService()) {
return $messenger->addMessage($message, $type, $repeat);
}
if (!isset(static::$messages[$type])) {
static::$messages[$type] = [];
}
if (!($message instanceof Markup) && $message instanceof MarkupInterface) {
$message = Markup::create((string) $message);
}
// Do not use strict type checking so that equivalent string and
// MarkupInterface objects are detected.
if ($repeat || !in_array($message, static::$messages[$type])) {
static::$messages[$type][] = $message;
}
return $this;
}
/**
* {@inheritdoc}
*/
public function addStatus($message, $repeat = FALSE) {
return $this->addMessage($message, static::TYPE_STATUS, $repeat);
}
/**
* {@inheritdoc}
*/
public function addWarning($message, $repeat = FALSE) {
return $this->addMessage($message, static::TYPE_WARNING, $repeat);
}
/**
* {@inheritdoc}
*/
public function all() {
// Proxy to the Messenger service, if it exists.
if ($messenger = $this->getMessengerService()) {
return $messenger->all();
}
return static::$messages;
}
/**
* Returns the Messenger service.
*
* @return \Drupal\Core\Messenger\MessengerInterface|null
* The Messenger service.
*/
protected function getMessengerService() {
// Use the Messenger service, if it exists.
if (\Drupal::hasService('messenger')) {
// Note: because the container has the potential to be rebuilt during
// requests, this service cannot be directly stored on this class.
/** @var \Drupal\Core\Messenger\MessengerInterface $messenger */
$messenger = \Drupal::service('messenger');
// Transfer any messages into the service.
if (isset(static::$messages)) {
foreach (static::$messages as $type => $messages) {
foreach ($messages as $message) {
// Force repeat to TRUE since this is merging existing messages to
// the Messenger service and would have already checked this prior.
$messenger->addMessage($message, $type, TRUE);
}
}
static::$messages = NULL;
}
return $messenger;
}
// Otherwise, trigger an error.
@trigger_error('Adding or retrieving messages prior to the container being initialized was deprecated in Drupal 8.5.0 and this functionality will be removed before Drupal 9.0.0. Please report this usage at https://www.drupal.org/node/2928994.', E_USER_DEPRECATED);
// Prematurely creating $_SESSION['messages'] in this class' constructor
// causes issues when the container attempts to initialize its own session
// later down the road. This can only be done after it has been determined
// the Messenger service is not available (i.e. no container). It is also
// reasonable to assume that if the container becomes available in a
// subsequent request, a new instance of this class will be created and
// this code will never be reached. This is merely for BC purposes.
if (!isset(static::$messages)) {
// A "session" was already created, perhaps to simply allow usage of
// the previous method core used to store messages, use it.
if (isset($_SESSION)) {
if (!isset($_SESSION['messages'])) {
$_SESSION['messages'] = [];
}
static::$messages = &$_SESSION['messages'];
}
// Otherwise, just set an empty array.
else {
static::$messages = [];
}
}
}
/**
* {@inheritdoc}
*/
public function messagesByType($type) {
// Proxy to the Messenger service, if it exists.
if ($messenger = $this->getMessengerService()) {
return $messenger->messagesByType($type);
}
return static::$messages[$type];
}
/**
* {@inheritdoc}
*/
public function deleteAll() {
// Proxy to the Messenger service, if it exists.
if ($messenger = $this->getMessengerService()) {
return $messenger->deleteAll();
}
$messages = static::$messages;
static::$messages = NULL;
return $messages;
}
/**
* {@inheritdoc}
*/
public function deleteByType($type) {
// Proxy to the Messenger service, if it exists.
if ($messenger = $this->getMessengerService()) {
return $messenger->deleteByType($type);
}
$messages = static::$messages[$type];
unset(static::$messages[$type]);
return $messages;
}
}
......@@ -193,6 +193,9 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
return;
}
// Unset the messenger to make sure that we'll get the service from the
// new container.
$this->messenger = NULL;
$module_names = array_values($this->modules['install']);
$this->messenger()->addStatus($this->formatPlural(count($module_names), 'Module %name has been enabled.', '@count modules have been enabled: %names.', [
'%name' => $module_names[0],
......
......@@ -135,6 +135,7 @@ function system_test_filetransfer_info() {
* Implements hook_module_preinstall().
*/
function system_test_module_preinstall($module) {
\Drupal::messenger()->addStatus('system_test_preinstall_module called');
\Drupal::state()->set('system_test_preinstall_module', $module);
}
......
......@@ -49,6 +49,15 @@ public function testDrupalMessengerService() {
// Ensure that strings that are not marked as safe are escaped.
$this->assertEscaped('<em>This<span>markup will be</span> escaped</em>.');
// Ensure messages survive a container rebuild.
$assert = $this->assertSession();
$this->drupalLogin($this->rootUser);
$edit = [];
$edit["modules[help][enable]"] = TRUE;
$this->drupalPostForm('admin/modules', $edit, t('Install'));
$assert->pageTextContains('Help has been enabled');
$assert->pageTextContains('system_test_preinstall_module called');
}
}
<?php
namespace Drupal\KernelTests\Core\Messenger;
use Drupal\Core\Messenger\LegacyMessenger;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\KernelTests\KernelTestBase;
/**
* @group Messenger
* @group legacy
*
* @coversDefaultClass \Drupal\Core\Messenger\LegacyMessenger
*
* Normally this test class would be named LegacyMessengerTest, but test classes
* starting with 'Legacy' are treated as belonging to group legacy. We want to
* explicitly use group annotation for consistency with other legacy tests.
*
* @see https://www.drupal.org/node/2931598#comment-12395743
* @see https://www.drupal.org/node/2774931
*/
class MessengerLegacyTest extends KernelTestBase {
/**
* Retrieves the Messenger service from LegacyMessenger.
*
* @param \Drupal\Core\Messenger\LegacyMessenger $legacy_messenger
* The legacy messenger.
*
* @return \Drupal\Core\Messenger\MessengerInterface|null
* A messenger implementation.
*/
protected function getMessengerService(LegacyMessenger $legacy_messenger) {
$method = new \ReflectionMethod($legacy_messenger, 'getMessengerService');
$method->setAccessible(TRUE);
return $method->invoke($legacy_messenger);
}
/**
* @covers \Drupal::messenger
* @covers ::getMessengerService
* @covers ::all
* @covers ::addMessage
* @covers ::addError
* @covers ::addStatus
* @covers ::addWarning
*
* @expectedDeprecation Adding or retrieving messages prior to the container being initialized was deprecated in Drupal 8.5.0 and this functionality will be removed before Drupal 9.0.0. Please report this usage at https://www.drupal.org/node/2928994.
*/
public function testMessages() {
// Save the current container for later use.
$container = \Drupal::getContainer();
// Unset the container to mimic not having one.
\Drupal::unsetContainer();
/** @var \Drupal\Core\Messenger\LegacyMessenger $messenger */
// Verify that the Messenger service doesn't exists.
$messenger = \Drupal::messenger();
$this->assertNull($this->getMessengerService($messenger));
// Add messages.
$messenger->addMessage('Foobar', 'custom');
$messenger->addMessage('Foobar', 'custom', TRUE);
$messenger->addError('Foo');
$messenger->addError('Foo', TRUE);
// Verify that retrieving another instance and adding more messages works.
$messenger = \Drupal::messenger();
$messenger->addStatus('Bar');
$messenger->addStatus('Bar', TRUE);
$messenger->addWarning('Fiz');
$messenger->addWarning('Fiz', TRUE);
// Restore the container.
\Drupal::setContainer($container);
// Verify that the Messenger service exists.
$messenger = \Drupal::messenger();
$this->assertInstanceOf(Messenger::class, $this->getMessengerService($messenger));
// Add more messages.
$messenger->addMessage('Platypus', 'custom');
$messenger->addMessage('Platypus', 'custom', TRUE);
$messenger->addError('Rhinoceros');
$messenger->addError('Rhinoceros', TRUE);
$messenger->addStatus('Giraffe');
$messenger->addStatus('Giraffe', TRUE);
$messenger->addWarning('Cheetah');
$messenger->addWarning('Cheetah', TRUE);
// Verify all messages added via LegacyMessenger are accounted for.
$messages = $messenger->all();
$this->assertContains('Foobar', $messages['custom']);
$this->assertContains('Foo', $messages[MessengerInterface::TYPE_ERROR]);
$this->assertContains('Bar', $messages[MessengerInterface::TYPE_STATUS]);
$this->assertContains('Fiz', $messages[MessengerInterface::TYPE_WARNING]);
// Verify all messages added via Messenger service are accounted for.
$this->assertContains('Platypus', $messages['custom']);
$this->assertContains('Rhinoceros', $messages[MessengerInterface::TYPE_ERROR]);
$this->assertContains('Giraffe', $messages[MessengerInterface::TYPE_STATUS]);
$this->assertContains('Cheetah', $messages[MessengerInterface::TYPE_WARNING]);
// Verify repeat counts.
$this->assertCount(4, $messages['custom']);
$this->assertCount(4, $messages[MessengerInterface::TYPE_STATUS]);
$this->assertCount(4, $messages[MessengerInterface::TYPE_WARNING]);
$this->assertCount(4, $messages[MessengerInterface::TYPE_ERROR]);
// Test deleteByType().
$this->assertCount(4, $messenger->deleteByType(MessengerInterface::TYPE_WARNING));
$this->assertCount(0, $messenger->messagesByType(MessengerInterface::TYPE_WARNING));
$this->assertCount(4, $messenger->messagesByType(MessengerInterface::TYPE_ERROR));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment