first commit

This commit is contained in:
Sampanna Rimal
2024-08-27 17:48:06 +05:45
commit 53c0140f58
10839 changed files with 1125847 additions and 0 deletions

View File

@ -0,0 +1,109 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Allows filtering of controller arguments.
*
* You can call getController() to retrieve the controller and getArguments
* to retrieve the current arguments. With setArguments() you can replace
* arguments that are used to call the controller.
*
* Arguments set in the event must be compatible with the signature of the
* controller.
*
* @author Christophe Coevoet <stof@notk.org>
*/
final class ControllerArgumentsEvent extends KernelEvent
{
private ControllerEvent $controllerEvent;
private array $arguments;
private array $namedArguments;
public function __construct(HttpKernelInterface $kernel, callable|ControllerEvent $controller, array $arguments, Request $request, ?int $requestType)
{
parent::__construct($kernel, $request, $requestType);
if (!$controller instanceof ControllerEvent) {
$controller = new ControllerEvent($kernel, $controller, $request, $requestType);
}
$this->controllerEvent = $controller;
$this->arguments = $arguments;
}
public function getController(): callable
{
return $this->controllerEvent->getController();
}
/**
* @param array<class-string, list<object>>|null $attributes
*/
public function setController(callable $controller, ?array $attributes = null): void
{
$this->controllerEvent->setController($controller, $attributes);
unset($this->namedArguments);
}
public function getArguments(): array
{
return $this->arguments;
}
public function setArguments(array $arguments): void
{
$this->arguments = $arguments;
unset($this->namedArguments);
}
public function getNamedArguments(): array
{
if (isset($this->namedArguments)) {
return $this->namedArguments;
}
$namedArguments = [];
$arguments = $this->arguments;
foreach ($this->controllerEvent->getControllerReflector()->getParameters() as $i => $param) {
if ($param->isVariadic()) {
$namedArguments[$param->name] = \array_slice($arguments, $i);
break;
}
if (\array_key_exists($i, $arguments)) {
$namedArguments[$param->name] = $arguments[$i];
} elseif ($param->isDefaultvalueAvailable()) {
$namedArguments[$param->name] = $param->getDefaultValue();
}
}
return $this->namedArguments = $namedArguments;
}
/**
* @template T of class-string|null
*
* @param T $className
*
* @return array<class-string, list<object>>|list<object>
*
* @psalm-return (T is null ? array<class-string, list<object>> : list<object>)
*/
public function getAttributes(?string $className = null): array
{
return $this->controllerEvent->getAttributes($className);
}
}

View File

@ -0,0 +1,113 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Allows filtering of a controller callable.
*
* You can call getController() to retrieve the current controller. With
* setController() you can set a new controller that is used in the processing
* of the request.
*
* Controllers should be callables.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
final class ControllerEvent extends KernelEvent
{
private string|array|object $controller;
private \ReflectionFunctionAbstract $controllerReflector;
private array $attributes;
public function __construct(HttpKernelInterface $kernel, callable $controller, Request $request, ?int $requestType)
{
parent::__construct($kernel, $request, $requestType);
$this->setController($controller);
}
public function getController(): callable
{
return $this->controller;
}
public function getControllerReflector(): \ReflectionFunctionAbstract
{
return $this->controllerReflector;
}
/**
* @param array<class-string, list<object>>|null $attributes
*/
public function setController(callable $controller, ?array $attributes = null): void
{
if (null !== $attributes) {
$this->attributes = $attributes;
}
if (isset($this->controller) && ($controller instanceof \Closure ? $controller == $this->controller : $controller === $this->controller)) {
$this->controller = $controller;
return;
}
if (null === $attributes) {
unset($this->attributes);
}
if (\is_array($controller) && method_exists(...$controller)) {
$this->controllerReflector = new \ReflectionMethod(...$controller);
} elseif (\is_string($controller) && str_contains($controller, '::')) {
$this->controllerReflector = new \ReflectionMethod(...explode('::', $controller, 2));
} else {
$this->controllerReflector = new \ReflectionFunction($controller(...));
}
$this->controller = $controller;
}
/**
* @template T of class-string|null
*
* @param T $className
*
* @return array<class-string, list<object>>|list<object>
*
* @psalm-return (T is null ? array<class-string, list<object>> : list<object>)
*/
public function getAttributes(?string $className = null): array
{
if (isset($this->attributes)) {
return null === $className ? $this->attributes : $this->attributes[$className] ?? [];
}
if (\is_array($this->controller) && method_exists(...$this->controller)) {
$class = new \ReflectionClass($this->controller[0]);
} elseif (\is_string($this->controller) && false !== $i = strpos($this->controller, '::')) {
$class = new \ReflectionClass(substr($this->controller, 0, $i));
} else {
$class = str_contains($this->controllerReflector->name, '{closure}') ? null : (\PHP_VERSION_ID >= 80111 ? $this->controllerReflector->getClosureCalledClass() : $this->controllerReflector->getClosureScopeClass());
}
$this->attributes = [];
foreach (array_merge($class?->getAttributes() ?? [], $this->controllerReflector->getAttributes()) as $attribute) {
if (class_exists($attribute->getName())) {
$this->attributes[$attribute->getName()][] = $attribute->newInstance();
}
}
return null === $className ? $this->attributes : $this->attributes[$className] ?? [];
}
}

