Same filename and directory in other branches
- 8.9.x core/modules/system/tests/modules/service_provider_test/src/TestClass.php
- 9 core/modules/system/tests/modules/service_provider_test/src/TestClass.php
Namespace
Drupal\service_provider_test
File
core/modules/system/tests/modules/service_provider_test/src/TestClass.php
View source
<?php
namespace Drupal\service_provider_test;
use Drupal\Core\State\StateInterface;
use Drupal\Core\DestructableInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class TestClass implements EventSubscriberInterface, DestructableInterface {
protected $state;
public function __construct(StateInterface $state) {
$this->state = $state;
}
public function onKernelRequestTest(RequestEvent $event) {
\Drupal::messenger()
->addStatus(t('The service_provider_test event subscriber fired!'));
}
public function onKernelResponseTest(ResponseEvent $event) {
$container = \Drupal::getContainer();
if ($container
->hasParameter('container_rebuild_indicator')) {
$event
->getResponse()->headers
->set('container_rebuild_indicator', $container
->getParameter('container_rebuild_indicator'));
}
if ($container
->hasParameter('container_rebuild_test_parameter')) {
$event
->getResponse()->headers
->set('container_rebuild_test_parameter', $container
->getParameter('container_rebuild_test_parameter'));
}
}
public static function getSubscribedEvents() : array {
$events[KernelEvents::REQUEST][] = [
'onKernelRequestTest',
];
$events[KernelEvents::RESPONSE][] = [
'onKernelResponseTest',
];
return $events;
}
public function destruct() {
$this->state
->set('service_provider_test.destructed', TRUE);
}
}
Classes