2012-05-09 14:26:29 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Control;
|
|
|
|
|
|
|
|
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.
|
|
|
|
* You should 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
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $filters = array();
|
|
|
|
|
|
|
|
public function __construct($filters = array())
|
|
|
|
{
|
|
|
|
$this->filters = $filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign a list of request filters
|
|
|
|
*
|
|
|
|
* @param array $filters
|
|
|
|
*/
|
|
|
|
public function setFilters($filters)
|
|
|
|
{
|
|
|
|
$this->filters = $filters;
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
'4.0',
|
|
|
|
'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) {
|
2017-06-23 01:17:30 +02: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
|
|
|
}
|