2012-05-09 14:26:29 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Control;
|
|
|
|
|
2017-06-25 05:12:29 +02:00
|
|
|
use SilverStripe\Control\Middleware\HTTPMiddleware;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
2017-06-23 01:17:30 +02:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
2017-06-23 01:17:30 +02:00
|
|
|
* Middleware that provides back-support for the deprecated RequestFilter API.
|
2017-06-25 05:12:29 +02:00
|
|
|
*
|
2018-09-28 10:46:36 +02:00
|
|
|
* @deprecated 4.0.0:5.0.0 Use HTTPMiddleware directly instead.
|
2012-05-09 14:26:29 +02:00
|
|
|
*/
|
2017-06-23 01:17:30 +02:00
|
|
|
class RequestProcessor implements HTTPMiddleware
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
use Injectable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of currently assigned request filters
|
|
|
|
*
|
2017-06-25 05:12:29 +02:00
|
|
|
* @var RequestFilter[]
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
private $filters = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-25 05:12:29 +02:00
|
|
|
/**
|
|
|
|
* Construct new RequestFilter with a list of filter objects
|
|
|
|
*
|
|
|
|
* @param RequestFilter[] $filters
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
public function __construct($filters = [])
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
$this->filters = $filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign a list of request filters
|
|
|
|
*
|
2017-06-25 05:12:29 +02:00
|
|
|
* @param RequestFilter[] $filters
|
|
|
|
* @return $this
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function setFilters($filters)
|
|
|
|
{
|
|
|
|
$this->filters = $filters;
|
2017-06-25 05:12:29 +02:00
|
|
|
return $this;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-23 01:17:30 +02:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function process(HTTPRequest $request, callable $delegate)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-23 01:17:30 +02:00
|
|
|
if ($this->filters) {
|
|
|
|
Deprecation::notice(
|
2017-06-25 05:12:29 +02:00
|
|
|
'5.0',
|
2017-06-23 01:17:30 +02:00
|
|
|
'Deprecated RequestFilters are in use. Apply HTTPMiddleware to Director.middlewares instead.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($this->filters as $filter) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$res = $filter->preRequest($request);
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($res === false) {
|
2018-01-16 19:39:30 +01:00
|
|
|
return new HTTPResponse(_t(__CLASS__ . '.INVALID_REQUEST', 'Invalid request'), 400);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 01:17:30 +02:00
|
|
|
$response = $delegate($request);
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($this->filters as $filter) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$res = $filter->postRequest($request, $response);
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($res === false) {
|
2017-06-23 01:17:30 +02:00
|
|
|
return new HTTPResponse(_t(__CLASS__ . '.REQUEST_ABORTED', 'Request aborted'), 500);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-23 01:17:30 +02:00
|
|
|
|
|
|
|
return $response;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|