mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-28 04:58:46 +02:00
migration symfony 5 4 (#300)
* symfony 5.4 (diff dev) * symfony 5.4 (working) * symfony 5.4 (update autoload) * symfony 5.4 (remove swiftmailer mailer implementation) * symfony 5.4 (php doc and split Global accessor class) ### Impacted packages: composer require php:">=7.2.5 <8.0.0" symfony/console:5.4.* symfony/dotenv:5.4.* symfony/framework-bundle:5.4.* symfony/twig-bundle:5.4.* symfony/yaml:5.4.* --update-with-dependencies composer require symfony/stopwatch:5.4.* symfony/web-profiler-bundle:5.4.* --dev --update-with-dependencies
This commit is contained in:
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace Symfony\Component\HttpKernel;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
@@ -19,17 +18,30 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
|
||||
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
|
||||
use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
|
||||
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
|
||||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
|
||||
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
|
||||
use Symfony\Component\HttpKernel\Event\ControllerEvent;
|
||||
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
|
||||
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
||||
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
|
||||
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||
use Symfony\Component\HttpKernel\Event\ResponseEvent;
|
||||
use Symfony\Component\HttpKernel\Event\TerminateEvent;
|
||||
use Symfony\Component\HttpKernel\Event\ViewEvent;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\ControllerDoesNotReturnResponseException;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
// Help opcache.preload discover always-needed symbols
|
||||
class_exists(ControllerArgumentsEvent::class);
|
||||
class_exists(ControllerEvent::class);
|
||||
class_exists(ExceptionEvent::class);
|
||||
class_exists(FinishRequestEvent::class);
|
||||
class_exists(RequestEvent::class);
|
||||
class_exists(ResponseEvent::class);
|
||||
class_exists(TerminateEvent::class);
|
||||
class_exists(ViewEvent::class);
|
||||
class_exists(KernelEvents::class);
|
||||
|
||||
/**
|
||||
* HttpKernel notifies events to convert a Request object to a Response one.
|
||||
@@ -47,20 +59,14 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->resolver = $resolver;
|
||||
$this->requestStack = $requestStack ?: new RequestStack();
|
||||
$this->argumentResolver = $argumentResolver;
|
||||
|
||||
if (null === $this->argumentResolver) {
|
||||
@trigger_error(sprintf('As of 3.1 an %s is used to resolve arguments. In 4.0 the $argumentResolver becomes the %s if no other is provided instead of using the $resolver argument.', ArgumentResolverInterface::class, ArgumentResolver::class), \E_USER_DEPRECATED);
|
||||
// fallback in case of deprecations
|
||||
$this->argumentResolver = $resolver;
|
||||
}
|
||||
$this->requestStack = $requestStack ?? new RequestStack();
|
||||
$this->argumentResolver = $argumentResolver ?? new ArgumentResolver();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
|
||||
public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true)
|
||||
{
|
||||
$request->headers->set('X-Php-Ob-Level', (string) ob_get_level());
|
||||
|
||||
@@ -76,7 +82,7 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $this->handleException($e, $request, $type);
|
||||
return $this->handleThrowable($e, $request, $type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,19 +91,19 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
*/
|
||||
public function terminate(Request $request, Response $response)
|
||||
{
|
||||
$this->dispatcher->dispatch(KernelEvents::TERMINATE, new PostResponseEvent($this, $request, $response));
|
||||
$this->dispatcher->dispatch(new TerminateEvent($this, $request, $response), KernelEvents::TERMINATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function terminateWithException(\Exception $exception, Request $request = null)
|
||||
public function terminateWithException(\Throwable $exception, Request $request = null)
|
||||
{
|
||||
if (!$request = $request ?: $this->requestStack->getMasterRequest()) {
|
||||
if (!$request = $request ?: $this->requestStack->getMainRequest()) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
$response = $this->handleException($exception, $request, self::MASTER_REQUEST);
|
||||
$response = $this->handleThrowable($exception, $request, self::MAIN_REQUEST);
|
||||
|
||||
$response->sendHeaders();
|
||||
$response->sendContent();
|
||||
@@ -110,21 +116,16 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
*
|
||||
* Exceptions are not caught.
|
||||
*
|
||||
* @param Request $request A Request instance
|
||||
* @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
|
||||
*
|
||||
* @return Response A Response instance
|
||||
*
|
||||
* @throws \LogicException If one of the listener does not behave as expected
|
||||
* @throws NotFoundHttpException When controller cannot be found
|
||||
*/
|
||||
private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
|
||||
private function handleRaw(Request $request, int $type = self::MAIN_REQUEST): Response
|
||||
{
|
||||
$this->requestStack->push($request);
|
||||
|
||||
// request
|
||||
$event = new GetResponseEvent($this, $request, $type);
|
||||
$this->dispatcher->dispatch(KernelEvents::REQUEST, $event);
|
||||
$event = new RequestEvent($this, $request, $type);
|
||||
$this->dispatcher->dispatch($event, KernelEvents::REQUEST);
|
||||
|
||||
if ($event->hasResponse()) {
|
||||
return $this->filterResponse($event->getResponse(), $request, $type);
|
||||
@@ -135,38 +136,37 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
|
||||
}
|
||||
|
||||
$event = new FilterControllerEvent($this, $controller, $request, $type);
|
||||
$this->dispatcher->dispatch(KernelEvents::CONTROLLER, $event);
|
||||
$event = new ControllerEvent($this, $controller, $request, $type);
|
||||
$this->dispatcher->dispatch($event, KernelEvents::CONTROLLER);
|
||||
$controller = $event->getController();
|
||||
|
||||
// controller arguments
|
||||
$arguments = $this->argumentResolver->getArguments($request, $controller);
|
||||
|
||||
$event = new FilterControllerArgumentsEvent($this, $controller, $arguments, $request, $type);
|
||||
$this->dispatcher->dispatch(KernelEvents::CONTROLLER_ARGUMENTS, $event);
|
||||
$event = new ControllerArgumentsEvent($this, $controller, $arguments, $request, $type);
|
||||
$this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);
|
||||
$controller = $event->getController();
|
||||
$arguments = $event->getArguments();
|
||||
|
||||
// call controller
|
||||
$response = \call_user_func_array($controller, $arguments);
|
||||
$response = $controller(...$arguments);
|
||||
|
||||
// view
|
||||
if (!$response instanceof Response) {
|
||||
$event = new GetResponseForControllerResultEvent($this, $request, $type, $response);
|
||||
$this->dispatcher->dispatch(KernelEvents::VIEW, $event);
|
||||
$event = new ViewEvent($this, $request, $type, $response);
|
||||
$this->dispatcher->dispatch($event, KernelEvents::VIEW);
|
||||
|
||||
if ($event->hasResponse()) {
|
||||
$response = $event->getResponse();
|
||||
}
|
||||
|
||||
if (!$response instanceof Response) {
|
||||
$msg = sprintf('The controller must return a response (%s given).', $this->varToString($response));
|
||||
} else {
|
||||
$msg = sprintf('The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned %s.', $this->varToString($response));
|
||||
|
||||
// the user may have forgotten to return something
|
||||
if (null === $response) {
|
||||
$msg .= ' Did you forget to add a return statement somewhere in your controller?';
|
||||
}
|
||||
throw new \LogicException($msg);
|
||||
|
||||
throw new ControllerDoesNotReturnResponseException($msg, $controller, __FILE__, __LINE__ - 17);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,19 +176,13 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Filters a response object.
|
||||
*
|
||||
* @param Response $response A Response instance
|
||||
* @param Request $request An error message in case the response is not a Response object
|
||||
* @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
|
||||
*
|
||||
* @return Response The filtered Response instance
|
||||
*
|
||||
* @throws \RuntimeException if the passed object is not a Response instance
|
||||
*/
|
||||
private function filterResponse(Response $response, Request $request, $type)
|
||||
private function filterResponse(Response $response, Request $request, int $type): Response
|
||||
{
|
||||
$event = new FilterResponseEvent($this, $request, $type, $response);
|
||||
$event = new ResponseEvent($this, $request, $type, $response);
|
||||
|
||||
$this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
|
||||
$this->dispatcher->dispatch($event, KernelEvents::RESPONSE);
|
||||
|
||||
$this->finishRequest($request, $type);
|
||||
|
||||
@@ -201,33 +195,25 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
* Note that the order of the operations is important here, otherwise
|
||||
* operations such as {@link RequestStack::getParentRequest()} can lead to
|
||||
* weird results.
|
||||
*
|
||||
* @param int $type
|
||||
*/
|
||||
private function finishRequest(Request $request, $type)
|
||||
private function finishRequest(Request $request, int $type)
|
||||
{
|
||||
$this->dispatcher->dispatch(KernelEvents::FINISH_REQUEST, new FinishRequestEvent($this, $request, $type));
|
||||
$this->dispatcher->dispatch(new FinishRequestEvent($this, $request, $type), KernelEvents::FINISH_REQUEST);
|
||||
$this->requestStack->pop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles an exception by trying to convert it to a Response.
|
||||
*
|
||||
* @param \Exception $e An \Exception instance
|
||||
* @param Request $request A Request instance
|
||||
* @param int $type The type of the request
|
||||
*
|
||||
* @return Response A Response instance
|
||||
* Handles a throwable by trying to convert it to a Response.
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function handleException(\Exception $e, $request, $type)
|
||||
private function handleThrowable(\Throwable $e, Request $request, int $type): Response
|
||||
{
|
||||
$event = new GetResponseForExceptionEvent($this, $request, $type, $e);
|
||||
$this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
|
||||
$event = new ExceptionEvent($this, $request, $type, $e);
|
||||
$this->dispatcher->dispatch($event, KernelEvents::EXCEPTION);
|
||||
|
||||
// a listener might have replaced the exception
|
||||
$e = $event->getException();
|
||||
$e = $event->getThrowable();
|
||||
|
||||
if (!$event->hasResponse()) {
|
||||
$this->finishRequest($request, $type);
|
||||
@@ -238,13 +224,7 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
$response = $event->getResponse();
|
||||
|
||||
// the developer asked for a specific status code
|
||||
if ($response->headers->has('X-Status-Code')) {
|
||||
@trigger_error(sprintf('Using the X-Status-Code header is deprecated since Symfony 3.3 and will be removed in 4.0. Use %s::allowCustomResponseCode() instead.', GetResponseForExceptionEvent::class), \E_USER_DEPRECATED);
|
||||
|
||||
$response->setStatusCode($response->headers->get('X-Status-Code'));
|
||||
|
||||
$response->headers->remove('X-Status-Code');
|
||||
} elseif (!$event->isAllowingCustomResponseCode() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
|
||||
if (!$event->isAllowingCustomResponseCode() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
|
||||
// ensure that we actually have an error response
|
||||
if ($e instanceof HttpExceptionInterface) {
|
||||
// keep the HTTP status code and headers
|
||||
@@ -265,23 +245,23 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
/**
|
||||
* Returns a human-readable string for the specified variable.
|
||||
*/
|
||||
private function varToString($var)
|
||||
private function varToString($var): string
|
||||
{
|
||||
if (\is_object($var)) {
|
||||
return sprintf('Object(%s)', \get_class($var));
|
||||
return sprintf('an object of type %s', \get_class($var));
|
||||
}
|
||||
|
||||
if (\is_array($var)) {
|
||||
$a = [];
|
||||
foreach ($var as $k => $v) {
|
||||
$a[] = sprintf('%s => %s', $k, $this->varToString($v));
|
||||
$a[] = sprintf('%s => ...', $k);
|
||||
}
|
||||
|
||||
return sprintf('Array(%s)', implode(', ', $a));
|
||||
return sprintf('an array ([%s])', mb_substr(implode(', ', $a), 0, 255));
|
||||
}
|
||||
|
||||
if (\is_resource($var)) {
|
||||
return sprintf('Resource(%s)', get_resource_type($var));
|
||||
return sprintf('a resource (%s)', get_resource_type($var));
|
||||
}
|
||||
|
||||
if (null === $var) {
|
||||
@@ -289,11 +269,19 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
||||
}
|
||||
|
||||
if (false === $var) {
|
||||
return 'false';
|
||||
return 'a boolean value (false)';
|
||||
}
|
||||
|
||||
if (true === $var) {
|
||||
return 'true';
|
||||
return 'a boolean value (true)';
|
||||
}
|
||||
|
||||
if (\is_string($var)) {
|
||||
return sprintf('a string ("%s%s")', mb_substr($var, 0, 255), mb_strlen($var) > 255 ? '...' : '');
|
||||
}
|
||||
|
||||
if (is_numeric($var)) {
|
||||
return sprintf('a number (%s)', (string) $var);
|
||||
}
|
||||
|
||||
return (string) $var;
|
||||
|
||||
Reference in New Issue
Block a user