View File

@ -0,0 +1,72 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Allows to create a response for a thrown exception.
*
* Call setResponse() to set the response that will be returned for the
* current request. The propagation of this event is stopped as soon as a
* response is set.
*
* You can also call setThrowable() to replace the thrown exception. This
* exception will be thrown if no response is set during processing of this
* event.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
final class ExceptionEvent extends RequestEvent
{
private \Throwable $throwable;
private bool $allowCustomResponseCode = false;
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Throwable $e)
{
parent::__construct($kernel, $request, $requestType);
$this->setThrowable($e);
}
public function getThrowable(): \Throwable
{
return $this->throwable;
}
/**
* Replaces the thrown exception.
*
* This exception will be thrown if no response is set in the event.
*/
public function setThrowable(\Throwable $exception): void
{
$this->throwable = $exception;
}
/**
* Mark the event as allowing a custom response code.
*/
public function allowCustomResponseCode(): void
{
$this->allowCustomResponseCode = true;
}
/**
* Returns true if the event allows a custom response code.
*/
public function isAllowingCustomResponseCode(): bool
{
return $this->allowCustomResponseCode;
}
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Event;
/**
* Triggered whenever a request is fully processed.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
final class FinishRequestEvent extends KernelEvent
{
}

View File

@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Contracts\EventDispatcher\Event;
/**
* Base class for events thrown in the HttpKernel component.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class KernelEvent extends Event
{
private HttpKernelInterface $kernel;
private Request $request;
private ?int $requestType;
/**
* @param int $requestType The request type the kernel is currently processing; one of
* HttpKernelInterface::MAIN_REQUEST or HttpKernelInterface::SUB_REQUEST
*/
public function __construct(HttpKernelInterface $kernel, Request $request, ?int $requestType)
{
$this->kernel = $kernel;
$this->request = $request;
$this->requestType = $requestType;
}
/**
* Returns the kernel in which this event was thrown.
*/
public function getKernel(): HttpKernelInterface
{
return $this->kernel;
}
/**
* Returns the request the kernel is currently processing.
*/
public function getRequest(): Request
{
return $this->request;
}
/**
* Returns the request type the kernel is currently processing.
*
* @return int One of HttpKernelInterface::MAIN_REQUEST and
* HttpKernelInterface::SUB_REQUEST
*/
public function getRequestType(): int
{
return $this->requestType;
}
/**
* Checks if this is the main request.
*/
public function isMainRequest(): bool
{
return HttpKernelInterface::MAIN_REQUEST === $this->requestType;
}
}

View File

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Event;
use Symfony\Component\HttpFoundation\Response;
/**
* Allows to create a response for a request.
*
* Call setResponse() to set the response that will be returned for the
* current request. The propagation of this event is stopped as soon as a
* response is set.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class RequestEvent extends KernelEvent
{
private ?Response $response = null;
/**
* Returns the response object.
*/
public function getResponse(): ?Response
{
return $this->response;
}
/**
* Sets a response and stops event propagation.
*
* @return void
*/
public function setResponse(Response $response)
{
$this->response = $response;
$this->stopPropagation();
}
/**
* Returns whether a response was set.
*/
public function hasResponse(): bool
{
return null !== $this->response;
}
}

View File

@ -0,0 +1,47 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Allows to filter a Response object.
*
* You can call getResponse() to retrieve the current response. With
* setResponse() you can set a new response that will be returned to the
* browser.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
final class ResponseEvent extends KernelEvent
{
private Response $response;
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, Response $response)
{
parent::__construct($kernel, $request, $requestType);
$this->setResponse($response);
}
public function getResponse(): Response
{
return $this->response;
}
public function setResponse(Response $response): void
{
$this->response = $response;
}
}

View File

@ -0,0 +1,41 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Allows to execute logic after a response was sent.
*
* Since it's only triggered on main requests, the `getRequestType()` method
* will always return the value of `HttpKernelInterface::MAIN_REQUEST`.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
final class TerminateEvent extends KernelEvent
{
private Response $response;
public function __construct(HttpKernelInterface $kernel, Request $request, Response $response)
{
parent::__construct($kernel, $request, HttpKernelInterface::MAIN_REQUEST);
$this->response = $response;
}
public function getResponse(): Response
{
return $this->response;
}
}

View File

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Allows to create a response for the return value of a controller.
*
* Call setResponse() to set the response that will be returned for the
* current request. The propagation of this event is stopped as soon as a
* response is set.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
final class ViewEvent extends RequestEvent
{
public readonly ?ControllerArgumentsEvent $controllerArgumentsEvent;
private mixed $controllerResult;
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, mixed $controllerResult, ?ControllerArgumentsEvent $controllerArgumentsEvent = null)
{
parent::__construct($kernel, $request, $requestType);
$this->controllerResult = $controllerResult;
$this->controllerArgumentsEvent = $controllerArgumentsEvent;
}
public function getControllerResult(): mixed
{
return $this->controllerResult;
}
public function setControllerResult(mixed $controllerResult): void
{
$this->controllerResult = $controllerResult;
}
}