diff --git a/_config/requestprocessors.yml b/_config/requestprocessors.yml index 7f11a8b7cce..d2e70eedd01 100644 --- a/_config/requestprocessors.yml +++ b/_config/requestprocessors.yml @@ -11,6 +11,7 @@ SilverStripe\Core\Injector\Injector: SessionMiddleware: '%$SilverStripe\Control\Middleware\SessionMiddleware' RequestProcessorMiddleware: '%$SilverStripe\Control\RequestProcessor' FlushMiddleware: '%$SilverStripe\Control\Middleware\FlushMiddleware' + EventsMiddleware: '%$SilverStripe\Control\Middleware\RegisteredEventsMiddleware' SilverStripe\Control\Middleware\AllowedHostsMiddleware: properties: AllowedHosts: '`SS_ALLOWED_HOSTS`' diff --git a/src/Control/Middleware/RegisteredEventsMiddleware.php b/src/Control/Middleware/RegisteredEventsMiddleware.php new file mode 100644 index 00000000000..8bb7c0696c7 --- /dev/null +++ b/src/Control/Middleware/RegisteredEventsMiddleware.php @@ -0,0 +1,80 @@ +events[$name] = $event; + } else { + $this->events[] = $event; + } + } + + /** + * Check if a named event is registered + * + * @param string $name + * @return bool + */ + public function hasEvent($name) + { + return !is_numeric($name) && isset($this->events[$name]); + } + + /** + * Unregister a named event + * + * @param string $name + */ + public function unregisterEvent($name) + { + if (is_numeric($name)) { + throw new InvalidArgumentException("Only named events can be unregistered"); + } + unset($this->events[$name]); + } + + /** + * Generate response for the given request + * + * @param HTTPRequest $request + * @param callable $delegate + * @return HTTPResponse + */ + public function process(HTTPRequest $request, callable $delegate) + { + // Invoke all registered events + foreach ($this->events as $event) { + $event($request); + } + return $delegate($request); + } +}