mirror of
https://github.com/getgrav/grav.git
synced 2026-05-09 00:37:13 +02:00
Allow new task controllers to run
This commit is contained in:
@@ -22,17 +22,24 @@ abstract class ProcessorBase implements ProcessorInterface
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
protected function startTimer()
|
||||
protected function startTimer($id = null, $title = null)
|
||||
{
|
||||
/** @var Debugger $debugger */
|
||||
$debugger = $this->container['debugger'];
|
||||
$debugger->startTimer($this->id, $this->title);
|
||||
$debugger->startTimer($id ?? $this->id, $title ?? $this->title);
|
||||
}
|
||||
|
||||
protected function stopTimer()
|
||||
protected function stopTimer($id = null)
|
||||
{
|
||||
/** @var Debugger $debugger */
|
||||
$debugger = $this->container['debugger'];
|
||||
$debugger->stopTimer($this->id);
|
||||
$debugger->stopTimer($id ?? $this->id);
|
||||
}
|
||||
|
||||
protected function addMessage($message, $label = 'info', $isString = true)
|
||||
{
|
||||
/** @var Debugger $debugger */
|
||||
$debugger = $this->container['debugger'];
|
||||
$debugger->addMessage($message, $label, $isString);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
namespace Grav\Common\Processors;
|
||||
|
||||
use Grav\Framework\RequestHandler\Exception\NotFoundException;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
@@ -21,8 +22,21 @@ class TasksProcessor extends ProcessorBase
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
|
||||
{
|
||||
$this->startTimer();
|
||||
|
||||
$task = $this->container['task'];
|
||||
if ($task) {
|
||||
$params = $request->getAttribute('controller');
|
||||
$controllerClass = $params['class'] ?? null;
|
||||
if ($controllerClass) {
|
||||
/** @var RequestHandlerInterface $controller */
|
||||
$controller = new $controllerClass($params);
|
||||
try {
|
||||
return $controller->handle($request);
|
||||
} catch (NotFoundException $e) {
|
||||
// Task not found: Let it pass through.
|
||||
}
|
||||
}
|
||||
|
||||
$this->container->fireEvent('onTask.' . $task);
|
||||
}
|
||||
$this->stopTimer();
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Framework\DI
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Grav\Framework\DI;
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Framework\RequestHandler
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Grav\Framework\RequestHandler\Exception;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Framework\RequestHandler
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Grav\Framework\RequestHandler\Exception;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class NotFoundException extends RequestException
|
||||
{
|
||||
/**
|
||||
* NotFoundException constructor.
|
||||
* @param ServerRequestInterface $request
|
||||
* @param \Throwable|null $previous
|
||||
*/
|
||||
public function __construct(ServerRequestInterface $request, \Throwable $previous = null)
|
||||
{
|
||||
if (\in_array(strtoupper($request->getMethod()), ['PUT', 'PATCH', 'DELETE'])) {
|
||||
parent::__construct($request, 'Method Not Allowed', 405, $previous);
|
||||
} else {
|
||||
parent::__construct($request, 'Not Found', 404, $previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Framework\RequestHandler
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Grav\Framework\RequestHandler\Exception;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class PageExpiredException extends RequestException
|
||||
{
|
||||
/**
|
||||
* PageExpiredException constructor.
|
||||
* @param ServerRequestInterface $request
|
||||
* @param \Throwable|null $previous
|
||||
*/
|
||||
public function __construct(ServerRequestInterface $request, \Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($request, 'Page Expired', 400, $previous); // 419
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Framework\RequestHandler
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Grav\Framework\RequestHandler\Exception;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class RequestException extends \RuntimeException
|
||||
{
|
||||
/** @var array Map of standard HTTP status code/reason phrases */
|
||||
private static $phrases = [
|
||||
400 => 'Bad Request',
|
||||
401 => 'Unauthorized',
|
||||
402 => 'Payment Required',
|
||||
403 => 'Forbidden',
|
||||
404 => 'Not Found',
|
||||
405 => 'Method Not Allowed',
|
||||
406 => 'Not Acceptable',
|
||||
407 => 'Proxy Authentication Required',
|
||||
408 => 'Request Time-out',
|
||||
409 => 'Conflict',
|
||||
410 => 'Gone',
|
||||
411 => 'Length Required',
|
||||
412 => 'Precondition Failed',
|
||||
413 => 'Request Entity Too Large',
|
||||
414 => 'Request-URI Too Large',
|
||||
415 => 'Unsupported Media Type',
|
||||
416 => 'Requested range not satisfiable',
|
||||
417 => 'Expectation Failed',
|
||||
418 => 'I\'m a teapot',
|
||||
419 => 'Page Expired',
|
||||
422 => 'Unprocessable Entity',
|
||||
423 => 'Locked',
|
||||
424 => 'Failed Dependency',
|
||||
425 => 'Unordered Collection',
|
||||
426 => 'Upgrade Required',
|
||||
428 => 'Precondition Required',
|
||||
429 => 'Too Many Requests',
|
||||
431 => 'Request Header Fields Too Large',
|
||||
451 => 'Unavailable For Legal Reasons',
|
||||
|
||||
500 => 'Internal Server Error',
|
||||
501 => 'Not Implemented',
|
||||
502 => 'Bad Gateway',
|
||||
503 => 'Service Unavailable',
|
||||
504 => 'Gateway Time-out',
|
||||
505 => 'HTTP Version not supported',
|
||||
506 => 'Variant Also Negotiates',
|
||||
507 => 'Insufficient Storage',
|
||||
508 => 'Loop Detected',
|
||||
511 => 'Network Authentication Required',
|
||||
];
|
||||
|
||||
/** @var ServerRequestInterface */
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param \Throwable|null $previous
|
||||
*/
|
||||
public function __construct(ServerRequestInterface $request, string $message, int $code = 500, \Throwable $previous = null)
|
||||
{
|
||||
$this->request = $request;
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ServerRequestInterface
|
||||
*/
|
||||
public function getRequest() : ServerRequestInterface
|
||||
{
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
public function getHttpCode() : int
|
||||
{
|
||||
$code = $this->getCode();
|
||||
|
||||
return isset(self::$phrases[$code]) ? $code : 500;
|
||||
}
|
||||
|
||||
public function getHttpReason() : ?string
|
||||
{
|
||||
return self::$phrases[$this->getCode()] ?? self::$phrases[500];
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Framework\RequestHandler
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Grav\Framework\RequestHandler\Middlewares;
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Framework\RequestHandler
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Grav\Framework\RequestHandler;
|
||||
|
||||
Reference in New Issue
Block a user