HoldTestSubscriber.php in Drupal 10
File
core/modules/system/tests/modules/hold_test/src/EventSubscriber/HoldTestSubscriber.php
View source
<?php
namespace Drupal\hold_test\EventSubscriber;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class HoldTestSubscriber implements EventSubscriberInterface {
const HOLD_REQUEST = 'request';
const HOLD_RESPONSE = 'response';
const WAIT = 100000;
protected $sitePath;
public function __construct(string $site_path) {
$this->sitePath = $site_path;
}
public function onRequest() {
$this
->hold(static::HOLD_REQUEST);
}
public function onRespond() {
$this
->hold(static::HOLD_RESPONSE);
}
protected function hold($type) {
$path = "{$this->sitePath}/hold_test_{$type}.txt";
do {
$status = (bool) file_get_contents($path);
} while ($status && NULL === usleep(static::WAIT));
}
public static function getSubscribedEvents() : array {
$events[KernelEvents::REQUEST][] = [
'onRequest',
];
$events[KernelEvents::RESPONSE][] = [
'onRespond',
];
return $events;
}
}