Framework done ✨✨✨
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
79311b1041
commit
ac98c21bf8
@ -1,16 +1,20 @@
|
||||
<?php
|
||||
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
$dotenv = Dotenv::createUnsafeImmutable(__DIR__,'.env');
|
||||
$dotenv->safeLoad();
|
||||
// apenrently getEnv is not a good thing cause
|
||||
// const DB_HOST = $_ENV['DB_HOST'] ?? 'localhost';
|
||||
// const DB_DATABASE = $_ENV['DB_DATABASE'] ?? 'heartTrack';
|
||||
// const DB_USER = $_ENV['DB_USER'] ?? 'toto';
|
||||
// const DB_PASSWORD = $_ENV['DB_PASSWORD'] ?? 'achanger';
|
||||
// const APP_ENV = $_ENV['APP_ENV'] ?? 'development';
|
||||
define("APP_ENV", getenv('APP_ENV') ?? 'development');
|
||||
|
||||
const DB_HOST = 'localhost';
|
||||
const DB_DATABASE = 'heartTrack';
|
||||
const DB_USER = 'toto';
|
||||
const DB_PASSWORD = 'achanger';
|
||||
const APP_ENV = 'development';
|
||||
|
||||
const DSN = "mysql:host=" . DB_HOST . ";dbname=" . DB_DATABASE;
|
||||
|
@ -1,9 +1,36 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
require_once __DIR__ . '/../config/config.php';
|
||||
if (APP_ENV === 'console') {
|
||||
require_once __DIR__ . '/../src/console/Console.php';
|
||||
}
|
||||
elseif (APP_ENV === 'development') {
|
||||
require_once __DIR__ . DIRECTORY_SEPARATOR . '../src/app/index.test.php';
|
||||
|
||||
use App\AppCreator;
|
||||
use App\Router\Middleware\LoggingMiddleware;
|
||||
use App\Router\Request\RequestFactory;
|
||||
use Shared\ArgumentControllerResolver;
|
||||
use Shared\IArgumentResolver;
|
||||
use Twig\Environment;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
|
||||
|
||||
$appFactory = new AppCreator();
|
||||
$appFactory->registerService(IArgumentResolver::class, ArgumentControllerResolver::class);
|
||||
|
||||
$appFactory->registerService(\Twig\Loader\LoaderInterface::class, function() {
|
||||
return new FilesystemLoader(__DIR__ . '/../src/app/views/Templates');
|
||||
});
|
||||
|
||||
$appFactory->registerService(\Twig\Environment::class,\Twig\Environment::class);
|
||||
|
||||
// Connexion à la base de données
|
||||
// $databaseContext = DatabaseContext::getInstance();
|
||||
|
||||
$appFactory->AddControllers();
|
||||
$app = $appFactory->create();
|
||||
if (!is_null($app)){
|
||||
// Ajout des Middleware
|
||||
/*$app->use(new LoggingMiddleware());*/
|
||||
|
||||
$app->mapControllers();
|
||||
$app->run(RequestFactory::createFromGlobals());
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Controller\BaseController;
|
||||
use App\Controller\FrontController;
|
||||
use App\Router\Request\HttpRequest;
|
||||
use App\Router\Middleware\IHttpMiddleware;
|
||||
use App\Router\Request\IRequest;
|
||||
use App\Router\Route;
|
||||
use App\Views\Directives\Navigate;
|
||||
use Shared\Attributes\Route as RouteAttribute;
|
||||
use App\Router\Router;
|
||||
use App\Router\Session;
|
||||
use Shared\Log;
|
||||
|
||||
class App
|
||||
{
|
||||
private string $appName;
|
||||
private int $version;
|
||||
|
||||
private ?IHttpMiddleware $middlewarePipeline = null;
|
||||
|
||||
private Container $container;
|
||||
|
||||
private Router $router;
|
||||
|
||||
private array $controllers = [];
|
||||
|
||||
private FrontController $frontController;
|
||||
|
||||
public function __construct(string $appName, int $version, \App\Container $diContainer)
|
||||
{
|
||||
$this->appName = $appName;
|
||||
$this->version = $version;
|
||||
$this->container = $diContainer;
|
||||
$this->router = new Router("");
|
||||
$this->frontController = new FrontController($this->router,$this->container);
|
||||
}
|
||||
|
||||
public function use(IHttpMiddleware $middleware)
|
||||
{
|
||||
if ($this->middlewarePipeline === null) {
|
||||
$this->middlewarePipeline = $middleware;
|
||||
} else {
|
||||
// Chain the new middleware to the end of the existing pipeline
|
||||
$currentMiddleware = $this->middlewarePipeline;
|
||||
while ($currentMiddleware->getNext() !== null) {
|
||||
$currentMiddleware = $currentMiddleware->getNext();
|
||||
}
|
||||
$currentMiddleware->setNext($middleware);
|
||||
}
|
||||
}
|
||||
|
||||
public function getAppName(): string
|
||||
{
|
||||
return $this->appName;
|
||||
}
|
||||
|
||||
/* public function twigConfigure(array $extensionClassNames = []): void
|
||||
{
|
||||
if (!$this->container->has(\Twig\Environment::class)) {
|
||||
throw new \LogicException('You cannot use the "twigConfigure" method if the Twig Bundle is not available. Try running "composer require twig/twig".');
|
||||
}
|
||||
|
||||
$twigEnvironment = $this->container->get(\Twig\Environment::class);
|
||||
|
||||
if (empty($extensionClassNames)) {
|
||||
$twigEnvironment->addExtension(new Navigate($this->router));
|
||||
|
||||
} else {
|
||||
foreach ($extensionClassNames as $extensionClassName) {
|
||||
if (class_exists($extensionClassName)) {
|
||||
$extensionInstance = new $extensionClassName();
|
||||
if ($extensionInstance instanceof \Twig\Extension\ExtensionInterface) {
|
||||
$twigEnvironment->addExtension($extensionInstance);
|
||||
} else {
|
||||
throw new \InvalidArgumentException("Class '$extensionClassName' does not implement Twig\Extension\ExtensionInterface.");
|
||||
}
|
||||
} else {
|
||||
throw new \InvalidArgumentException("Class '$extensionClassName' does not exist.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
public function getVersion(): int
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
public function run(IRequest $request)
|
||||
{
|
||||
if ($this->middlewarePipeline == null) {
|
||||
return $this->frontController->dispatch($request);
|
||||
}
|
||||
// Exécutez le middleware en utilisant le pipeline
|
||||
return $this->middlewarePipeline->handle($request, function($request) {
|
||||
// Logique de gestion principale de la requête ici
|
||||
$this->frontController->dispatch($request);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function mapControllers(): void
|
||||
{
|
||||
$classes = $this->container->getAllRegisteredClassNames();
|
||||
|
||||
foreach ($classes as $class) {
|
||||
if ($this->isController($class)) {
|
||||
$this->mapControllerRoutes($class);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
function mapControllerRoutes(string $controllerClass): void
|
||||
{
|
||||
$reflectionClass = new \ReflectionClass($controllerClass);
|
||||
$attributes = $reflectionClass->getAttributes(RouteAttribute::class);
|
||||
$prefix = '';
|
||||
if (!empty($attributes)) {
|
||||
|
||||
$prefix = $attributes[0]->newInstance()->getPath();
|
||||
}
|
||||
|
||||
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
|
||||
|
||||
foreach ($method->getAttributes(RouteAttribute::class) as $attribute) {
|
||||
|
||||
/** @var RouteAttribute $route */
|
||||
$route = $attribute->newInstance();
|
||||
|
||||
$this->router->addControllerRoute(
|
||||
implode('|', $route->getMethods()),
|
||||
$prefix . $route->getPath(),
|
||||
$controllerClass,
|
||||
$method->getName(),
|
||||
$route->getName()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function isController(string $class): bool
|
||||
{
|
||||
$reflectionClass = new \ReflectionClass($class);
|
||||
return $reflectionClass->isSubclassOf(BaseController::class);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Controller\BaseController;
|
||||
use Shared\Log;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
|
||||
class AppCreator
|
||||
{
|
||||
private Container $container;
|
||||
|
||||
private array $services = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->container = new Container;
|
||||
}
|
||||
|
||||
public function registerService(string $serviceId, callable|string $service): self
|
||||
{
|
||||
$this->container->set($serviceId, $service);
|
||||
$this->services[] = $serviceId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance or perform actions based on the current application environment.
|
||||
*
|
||||
* @return App|null An instance of the App class in the 'development' environment, or null in other environments.
|
||||
*/
|
||||
public function create(): ?App
|
||||
{
|
||||
// Check the application environment
|
||||
switch (APP_ENV) {
|
||||
case 'console':
|
||||
// Load the Console.php file in case of the 'console' environment
|
||||
require_once __DIR__ . '/../console/Console.php';
|
||||
break;
|
||||
case 'development':
|
||||
// Create a new instance of the App class in the 'development' environment
|
||||
return new App("HeartTrack", 1, $this->container);
|
||||
break;
|
||||
case 'html':
|
||||
// Load the index.test.php file in case of the 'html' environment
|
||||
require_once __DIR__ . '/index.test.php';
|
||||
break;
|
||||
default:
|
||||
// Handle other environment cases here, if necessary
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function AddControllers($namespacePrefix = 'App\Controller', $pathToControllers = __DIR__ . '/controller'): self
|
||||
{
|
||||
$controllerFiles = glob($pathToControllers . '/*.php');
|
||||
|
||||
foreach ($controllerFiles as $file) {
|
||||
// Get class name from file name
|
||||
$class = basename($file, '.php');
|
||||
$fullClassName = $namespacePrefix . '\\' . $class;
|
||||
if (!class_exists($fullClassName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Use reflection to check if class extends BaseController
|
||||
$reflectionClass = new \ReflectionClass($fullClassName);
|
||||
if ($reflectionClass->isSubclassOf(BaseController::class)) {
|
||||
// Register in DI container
|
||||
$this->container->set($fullClassName, function () use ($fullClassName) {
|
||||
$controllerInstance = new $fullClassName();
|
||||
$controllerInstance->setContainer($this->container);
|
||||
return $controllerInstance;
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getServiceRegistered(): array
|
||||
{
|
||||
return $this->services;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Shared\Log;
|
||||
use Twig\Environment;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
|
||||
class Container implements ContainerInterface
|
||||
{
|
||||
private array $entries = [];
|
||||
|
||||
public function get(string $id)
|
||||
{
|
||||
|
||||
if ($this->has($id)) {
|
||||
$entry = $this->entries[$id];
|
||||
if (is_callable($entry)) {
|
||||
|
||||
return $entry($this);
|
||||
}
|
||||
|
||||
$id = $entry;
|
||||
}
|
||||
|
||||
return $this->resolve($id);
|
||||
}
|
||||
|
||||
|
||||
public function has(string $id): bool
|
||||
{
|
||||
return isset($this->entries[$id]);
|
||||
}
|
||||
|
||||
public function set(string $id, callable|string $concrete): void
|
||||
{
|
||||
$this->entries[$id] = $concrete;
|
||||
|
||||
}
|
||||
|
||||
public function resolve(string $id)
|
||||
{
|
||||
// 1. Inspect the class that we are trying to get from the container
|
||||
try {
|
||||
$reflectionClass = new \ReflectionClass($id);
|
||||
} catch (\ReflectionException $e) {
|
||||
throw new \Exception($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
if (!$reflectionClass->isInstantiable()) {
|
||||
throw new \Exception('Class "' . $id . '" is not instantiable');
|
||||
}
|
||||
|
||||
// 2. Inspect the constructor of the class
|
||||
$constructor = $reflectionClass->getConstructor();
|
||||
|
||||
if (!$constructor) {
|
||||
return new $id;
|
||||
}
|
||||
|
||||
// 3. Inspect the constructor parameters (dependencies)
|
||||
$parameters = $constructor->getParameters();
|
||||
|
||||
if (!$parameters) {
|
||||
return new $id;
|
||||
}
|
||||
|
||||
// 4. If the constructor parameter is a class then try to resolve that class using the container
|
||||
$dependencies = array_map(
|
||||
function (\ReflectionParameter $param) use ($id) {
|
||||
$name = $param->getName();
|
||||
$type = $param->getType();
|
||||
|
||||
// Check for a default value
|
||||
if ($param->isDefaultValueAvailable()) {
|
||||
return $param->getDefaultValue();
|
||||
}
|
||||
|
||||
if (!$type) {
|
||||
throw new \Exception(
|
||||
'Failed to resolve class "' . $id . '" because param "' . $name . '" is missing a type hint'
|
||||
);
|
||||
}
|
||||
|
||||
if ($type instanceof \ReflectionUnionType) {
|
||||
throw new \Exception(
|
||||
'Failed to resolve class "' . $id . '" because of union type for param "' . $name . '"'
|
||||
);
|
||||
}
|
||||
|
||||
if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
|
||||
return $this->get($type->getName());
|
||||
}
|
||||
|
||||
throw new \Exception(
|
||||
'Failed to resolve class "' . $id . '" because invalid param "' . $name . '"'
|
||||
);
|
||||
},
|
||||
$parameters
|
||||
);
|
||||
|
||||
return $reflectionClass->newInstanceArgs($dependencies);
|
||||
}
|
||||
|
||||
public function getAllRegisteredClassNames(): array
|
||||
{
|
||||
return array_keys($this->entries);
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Container;
|
||||
use App\Router\Response\RedirectResponse;
|
||||
use App\Router\Response\Response;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
|
||||
abstract class BaseController
|
||||
{
|
||||
protected ContainerInterface $container;
|
||||
|
||||
public function setContainer(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
abstract public function index(): Response;
|
||||
|
||||
protected function renderView(string $view, array $parameters = []): string
|
||||
{
|
||||
if (!$this->container->has(\Twig\Environment::class)) {
|
||||
throw new \LogicException('You cannot use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
|
||||
}
|
||||
|
||||
return $this->container->get(\Twig\Environment::class)->render($view, $parameters);
|
||||
}
|
||||
/**
|
||||
* Renders a view.
|
||||
*
|
||||
* If an invalid form is found in the list of parameters, a 422 status code is returned.
|
||||
* Forms found in parameters are auto-cast to form views.
|
||||
*/
|
||||
protected function render(string $view, array $parameters = [], Response $response = null): Response
|
||||
{
|
||||
$content = $this->renderView($view, $parameters);
|
||||
$response ??= new Response();
|
||||
|
||||
/* if (200 === $response->getStatusCode()) {
|
||||
foreach ($parameters as $v) {
|
||||
if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
|
||||
$response->setStatusCode(422);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
$response->setContent($content);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
protected function redirect(string $url, int $status = 302): RedirectResponse
|
||||
{
|
||||
return new RedirectResponse($url, $status);
|
||||
}
|
||||
|
||||
protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
|
||||
{
|
||||
return $this->redirect($this->generateUrl($route, $parameters), $status);
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO : Should hanle ierror if the route is not existing
|
||||
* */
|
||||
protected function generateUrl(string $route, array $parameters = []): string
|
||||
{
|
||||
return $this->container->get(\App\Router\Router::class)->generate($route, $parameters);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Container;
|
||||
use App\Router\Request\IRequest;
|
||||
use App\Router\Response\Response;
|
||||
use Shared\Attributes\Route;
|
||||
use Twig\Environment;
|
||||
|
||||
// TODO : Remove this BaseClass
|
||||
class Controller extends BaseController
|
||||
{
|
||||
private Environment $twig;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
session_start();
|
||||
}
|
||||
|
||||
#[Route(path: '/', name: 'home', methods: ['GET'])] // 8
|
||||
public function index(): Response
|
||||
{
|
||||
return $this->render('./page/home.html.twig',[
|
||||
'pp' => "test2",
|
||||
'user' => "Ladmion le Cochon",
|
||||
'role' => "Athlète",
|
||||
'friendship' => [],
|
||||
'analyzes' => [],
|
||||
'mails' => [],
|
||||
'users' => [],
|
||||
'infoUser' => [],
|
||||
'exos' => [],
|
||||
'member' => []
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(path: '/exercice', name: 'exercice', methods: ['GET'])] // 8
|
||||
public function exercice(): Response
|
||||
{
|
||||
return $this->render('./page/exercice.html.twig',[
|
||||
'pp' => "test2",
|
||||
'user' => "Ladmion le Cochon",
|
||||
'role' => "Athlète",
|
||||
'friendship' => [],
|
||||
'analyzes' => [],
|
||||
'mails' => [],
|
||||
'users' => [],
|
||||
'infoUser' => [],
|
||||
'exos' => [],
|
||||
'member' => []
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(path: '/friend', name: 'friend', methods: ['GET'])] // 8
|
||||
public function friend(): Response
|
||||
{
|
||||
return $this->render('./page/friend.html.twig',[
|
||||
'pp' => "test2",
|
||||
'user' => "Ladmion le Cochon",
|
||||
'role' => "Athlète",
|
||||
'friendship' => [],
|
||||
'analyzes' => [],
|
||||
'mails' => [],
|
||||
'users' => [],
|
||||
'infoUser' => [],
|
||||
'exos' => [],
|
||||
'member' => []
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(path: '/coaching', name: 'coaching', methods: ['GET'])] // 8
|
||||
public function coaching(): Response
|
||||
{
|
||||
return $this->render('./page/coaching.html.twig',[
|
||||
'pp' => "test2",
|
||||
'user' => "Ladmion le Cochon",
|
||||
'role' => "Athlète",
|
||||
'friendship' => [],
|
||||
'analyzes' => [],
|
||||
'mails' => [],
|
||||
'users' => [],
|
||||
'infoUser' => [],
|
||||
'exos' => [],
|
||||
'member' => []
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(path: '/mail', name: 'mail', methods: ['GET'])] // 8
|
||||
public function mail(): Response
|
||||
{
|
||||
return $this->render('./page/mail.html.twig',[
|
||||
'pp' => "test2",
|
||||
'user' => "Ladmion le Cochon",
|
||||
'role' => "Athlète",
|
||||
'friendship' => [],
|
||||
'analyzes' => [],
|
||||
'mails' => [],
|
||||
'users' => [],
|
||||
'infoUser' => [],
|
||||
'exos' => [],
|
||||
'member' => []
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(path: '/import', name: 'import', methods: ['GET'])] // 8
|
||||
public function import(): Response
|
||||
{
|
||||
return $this->render('./page/import.html.twig',[
|
||||
'pp' => "test2",
|
||||
'user' => "Ladmion le Cochon",
|
||||
'role' => "Athlète",
|
||||
'friendship' => [],
|
||||
'analyzes' => [],
|
||||
'mails' => [],
|
||||
'users' => [],
|
||||
'infoUser' => [],
|
||||
'exos' => [],
|
||||
'member' => []
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#[Route(path: '/hello', name: 'hello', methods: ['GET'])]
|
||||
public function hello(): Response
|
||||
{
|
||||
return new Response('Hello');
|
||||
}
|
||||
|
||||
#[Route(path: '/hi', name: 'hi', methods: ['GET'])]
|
||||
public function hi(string $name, IRequest $req): Response
|
||||
{
|
||||
return new Response($name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
namespace App\Controller;
|
||||
use App\Container;
|
||||
use App\Router\Request\IRequest;
|
||||
use App\Router\Response\Response;
|
||||
use App\Router\Router;
|
||||
use Shared\Exception\NotFoundHttpException;
|
||||
use Shared\Exception\NotImplementedException;
|
||||
use Shared\IArgumentResolver;
|
||||
use Shared\Log;
|
||||
|
||||
class FrontController {
|
||||
private Router $router;
|
||||
|
||||
private Container $container;
|
||||
|
||||
public function __construct(Router $router, Container $container) {
|
||||
$this->router = $router;
|
||||
$this->container = $container;
|
||||
|
||||
}
|
||||
|
||||
public function dispatch(IRequest $request) {
|
||||
try {
|
||||
$match = $this->router->match($request);
|
||||
if (!is_null($match)) {
|
||||
$method = $match['target'];
|
||||
|
||||
$controller = $this->getController($match['target']);
|
||||
$callable = array($controller,$method[1]);
|
||||
$request->addToBody($match['params']);
|
||||
|
||||
if (!is_callable($callable)){
|
||||
throw new NotImplementedException('Controller target is not callable' .'Handle when route target is not a callable : not handle');
|
||||
}
|
||||
$argumentResolver = $this->container->get(IArgumentResolver::class);
|
||||
$arguments = $argumentResolver->getArguments($request, $callable);
|
||||
// check role
|
||||
$response = call_user_func_array($callable, $arguments);
|
||||
|
||||
// should handle response properly like if it's a HTML, STING, JSON,....
|
||||
$response->send();
|
||||
} else {
|
||||
$this->handleError(404, "Page not found");
|
||||
}
|
||||
} catch (NotFoundHttpException $e) {
|
||||
$this->handleError(404, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function getController($controllerSpec) {
|
||||
if (is_array($controllerSpec)) {
|
||||
$controllerName = $controllerSpec[0];
|
||||
} else {
|
||||
$controllerName = $controllerSpec;
|
||||
}
|
||||
|
||||
return $this->container->get($controllerName);
|
||||
}
|
||||
|
||||
// TODO : Don't work need Antoine help
|
||||
private function handleError(int $statusCode, $message) : void {
|
||||
if (!$this->container->has(\Twig\Environment::class)) {
|
||||
throw new \LogicException('You cannot use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
|
||||
}
|
||||
|
||||
$response = new Response($this->container->get(\Twig\Environment::class)->render('./errorbase.html.twig',['title'=> $message , "nb" => $statusCode, "name" => $message, "descr" => $message ]),$statusCode);
|
||||
$response->send();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
namespace App\Router;
|
||||
|
||||
/**
|
||||
* Represents a single route in the application.
|
||||
*/
|
||||
class Route
|
||||
{
|
||||
/**
|
||||
* The name of the route.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
private ?string $name;
|
||||
|
||||
/**
|
||||
* The path for the route.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private string $path;
|
||||
|
||||
/**
|
||||
* The callable to be executed when the route is matched.
|
||||
*
|
||||
* @var callable
|
||||
*/
|
||||
private $callable;
|
||||
|
||||
/**
|
||||
* Constructor for the Route.
|
||||
*
|
||||
* @param string $path The path for the route.
|
||||
* @param callable $callable The callable to be executed for this route.
|
||||
* @param array|null $params Optional parameters for the route.
|
||||
* @param string|null $name Optional name for the route.
|
||||
*/
|
||||
public function __construct(string $path, callable $callable, array $params = null, string $name = null)
|
||||
{
|
||||
$this->path = $path;
|
||||
$this->callable = $callable;
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the route.
|
||||
*
|
||||
* @return string|null The name of the route.
|
||||
*/
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of the route.
|
||||
*
|
||||
* @param string|null $name The name to set.
|
||||
*/
|
||||
public function setName(?string $name): void
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the callable associated with the route.
|
||||
*
|
||||
* @return callable The callable for this route.
|
||||
*/
|
||||
public function getCallable()
|
||||
{
|
||||
return $this->callable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the path for the route.
|
||||
*
|
||||
* @return string The path for the route.
|
||||
*/
|
||||
public function getPath(): string
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the callable for the route.
|
||||
*
|
||||
* @param callable $callable The callable to set for this route.
|
||||
*/
|
||||
public function setCallable(callable $callable)
|
||||
{
|
||||
$this->callable = $callable;
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace App\Router;
|
||||
use App\Router\Request\IRequest;
|
||||
/**
|
||||
* Router class to manage a collection of routes in the application.
|
||||
* It provides functionalities to add routes and check if a given URL matches any of the defined routes.
|
||||
*/
|
||||
class Router {
|
||||
|
||||
/**
|
||||
* The base path for routing.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private string $path;
|
||||
|
||||
/**
|
||||
* Collection of routes.
|
||||
*
|
||||
* @var \AltoRouter
|
||||
*/
|
||||
private \AltoRouter $routes;
|
||||
|
||||
/**
|
||||
* Supported HTTP verbs.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
public static $verbs = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE'];
|
||||
|
||||
/**
|
||||
* Constructor for Router.
|
||||
*
|
||||
* @param string $path The base path for the router.
|
||||
*/
|
||||
public function __construct(string $path = "/PHP/project/index.php") {
|
||||
$this->path = $path;
|
||||
$this->routes = new \AltoRouter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new Route to the collection.
|
||||
*
|
||||
* @param string $method The HTTP method.
|
||||
* @param Route $route The route object.
|
||||
* @throws \InvalidArgumentException If method is not supported.
|
||||
*/
|
||||
public function add(string $method, Route $route) {
|
||||
if (!in_array($method, self::$verbs)) {
|
||||
throw new \InvalidArgumentException("Method not supported");
|
||||
}
|
||||
$this->routes->map($method, $route->getPath(), $route->getCallable(), $route->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a route for a controller action.
|
||||
*
|
||||
* @param string $method The HTTP method.
|
||||
* @param string $path The path for the route.
|
||||
* @param mixed $controller The controller object.
|
||||
* @param string $action The action method in the controller.
|
||||
* @param string $name (Optional) The name of the route.
|
||||
* @throws \InvalidArgumentException If method is not supported.
|
||||
*/
|
||||
public function addControllerRoute(string $method, string $path, $controller, string $action, string $name = '') {
|
||||
if (!in_array($method, self::$verbs)) {
|
||||
throw new \InvalidArgumentException("Method not supported");
|
||||
}
|
||||
$this->routes->map($method, $path, [$controller, $action], $name);
|
||||
}
|
||||
|
||||
// TODO: Implement the extractParams method.
|
||||
// public function extractParams(string $path) {}
|
||||
|
||||
/**
|
||||
* Adds a GET route.
|
||||
*
|
||||
* @param string $path The path for the route.
|
||||
* @param callable $callable The callback function.
|
||||
* @param string $name The name of the route.
|
||||
*/
|
||||
public function get(string $path, callable $callable, $name) {
|
||||
$this->routes->map('GET', $path, $callable, $name);
|
||||
}
|
||||
|
||||
// Similar methods for post, put, etc. can be added here.
|
||||
|
||||
/**
|
||||
* Checks if the request can be processed.
|
||||
*
|
||||
* @param IRequest $request The request object.
|
||||
* @return array|null The matched route or null if no match.
|
||||
*/
|
||||
public function match(IRequest $request): ?array {
|
||||
return $this->routes->match($request->getRequestUri(), $request->getMethod()) ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all routes.
|
||||
*
|
||||
* @return array The array of routes.
|
||||
*/
|
||||
public function getRoutes() {
|
||||
return []; // TODO: Implement the actual logic to return routes.
|
||||
}
|
||||
|
||||
|
||||
public function generate (string $routeName, array $params = array()): string
|
||||
{
|
||||
return $this->routes->generate($routeName,$params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,121 @@
|
||||
<?php
|
||||
/*
|
||||
Use the static method getInstance to get the object.
|
||||
*/
|
||||
namespace App\Router;
|
||||
|
||||
class Session
|
||||
{
|
||||
const SESSION_STARTED = TRUE;
|
||||
const SESSION_NOT_STARTED = FALSE;
|
||||
|
||||
// The state of the session
|
||||
private $sessionState = self::SESSION_NOT_STARTED;
|
||||
|
||||
// THE only instance of the class
|
||||
private static $instance;
|
||||
|
||||
|
||||
private function __construct() {}
|
||||
|
||||
|
||||
/**
|
||||
* Returns THE instance of 'Session'.
|
||||
* The session is automatically initialized if it wasn't.
|
||||
*
|
||||
* @return object
|
||||
**/
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if ( !isset(self::$instance))
|
||||
{
|
||||
self::$instance = new self;
|
||||
}
|
||||
|
||||
self::$instance->startSession();
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* (Re)starts the session.
|
||||
*
|
||||
* @return bool TRUE if the session has been initialized, else FALSE.
|
||||
**/
|
||||
|
||||
private function startSession()
|
||||
{
|
||||
if ( $this->sessionState == self::SESSION_NOT_STARTED )
|
||||
{
|
||||
$this->sessionState = session_start();
|
||||
}
|
||||
|
||||
return $this->sessionState;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stores datas in the session.
|
||||
* Example: $instance->foo = 'bar';
|
||||
*
|
||||
* @param name Name of the datas.
|
||||
* @param value Your datas.
|
||||
* @return void
|
||||
**/
|
||||
|
||||
public function __set( $name , $value )
|
||||
{
|
||||
$_SESSION[$name] = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets datas from the session.
|
||||
* Example: echo $instance->foo;
|
||||
*
|
||||
* @param name Name of the datas to get.
|
||||
* @return mixed Datas stored in session.
|
||||
**/
|
||||
|
||||
public function __get( string $name )
|
||||
{
|
||||
if ( isset($_SESSION[$name]))
|
||||
{
|
||||
return $_SESSION[$name];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function __isset( $name )
|
||||
{
|
||||
return isset($_SESSION[$name]);
|
||||
}
|
||||
|
||||
|
||||
public function __unset( $name )
|
||||
{
|
||||
unset( $_SESSION[$name] );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Destroys the current session.
|
||||
*
|
||||
* @return bool TRUE is session has been deleted, else FALSE.
|
||||
**/
|
||||
|
||||
public function destroy()
|
||||
{
|
||||
if ( $this->sessionState == self::SESSION_STARTED )
|
||||
{
|
||||
$this->sessionState = !session_destroy();
|
||||
unset( $_SESSION );
|
||||
|
||||
return !$this->sessionState;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace App\Router\Middleware;
|
||||
use App\Router\Request\IRequest;
|
||||
use Shared\Validation\Validator;
|
||||
|
||||
class RequestValidationMiddleware extends Middleware {
|
||||
private $validator;
|
||||
private $rules;
|
||||
|
||||
public function __construct(Validator $validator, array $rules) {
|
||||
$this->validator = $validator;
|
||||
$this->rules = $rules;
|
||||
}
|
||||
|
||||
public function handle(IRequest $request, callable $next) {
|
||||
$this->validateRequest($request);
|
||||
return parent::handle($request, $next);
|
||||
}
|
||||
|
||||
private function validateRequest(IRequest $request) {
|
||||
foreach ($this->rules as $param => $ruleSet) {
|
||||
foreach ($ruleSet as $rule) {
|
||||
$this->validator->rule($param, $rule['callback'], $rule['message']);
|
||||
}
|
||||
}
|
||||
|
||||
$requestData = array_merge($request->getQueryParameters(), $request->getRequestParameters());
|
||||
$this->validator->assert($requestData);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// $validationRules = [
|
||||
// 'email' => [
|
||||
// ['callback' => Validator::required(), 'message' => 'Email is required.'],
|
||||
// ['callback' => Validator::email(), 'message' => 'Email must be a valid email address.']
|
||||
// ],
|
||||
// // Add more rules as needed
|
||||
// ];
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace App\Router\Middleware;
|
||||
|
||||
use App\Router\Request\IRequest;
|
||||
|
||||
interface IHttpMiddleware {
|
||||
public function handle(IRequest $request, callable $next);
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace App\Router\Middleware;
|
||||
|
||||
use App\Router\Request\IRequest;
|
||||
|
||||
class LoggingMiddleware extends Middleware {
|
||||
public function handle(IRequest $request, callable $next) {
|
||||
// Logique de journalisation
|
||||
echo "LoggingMiddleware: Log request - Method: {$request->getMethod()}, URI: {$request->getRequestUri()}\n";
|
||||
return parent::handle($request, $next);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Router\Middleware;
|
||||
|
||||
use App\Router\Request\IRequest;
|
||||
|
||||
abstract class Middleware implements IHttpMiddleware {
|
||||
protected $next;
|
||||
|
||||
public function setNext(IHttpMiddleware $nextMiddleware) {
|
||||
$this->next = $nextMiddleware;
|
||||
}
|
||||
|
||||
public function handle(IRequest $request, callable $next) {
|
||||
if ($this->next !== null) {
|
||||
return $this->next->handle($request, $next);
|
||||
}
|
||||
return $next($request);
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace App\Router\Request;
|
||||
|
||||
interface ContentStrategy {
|
||||
public function getContent(): array;
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace App\Router\Request;
|
||||
|
||||
// should maybe change this
|
||||
class ContentStrategyFactory {
|
||||
private static $strategyMap = [
|
||||
'application/json' => JsonContentStrategy::class,
|
||||
// Format...
|
||||
];
|
||||
|
||||
public static function createContentStrategy(string $contentType, string $requestMethod): ContentStrategy {
|
||||
foreach (self::$strategyMap as $type => $className) {
|
||||
if ($contentType === $type || in_array($requestMethod, ['PUT', 'PATCH', 'DELETE'])) {
|
||||
return new $className();
|
||||
}
|
||||
}
|
||||
return new FormContentStrategy();
|
||||
}
|
||||
|
||||
public static function registerStrategy(string $contentType, string $className): void {
|
||||
self::$strategyMap[$contentType] = $className;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace App\Router\Request;
|
||||
|
||||
|
||||
class FormContentStrategy implements ContentStrategy {
|
||||
public function getContent(): array {
|
||||
return $_POST;
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace App\Router\Request;
|
||||
|
||||
class HttpRequest implements IRequest {
|
||||
private $queryParameters;
|
||||
private $requestParameters;
|
||||
private $method;
|
||||
private $requestUri;
|
||||
private $headers;
|
||||
|
||||
private array $body;
|
||||
|
||||
public function __construct(
|
||||
array $query,
|
||||
array $server,
|
||||
array $headers,
|
||||
ContentStrategy $contentStrategy,
|
||||
array $body
|
||||
) {
|
||||
$this->queryParameters = $query;
|
||||
$this->requestUri = $server['REQUEST_URI'] ?? '';
|
||||
$this->method = strtoupper($server['REQUEST_METHOD'] ?? 'GET');
|
||||
$this->headers = $headers;
|
||||
$this->requestParameters = $contentStrategy->getContent();
|
||||
$this->body = $body;
|
||||
}
|
||||
|
||||
public function getQueryParameters(): array {
|
||||
return $this->queryParameters;
|
||||
}
|
||||
|
||||
public function getRequestParameters(): array {
|
||||
return $this->requestParameters;
|
||||
}
|
||||
|
||||
public function getMethod(): string {
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
public function getRequestUri(): string {
|
||||
return $this->requestUri;
|
||||
}
|
||||
|
||||
public function getHeaders(): array {
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
public function getBody(): array{
|
||||
return $this->body;
|
||||
}
|
||||
public function addToBody(string|array $attributes){
|
||||
$this->body[] = $attributes;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace App\Router\Request;
|
||||
|
||||
|
||||
interface IRequest
|
||||
{
|
||||
public function getRequestUri();
|
||||
|
||||
public function getBody();
|
||||
public function addToBody(string|array $attributes);
|
||||
|
||||
public function getHeaders();
|
||||
public function getMethod();
|
||||
public function getQueryParameters(): array;
|
||||
public function getRequestParameters(): array;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace App\Router\Request;
|
||||
|
||||
class JsonContentStrategy implements ContentStrategy {
|
||||
public function getContent(): array {
|
||||
$rawContent = file_get_contents('php://input');
|
||||
return json_decode($rawContent, true) ?? [];
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace App\Router\Request;
|
||||
|
||||
class RequestFactory {
|
||||
|
||||
public static function createFromGlobals(): IRequest {
|
||||
$query = $_GET;
|
||||
$server = $_SERVER;
|
||||
$headers = self::getRequestHeaders();
|
||||
|
||||
$contentType = $headers['Content-Type'] ?? '';
|
||||
$contentStrategy = ContentStrategyFactory::createContentStrategy($contentType, $server['REQUEST_METHOD']);
|
||||
|
||||
return new HttpRequest($query, $server, $headers, $contentStrategy,[]);
|
||||
}
|
||||
// should not be heare
|
||||
private static function getRequestHeaders(): array {
|
||||
$headers = [];
|
||||
foreach ($_SERVER as $key => $value) {
|
||||
if (substr($key, 0, 5) === 'HTTP_') {
|
||||
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
|
||||
$headers[$header] = $value;
|
||||
}
|
||||
}
|
||||
return $headers;
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace App\Router\Response;
|
||||
|
||||
interface IResponse {
|
||||
public function getContent(): string;
|
||||
public function setContent(string $content): void;
|
||||
public function getStatusCode(): int;
|
||||
public function setStatusCode(int $statusCode): void;
|
||||
public function getHeaders(): array;
|
||||
public function setHeader(string $key, string $value): void;
|
||||
public function send(): void;
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Router\Response;
|
||||
|
||||
class RedirectResponse implements IResponse
|
||||
{
|
||||
private $content;
|
||||
private $statusCode;
|
||||
private $headers;
|
||||
private $url;
|
||||
|
||||
public function __construct(string $url, int $statusCode = 302, array $headers = [])
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->statusCode = $statusCode;
|
||||
$this->headers = $headers;
|
||||
$this->content = '';
|
||||
}
|
||||
|
||||
public function getContent(): string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function setContent(string $content): void
|
||||
{
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
|
||||
public function setStatusCode(int $statusCode): void
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
}
|
||||
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
public function setHeader(string $key, string $value): void
|
||||
{
|
||||
$this->headers[$key] = $value;
|
||||
}
|
||||
|
||||
public function send(): void
|
||||
{
|
||||
http_response_code($this->statusCode);
|
||||
|
||||
foreach ($this->headers as $name => $value) {
|
||||
header("$name: $value");
|
||||
}
|
||||
|
||||
header("Location: " . $this->url);
|
||||
|
||||
// Optionally echo content if any
|
||||
echo $this->content;
|
||||
|
||||
exit();
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace App\Router\Response;
|
||||
|
||||
class Response implements IResponse {
|
||||
private string $content;
|
||||
private int $statusCode;
|
||||
private array $headers;
|
||||
|
||||
public function __construct(string $content = "", int $statusCode = 200, array $headers = []) {
|
||||
$this->content = $content;
|
||||
$this->statusCode = $statusCode;
|
||||
$this->headers = $headers;
|
||||
}
|
||||
|
||||
public function getContent(): string {
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function setContent(string $content): void {
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function getStatusCode(): int {
|
||||
return $this->statusCode;
|
||||
}
|
||||
|
||||
public function setStatusCode(int $statusCode): void {
|
||||
$this->statusCode = $statusCode;
|
||||
}
|
||||
|
||||
public function getHeaders(): array {
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
public function setHeader(string $key, string $value): void {
|
||||
$this->headers[$key] = $value;
|
||||
}
|
||||
|
||||
public function send(): void {
|
||||
foreach ($this->headers as $key => $value) {
|
||||
header("{$key}: {$value}");
|
||||
}
|
||||
http_response_code($this->statusCode);
|
||||
echo $this->content;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Views\Directives;
|
||||
use App\Router\Router;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class Navigate extends AbstractExtension
|
||||
{
|
||||
private Router $router;
|
||||
public function __construct( Router $router)
|
||||
{
|
||||
$this->router = $router;
|
||||
}
|
||||
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('navigate', [$this, 'getPath']),
|
||||
];
|
||||
}
|
||||
public function getPath(string $name, array $parameters = []): string
|
||||
{
|
||||
return $this->router->generate($name, $parameters);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
namespace Shared;
|
||||
|
||||
use App\Router\Request\IRequest;
|
||||
/**
|
||||
* Responsible for resolving the arguments passed to a controller action.
|
||||
*/
|
||||
class ArgumentControllerResolver implements IArgumentResolver{
|
||||
|
||||
/**
|
||||
* Resolves and returns the arguments for a given controller callable.
|
||||
*
|
||||
* @param IRequest $request The HTTP request object.
|
||||
* @param callable $controller The controller callable.
|
||||
* @return array An array of resolved arguments.
|
||||
* @throws \ReflectionException If the controller method does not exist.
|
||||
* @throws \InvalidArgumentException If an argument cannot be resolved.
|
||||
*/
|
||||
public function getArguments(IRequest $request, callable $controller): array
|
||||
{
|
||||
try {
|
||||
|
||||
// Check if $controller is an array and has two elements (class and method)
|
||||
if (is_array($controller) && count($controller) === 2) {
|
||||
$className = is_object($controller[0]) ? get_class($controller[0]) : $controller[0];
|
||||
$methodName = $controller[1];
|
||||
$reflectionMethod = new \ReflectionMethod($className, $methodName);
|
||||
} else {
|
||||
// Handle other types of callables if needed
|
||||
throw new \InvalidArgumentException("Invalid controller callable format.");
|
||||
}
|
||||
} catch (\ReflectionException $e) {
|
||||
throw new \InvalidArgumentException("Controller method error: " . $e->getMessage());
|
||||
}
|
||||
|
||||
$args = [];
|
||||
foreach ($reflectionMethod->getParameters() as $param) {
|
||||
if (IRequest::class === $param->getType()->getName() || is_subclass_of($param->getType()->getName(), IRequest::class)) {
|
||||
$args[] = $request;
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $param->getName();
|
||||
$value = $this->getFromRequest($name, $request);
|
||||
|
||||
if ($value === null && $param->isDefaultValueAvailable()) {
|
||||
$value = $param->getDefaultValue();
|
||||
} elseif ($value === null) {
|
||||
throw new \InvalidArgumentException("Missing argument: $name");
|
||||
}
|
||||
|
||||
$args[] = $value;
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a value from the request based on a key.
|
||||
*
|
||||
* @param string $key The key to look for in the request.
|
||||
* @param IRequest $req The request object.
|
||||
* @return mixed The value from the request or null if not found.
|
||||
*/
|
||||
public function getFromRequest(string $key, IRequest $req): mixed
|
||||
{
|
||||
$body = $req->getBody();
|
||||
if (array_key_exists($key, $body)) {
|
||||
return $body[$key];
|
||||
}
|
||||
|
||||
$queryParams = $req->getQueryParameters();
|
||||
if (array_key_exists($key, $queryParams)) {
|
||||
return $queryParams[$key];
|
||||
}
|
||||
|
||||
$requestParams = $req->getRequestParameters();
|
||||
if (array_key_exists($key, $requestParams)) {
|
||||
return $requestParams[$key];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace Shared;
|
||||
|
||||
use App\Router\Request\IRequest;
|
||||
|
||||
/**
|
||||
* Interface for classes that resolve arguments for controller methods.
|
||||
*/
|
||||
interface IArgumentResolver
|
||||
{
|
||||
/**
|
||||
* Resolves the arguments for a controller method based on the given request.
|
||||
*
|
||||
* @param IRequest $request The request object.
|
||||
* @param callable $controller The controller callable.
|
||||
* @return array An array of arguments resolved for the controller method.
|
||||
*/
|
||||
public function getArguments(IRequest $request, callable $controller): array;
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace Shared;
|
||||
|
||||
class Log {
|
||||
const INFO = 'INFO';
|
||||
const WARNING = 'WARNING';
|
||||
const ERROR = 'ERROR';
|
||||
|
||||
private static int $logCount = 0;
|
||||
private static array $logs = [];
|
||||
|
||||
private static function getLogStyle(string $level): string {
|
||||
return match ($level) {
|
||||
self::WARNING => "background-color: #fff3cd; color: #856404; border-color: #ffeeba",
|
||||
self::ERROR => "background-color: #f8d7da; color: #721c24; border-color: #f5c6cb",
|
||||
default => "background-color: #f8f8f8; color: #383d41; border-color: #ccc",
|
||||
};
|
||||
}
|
||||
|
||||
private static function addLog($something, string $level, string $label): bool|string
|
||||
{
|
||||
$style = self::getLogStyle($level);
|
||||
|
||||
ob_start();
|
||||
echo "<pre style='$style; padding: 10px;'>";
|
||||
echo "<strong>$label [$level] (Log " . self::$logCount . ")</strong><br>";
|
||||
var_dump($something);
|
||||
echo "</pre>";
|
||||
$logOutput = ob_get_clean();
|
||||
|
||||
self::$logs[] = ['level' => $level, 'output' => $logOutput];
|
||||
return $logOutput;
|
||||
}
|
||||
|
||||
public static function dd($something, string $label = "LOGGER", string $level = self::INFO) {
|
||||
echo self::addLog($something, $level, $label);
|
||||
die();
|
||||
}
|
||||
|
||||
public static function ddArray(array $array, string $label = "LOGGER", string $level = self::INFO) {
|
||||
self::dd($array, $label, $level);
|
||||
}
|
||||
|
||||
public static function log($something, string $label = "LOGGER", string $level = self::INFO) {
|
||||
self::$logCount++;
|
||||
echo self::addLog($something, $level, $label);
|
||||
}
|
||||
|
||||
public static function getLogCount(): int
|
||||
{
|
||||
return self::$logCount;
|
||||
}
|
||||
|
||||
public static function getLogsByLevel(string $level): array
|
||||
{
|
||||
return array_filter(self::$logs, function($log) use ($level) {
|
||||
return $log['level'] === $level;
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Shared\Attributes;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_CLASS)]
|
||||
class Route
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
private string|array $path = '',
|
||||
private ?string $name = null,
|
||||
private array|string $methods = ['GET'],
|
||||
)
|
||||
{}
|
||||
|
||||
public function getPath(): array| string{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
public function getName(): ?string{
|
||||
return $this->name;
|
||||
}
|
||||
public function getMethods(): array | string
|
||||
{
|
||||
return $this->methods;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace Shared\Exception;
|
||||
|
||||
class HttpException extends \RuntimeException
|
||||
{
|
||||
private int $statusCode;
|
||||
private array $headers;
|
||||
|
||||
public function __construct(int $statusCode, string $message = '', \Throwable $previous = null, array $headers = [], int $code = 0)
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
$this->headers = $headers;
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setHeaders(array $headers)
|
||||
{
|
||||
$this->headers = $headers;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace Shared\Exception;
|
||||
|
||||
|
||||
class NotFoundHttpException extends HttpException
|
||||
{
|
||||
public function __construct(string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
|
||||
{
|
||||
parent::__construct(404, $message, $previous, $headers, $code);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Shared\Exception;
|
||||
|
||||
final class NotImplementedException extends \Exception
|
||||
{
|
||||
public $message = "Not implemented method call";
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace Shared\Exception;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
class ValidationException extends Exception {
|
||||
protected array $errors;
|
||||
|
||||
#[Pure] public function __construct(array $errors, $message = "Validation errors occurred", $code = 0, Exception $previous = null) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->errors = $errors;
|
||||
}
|
||||
|
||||
public function getErrors(): array
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
return __CLASS__ . ": [{$this->code}]: {$this->message}\n" . $this->formatErrors();
|
||||
}
|
||||
|
||||
protected function formatErrors(): string
|
||||
{
|
||||
return implode("\n", array_map(function ($error) {
|
||||
return "- {$error}";
|
||||
}, $this->errors));
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
language: php
|
||||
php:
|
||||
- 5.3
|
||||
- 5.4
|
||||
- 5.5
|
||||
|
||||
script: phpunit --coverage-text ./
|
@ -0,0 +1,270 @@
|
||||
<?php
|
||||
|
||||
class AltoRouter {
|
||||
|
||||
protected $routes = array();
|
||||
protected $namedRoutes = array();
|
||||
protected $basePath = '';
|
||||
protected $matchTypes = array(
|
||||
'i' => '[0-9]++',
|
||||
'a' => '[0-9A-Za-z]++',
|
||||
'h' => '[0-9A-Fa-f]++',
|
||||
'*' => '.+?',
|
||||
'**' => '.++',
|
||||
'' => '[^/\.]++'
|
||||
);
|
||||
|
||||
/**
|
||||
* Create router in one call from config.
|
||||
*
|
||||
* @param array $routes
|
||||
* @param string $basePath
|
||||
* @param array $matchTypes
|
||||
*/
|
||||
public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) {
|
||||
$this->addRoutes($routes);
|
||||
$this->setBasePath($basePath);
|
||||
$this->addMatchTypes($matchTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add multiple routes at once from array in the following format:
|
||||
*
|
||||
* $routes = array(
|
||||
* array($method, $route, $target, $name)
|
||||
* );
|
||||
*
|
||||
* @param array $routes
|
||||
* @return void
|
||||
* @author Koen Punt
|
||||
*/
|
||||
public function addRoutes($routes){
|
||||
if(!is_array($routes) && !$routes instanceof Traversable) {
|
||||
throw new \Exception('Routes should be an array or an instance of Traversable');
|
||||
}
|
||||
foreach($routes as $route) {
|
||||
call_user_func_array(array($this, 'map'), $route);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the base path.
|
||||
* Useful if you are running your application from a subdirectory.
|
||||
*/
|
||||
public function setBasePath($basePath) {
|
||||
$this->basePath = $basePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add named match types. It uses array_merge so keys can be overwritten.
|
||||
*
|
||||
* @param array $matchTypes The key is the name and the value is the regex.
|
||||
*/
|
||||
public function addMatchTypes($matchTypes) {
|
||||
$this->matchTypes = array_merge($this->matchTypes, $matchTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a route to a target
|
||||
*
|
||||
* @param string $method One of 4 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PUT|DELETE)
|
||||
* @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id]
|
||||
* @param mixed $target The target where this route should point to. Can be anything.
|
||||
* @param string $name Optional name of this route. Supply if you want to reverse route this url in your application.
|
||||
*/
|
||||
public function map($method, $route, $target, $name = null) {
|
||||
|
||||
$this->routes[] = array($method, $route, $target, $name);
|
||||
|
||||
if($name) {
|
||||
if(isset($this->namedRoutes[$name])) {
|
||||
throw new \Exception("Can not redeclare route '{$name}'");
|
||||
} else {
|
||||
$this->namedRoutes[$name] = $route;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reversed routing
|
||||
*
|
||||
* Generate the URL for a named route. Replace regexes with supplied parameters
|
||||
*
|
||||
* @param string $routeName The name of the route.
|
||||
* @param array @params Associative array of parameters to replace placeholders with.
|
||||
* @return string The URL of the route with named parameters in place.
|
||||
*/
|
||||
public function generate($routeName, array $params = array()) {
|
||||
|
||||
// Check if named route exists
|
||||
if(!isset($this->namedRoutes[$routeName])) {
|
||||
throw new \Exception("Route '{$routeName}' does not exist.");
|
||||
}
|
||||
|
||||
// Replace named parameters
|
||||
$route = $this->namedRoutes[$routeName];
|
||||
|
||||
// prepend base path to route url again
|
||||
$url = $this->basePath . $route;
|
||||
|
||||
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
|
||||
|
||||
foreach($matches as $match) {
|
||||
list($block, $pre, $type, $param, $optional) = $match;
|
||||
|
||||
if ($pre) {
|
||||
$block = substr($block, 1);
|
||||
}
|
||||
|
||||
if(isset($params[$param])) {
|
||||
$url = str_replace($block, $params[$param], $url);
|
||||
} elseif ($optional) {
|
||||
$url = str_replace($pre . $block, '', $url);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Match a given Request Url against stored routes
|
||||
* @param string $requestUrl
|
||||
* @param string $requestMethod
|
||||
* @return array|boolean Array with route information on success, false on failure (no match).
|
||||
*/
|
||||
public function match($requestUrl = null, $requestMethod = null) {
|
||||
|
||||
$params = array();
|
||||
$match = false;
|
||||
|
||||
// set Request Url if it isn't passed as parameter
|
||||
if($requestUrl === null) {
|
||||
$requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
|
||||
}
|
||||
|
||||
// strip base path from request url
|
||||
$requestUrl = substr($requestUrl, strlen($this->basePath));
|
||||
|
||||
// Strip query string (?a=b) from Request Url
|
||||
if (($strpos = strpos($requestUrl, '?')) !== false) {
|
||||
$requestUrl = substr($requestUrl, 0, $strpos);
|
||||
}
|
||||
|
||||
// set Request Method if it isn't passed as a parameter
|
||||
if($requestMethod === null) {
|
||||
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
|
||||
}
|
||||
|
||||
// Force request_order to be GP
|
||||
// http://www.mail-archive.com/internals@lists.php.net/msg33119.html
|
||||
$_REQUEST = array_merge($_GET, $_POST);
|
||||
|
||||
foreach($this->routes as $handler) {
|
||||
list($method, $_route, $target, $name) = $handler;
|
||||
|
||||
$methods = explode('|', $method);
|
||||
$method_match = false;
|
||||
|
||||
// Check if request method matches. If not, abandon early. (CHEAP)
|
||||
foreach($methods as $method) {
|
||||
if (strcasecmp($requestMethod, $method) === 0) {
|
||||
$method_match = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Method did not match, continue to next route.
|
||||
if(!$method_match) continue;
|
||||
|
||||
// Check for a wildcard (matches all)
|
||||
if ($_route === '*') {
|
||||
$match = true;
|
||||
} elseif (isset($_route[0]) && $_route[0] === '@') {
|
||||
$match = preg_match('`' . substr($_route, 1) . '`u', $requestUrl, $params);
|
||||
} else {
|
||||
$route = null;
|
||||
$regex = false;
|
||||
$j = 0;
|
||||
$n = isset($_route[0]) ? $_route[0] : null;
|
||||
$i = 0;
|
||||
|
||||
// Find the longest non-regex substring and match it against the URI
|
||||
while (true) {
|
||||
if (!isset($_route[$i])) {
|
||||
break;
|
||||
} elseif (false === $regex) {
|
||||
$c = $n;
|
||||
$regex = $c === '[' || $c === '(' || $c === '.';
|
||||
if (false === $regex && false !== isset($_route[$i+1])) {
|
||||
$n = $_route[$i + 1];
|
||||
$regex = $n === '?' || $n === '+' || $n === '*' || $n === '{';
|
||||
}
|
||||
if (false === $regex && $c !== '/' && (!isset($requestUrl[$j]) || $c !== $requestUrl[$j])) {
|
||||
continue 2;
|
||||
}
|
||||
$j++;
|
||||
}
|
||||
$route .= $_route[$i++];
|
||||
}
|
||||
|
||||
$regex = $this->compileRoute($route);
|
||||
$match = preg_match($regex, $requestUrl, $params);
|
||||
}
|
||||
|
||||
if(($match == true || $match > 0)) {
|
||||
|
||||
if($params) {
|
||||
foreach($params as $key => $value) {
|
||||
if(is_numeric($key)) unset($params[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'target' => $target,
|
||||
'params' => $params,
|
||||
'name' => $name
|
||||
);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile the regex for a given route (EXPENSIVE)
|
||||
*/
|
||||
private function compileRoute($route) {
|
||||
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
|
||||
|
||||
$matchTypes = $this->matchTypes;
|
||||
foreach($matches as $match) {
|
||||
list($block, $pre, $type, $param, $optional) = $match;
|
||||
|
||||
if (isset($matchTypes[$type])) {
|
||||
$type = $matchTypes[$type];
|
||||
}
|
||||
if ($pre === '.') {
|
||||
$pre = '\.';
|
||||
}
|
||||
|
||||
//Older versions of PCRE require the 'P' in (?P<named>)
|
||||
$pattern = '(?:'
|
||||
. ($pre !== '' ? $pre : null)
|
||||
. '('
|
||||
. ($param !== '' ? "?P<$param>" : null)
|
||||
. $type
|
||||
. '))'
|
||||
. ($optional !== '' ? '?' : null);
|
||||
|
||||
$route = str_replace($block, $pattern, $route);
|
||||
}
|
||||
|
||||
}
|
||||
return "`^$route$`u";
|
||||
}
|
||||
}
|
@ -0,0 +1,423 @@
|
||||
<?php
|
||||
|
||||
require 'AltoRouter.php';
|
||||
|
||||
class AltoRouterDebug extends AltoRouter{
|
||||
|
||||
public function getNamedRoutes(){
|
||||
return $this->namedRoutes;
|
||||
}
|
||||
|
||||
public function getRoutes(){
|
||||
return $this->routes;
|
||||
}
|
||||
|
||||
public function getBasePath(){
|
||||
return $this->basePath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SimpleTraversable implements Iterator{
|
||||
|
||||
protected $_position = 0;
|
||||
|
||||
protected $_data = array(
|
||||
array('GET', '/foo', 'foo_action', null),
|
||||
array('POST', '/bar', 'bar_action', 'second_route')
|
||||
);
|
||||
|
||||
public function current(){
|
||||
return $this->_data[$this->_position];
|
||||
}
|
||||
public function key(){
|
||||
return $this->_position;
|
||||
}
|
||||
public function next(){
|
||||
++$this->_position;
|
||||
}
|
||||
public function rewind(){
|
||||
$this->_position = 0;
|
||||
}
|
||||
public function valid(){
|
||||
return isset($this->_data[$this->_position]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-07-14 at 17:47:46.
|
||||
*/
|
||||
class AltoRouterTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var AltoRouter
|
||||
*/
|
||||
protected $router;
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, opens a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->router = new AltoRouterDebug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the fixture, for example, closes a network connection.
|
||||
* This method is called after a test is executed.
|
||||
*/
|
||||
protected function tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addRoutes
|
||||
*/
|
||||
public function testAddRoutes()
|
||||
{
|
||||
$method = 'POST';
|
||||
$route = '/[:controller]/[:action]';
|
||||
$target = function(){};
|
||||
|
||||
$this->router->addRoutes(array(
|
||||
array($method, $route, $target),
|
||||
array($method, $route, $target, 'second_route')
|
||||
));
|
||||
|
||||
$routes = $this->router->getRoutes();
|
||||
|
||||
$this->assertEquals(array($method, $route, $target, null), $routes[0]);
|
||||
$this->assertEquals(array($method, $route, $target, 'second_route'), $routes[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addRoutes
|
||||
*/
|
||||
public function testAddRoutesAcceptsTraverable()
|
||||
{
|
||||
$traversable = new SimpleTraversable();
|
||||
$this->router->addRoutes($traversable);
|
||||
|
||||
$traversable->rewind();
|
||||
|
||||
$first = $traversable->current();
|
||||
$traversable->next();
|
||||
$second = $traversable->current();
|
||||
|
||||
$routes = $this->router->getRoutes();
|
||||
|
||||
$this->assertEquals($first, $routes[0]);
|
||||
$this->assertEquals($second, $routes[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addRoutes
|
||||
* @expectedException Exception
|
||||
*/
|
||||
public function testAddRoutesThrowsExceptionOnInvalidArgument()
|
||||
{
|
||||
$this->router->addRoutes(new stdClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::setBasePath
|
||||
*/
|
||||
public function testSetBasePath()
|
||||
{
|
||||
$basePath = $this->router->setBasePath('/some/path');
|
||||
$this->assertEquals('/some/path', $this->router->getBasePath());
|
||||
|
||||
$basePath = $this->router->setBasePath('/some/path');
|
||||
$this->assertEquals('/some/path', $this->router->getBasePath());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::map
|
||||
*/
|
||||
public function testMap()
|
||||
{
|
||||
$method = 'POST';
|
||||
$route = '/[:controller]/[:action]';
|
||||
$target = function(){};
|
||||
|
||||
$this->router->map($method, $route, $target);
|
||||
|
||||
$routes = $this->router->getRoutes();
|
||||
|
||||
$this->assertEquals(array($method, $route, $target, null), $routes[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::map
|
||||
*/
|
||||
public function testMapWithName()
|
||||
{
|
||||
$method = 'POST';
|
||||
$route = '/[:controller]/[:action]';
|
||||
$target = function(){};
|
||||
$name = 'myroute';
|
||||
|
||||
$this->router->map($method, $route, $target, $name);
|
||||
|
||||
$routes = $this->router->getRoutes();
|
||||
$this->assertEquals(array($method, $route, $target, $name), $routes[0]);
|
||||
|
||||
$named_routes = $this->router->getNamedRoutes();
|
||||
$this->assertEquals($route, $named_routes[$name]);
|
||||
|
||||
try{
|
||||
$this->router->map($method, $route, $target, $name);
|
||||
$this->fail('Should not be able to add existing named route');
|
||||
}catch(Exception $e){
|
||||
$this->assertEquals("Can not redeclare route '{$name}'", $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::generate
|
||||
*/
|
||||
public function testGenerate()
|
||||
{
|
||||
$params = array(
|
||||
'controller' => 'test',
|
||||
'action' => 'someaction'
|
||||
);
|
||||
|
||||
$this->router->map('GET', '/[:controller]/[:action]', function(){}, 'foo_route');
|
||||
|
||||
$this->assertEquals('/test/someaction',
|
||||
$this->router->generate('foo_route', $params));
|
||||
|
||||
$params = array(
|
||||
'controller' => 'test',
|
||||
'action' => 'someaction',
|
||||
'type' => 'json'
|
||||
);
|
||||
|
||||
$this->assertEquals('/test/someaction',
|
||||
$this->router->generate('foo_route', $params));
|
||||
|
||||
}
|
||||
|
||||
public function testGenerateWithOptionalUrlParts()
|
||||
{
|
||||
$this->router->map('GET', '/[:controller]/[:action].[:type]?', function(){}, 'bar_route');
|
||||
|
||||
$params = array(
|
||||
'controller' => 'test',
|
||||
'action' => 'someaction'
|
||||
);
|
||||
|
||||
$this->assertEquals('/test/someaction',
|
||||
$this->router->generate('bar_route', $params));
|
||||
|
||||
$params = array(
|
||||
'controller' => 'test',
|
||||
'action' => 'someaction',
|
||||
'type' => 'json'
|
||||
);
|
||||
|
||||
$this->assertEquals('/test/someaction.json',
|
||||
$this->router->generate('bar_route', $params));
|
||||
}
|
||||
|
||||
public function testGenerateWithNonexistingRoute()
|
||||
{
|
||||
try{
|
||||
$this->router->generate('nonexisting_route');
|
||||
$this->fail('Should trigger an exception on nonexisting named route');
|
||||
}catch(Exception $e){
|
||||
$this->assertEquals("Route 'nonexisting_route' does not exist.", $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::match
|
||||
* @covers AltoRouter::compileRoute
|
||||
*/
|
||||
public function testMatch()
|
||||
{
|
||||
$this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'foo_action',
|
||||
'params' => array(
|
||||
'controller' => 'test',
|
||||
'action' => 'do'
|
||||
),
|
||||
'name' => 'foo_route'
|
||||
), $this->router->match('/foo/test/do', 'GET'));
|
||||
|
||||
$this->assertFalse($this->router->match('/foo/test/do', 'POST'));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'foo_action',
|
||||
'params' => array(
|
||||
'controller' => 'test',
|
||||
'action' => 'do'
|
||||
),
|
||||
'name' => 'foo_route'
|
||||
), $this->router->match('/foo/test/do?param=value', 'GET'));
|
||||
|
||||
}
|
||||
|
||||
public function testMatchWithFixedParamValues()
|
||||
{
|
||||
$this->router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'usersController#doAction',
|
||||
'params' => array(
|
||||
'id' => 1,
|
||||
'action' => 'delete'
|
||||
),
|
||||
'name' => 'users_do'
|
||||
), $this->router->match('/users/1/delete', 'POST'));
|
||||
|
||||
$this->assertFalse($this->router->match('/users/1/delete', 'GET'));
|
||||
$this->assertFalse($this->router->match('/users/abc/delete', 'POST'));
|
||||
$this->assertFalse($this->router->match('/users/1/create', 'GET'));
|
||||
}
|
||||
|
||||
public function testMatchWithServerVars()
|
||||
{
|
||||
$this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route');
|
||||
|
||||
$_SERVER['REQUEST_URI'] = '/foo/test/do';
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'foo_action',
|
||||
'params' => array(
|
||||
'controller' => 'test',
|
||||
'action' => 'do'
|
||||
),
|
||||
'name' => 'foo_route'
|
||||
), $this->router->match());
|
||||
}
|
||||
|
||||
public function testMatchWithOptionalUrlParts()
|
||||
{
|
||||
$this->router->map('GET', '/bar/[:controller]/[:action].[:type]?', 'bar_action', 'bar_route');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'bar_action',
|
||||
'params' => array(
|
||||
'controller' => 'test',
|
||||
'action' => 'do',
|
||||
'type' => 'json'
|
||||
),
|
||||
'name' => 'bar_route'
|
||||
), $this->router->match('/bar/test/do.json', 'GET'));
|
||||
|
||||
}
|
||||
|
||||
public function testMatchWithWildcard()
|
||||
{
|
||||
$this->router->map('GET', '/a', 'foo_action', 'foo_route');
|
||||
$this->router->map('GET', '*', 'bar_action', 'bar_route');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'bar_action',
|
||||
'params' => array(),
|
||||
'name' => 'bar_route'
|
||||
), $this->router->match('/everything', 'GET'));
|
||||
|
||||
}
|
||||
|
||||
public function testMatchWithCustomRegexp()
|
||||
{
|
||||
$this->router->map('GET', '@^/[a-z]*$', 'bar_action', 'bar_route');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'bar_action',
|
||||
'params' => array(),
|
||||
'name' => 'bar_route'
|
||||
), $this->router->match('/everything', 'GET'));
|
||||
|
||||
$this->assertFalse($this->router->match('/some-other-thing', 'GET'));
|
||||
|
||||
}
|
||||
|
||||
public function testMatchWithUnicodeRegex()
|
||||
{
|
||||
$pattern = '/(?<path>[^';
|
||||
// Arabic characters
|
||||
$pattern .= '\x{0600}-\x{06FF}';
|
||||
$pattern .= '\x{FB50}-\x{FDFD}';
|
||||
$pattern .= '\x{FE70}-\x{FEFF}';
|
||||
$pattern .= '\x{0750}-\x{077F}';
|
||||
// Alphanumeric, /, _, - and space characters
|
||||
$pattern .= 'a-zA-Z0-9\/_-\s';
|
||||
// 'ZERO WIDTH NON-JOINER'
|
||||
$pattern .= '\x{200C}';
|
||||
$pattern .= ']+)';
|
||||
|
||||
$this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'unicode_action',
|
||||
'name' => 'unicode_route',
|
||||
'params' => array(
|
||||
'path' => '大家好'
|
||||
)
|
||||
), $this->router->match('/大家好', 'GET'));
|
||||
|
||||
$this->assertFalse($this->router->match('/﷽', 'GET'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addMatchTypes
|
||||
*/
|
||||
public function testMatchWithCustomNamedRegex()
|
||||
{
|
||||
$this->router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
|
||||
$this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'bar_action',
|
||||
'params' => array(
|
||||
'customId' => 'AB1',
|
||||
),
|
||||
'name' => 'bar_route'
|
||||
), $this->router->match('/bar/AB1', 'GET'));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'bar_action',
|
||||
'params' => array(
|
||||
'customId' => 'AB1_0123456789',
|
||||
),
|
||||
'name' => 'bar_route'
|
||||
), $this->router->match('/bar/AB1_0123456789', 'GET'));
|
||||
|
||||
$this->assertFalse($this->router->match('/some-other-thing', 'GET'));
|
||||
|
||||
}
|
||||
|
||||
public function testMatchWithCustomNamedUnicodeRegex()
|
||||
{
|
||||
$pattern = '[^';
|
||||
// Arabic characters
|
||||
$pattern .= '\x{0600}-\x{06FF}';
|
||||
$pattern .= '\x{FB50}-\x{FDFD}';
|
||||
$pattern .= '\x{FE70}-\x{FEFF}';
|
||||
$pattern .= '\x{0750}-\x{077F}';
|
||||
$pattern .= ']+';
|
||||
|
||||
$this->router->addMatchTypes(array('nonArabic' => $pattern));
|
||||
$this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'non_arabic_action',
|
||||
'name' => 'non_arabic_route',
|
||||
'params' => array(
|
||||
'string' => 'some-path'
|
||||
)
|
||||
), $this->router->match('/bar/some-path', 'GET'));
|
||||
|
||||
$this->assertFalse($this->router->match('/﷽', 'GET'));
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
# AltoRouter [](http://travis-ci.org/dannyvankooten/AltoRouter)
|
||||
AltoRouter is a small but powerful routing class for PHP 5.3+, heavily inspired by [klein.php](https://github.com/chriso/klein.php/).
|
||||
|
||||
* Dynamic routing with named parameters
|
||||
* Reversed routing
|
||||
* Flexible regular expression routing (inspired by [Sinatra](http://www.sinatrarb.com/))
|
||||
* Custom regexes
|
||||
|
||||
## Getting started
|
||||
|
||||
1. PHP 5.3.x is required
|
||||
2. Install AltoRouter using Composer or manually
|
||||
2. Setup URL rewriting so that all requests are handled by **index.php**
|
||||
3. Create an instance of AltoRouter, map your routes and match a request.
|
||||
4. Have a look at the basic example in the `examples` directory for a better understanding on how to use AltoRouter.
|
||||
|
||||
## Routing
|
||||
```php
|
||||
$router = new AltoRouter();
|
||||
$router->setBasePath('/AltoRouter'); // (optional) the subdir AltoRouter lives in
|
||||
|
||||
// mapping routes
|
||||
$router->map('GET|POST','/', 'home#index', 'home');
|
||||
$router->map('GET','/users', array('c' => 'UserController', 'a' => 'ListAction'));
|
||||
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
|
||||
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
|
||||
|
||||
// reversed routing
|
||||
$router->generate('users_show', array('id' => 5));
|
||||
|
||||
```
|
||||
|
||||
**You can use the following limits on your named parameters. AltoRouter will create the correct regexes for you.**
|
||||
|
||||
```php
|
||||
* // Match all request URIs
|
||||
[i] // Match an integer
|
||||
[i:id] // Match an integer as 'id'
|
||||
[a:action] // Match alphanumeric characters as 'action'
|
||||
[h:key] // Match hexadecimal characters as 'key'
|
||||
[:action] // Match anything up to the next / or end of the URI as 'action'
|
||||
[create|edit:action] // Match either 'create' or 'edit' as 'action'
|
||||
[*] // Catch all (lazy, stops at the next trailing slash)
|
||||
[*:trailing] // Catch all as 'trailing' (lazy)
|
||||
[**:trailing] // Catch all (possessive - will match the rest of the URI)
|
||||
.[:format]? // Match an optional parameter 'format' - a / or . before the block is also optional
|
||||
```
|
||||
|
||||
**Some more complicated examples**
|
||||
|
||||
```php
|
||||
@/(?[A-Za-z]{2}_[A-Za-z]{2})$ // custom regex, matches language codes like "en_us" etc.
|
||||
/posts/[*:title][i:id] // Matches "/posts/this-is-a-title-123"
|
||||
/output.[xml|json:format]? // Matches "/output", "output.xml", "output.json"
|
||||
/[:controller]?/[:action]? // Matches the typical /controller/action format
|
||||
```
|
||||
|
||||
**The character before the colon (the 'match type') is a shortcut for one of the following regular expressions**
|
||||
|
||||
```php
|
||||
'i' => '[0-9]++'
|
||||
'a' => '[0-9A-Za-z]++'
|
||||
'h' => '[0-9A-Fa-f]++'
|
||||
'*' => '.+?'
|
||||
'**' => '.++'
|
||||
'' => '[^/\.]++'
|
||||
```
|
||||
|
||||
**New match types can be added using the `addMatchTypes()` method**
|
||||
|
||||
```php
|
||||
$router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
|
||||
```
|
||||
|
||||
|
||||
## Contributors
|
||||
- [Danny van Kooten](https://github.com/dannyvankooten)
|
||||
- [Koen Punt](https://github.com/koenpunt)
|
||||
- [John Long](https://github.com/adduc)
|
||||
- [Niahoo Osef](https://github.com/niahoo)
|
||||
|
||||
## License
|
||||
|
||||
(MIT License)
|
||||
|
||||
Copyright (c) 2012-2013 Danny van Kooten <hi@dannyvankooten.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "altorouter/altorouter",
|
||||
"description": "A lightning fast router for PHP",
|
||||
"keywords": ["router", "routing", "lightweight"],
|
||||
"homepage": "https://github.com/dannyvankooten/AltoRouter",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Danny van Kooten",
|
||||
"email": "dannyvankooten@gmail.com",
|
||||
"homepage": "http://dannyvankooten.com/"
|
||||
},
|
||||
{
|
||||
"name": "Koen Punt",
|
||||
"homepage": "https://github.com/koenpunt"
|
||||
},
|
||||
{
|
||||
"name": "niahoo",
|
||||
"homepage": "https://github.com/niahoo"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": ["AltoRouter.php"]
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
RewriteEngine On
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteRule . index.php [L]
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
require '../../AltoRouter.php';
|
||||
|
||||
$router = new AltoRouter();
|
||||
$router->setBasePath('/AltoRouter/examples/basic');
|
||||
$router->map('GET|POST','/', 'home#index', 'home');
|
||||
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
|
||||
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
|
||||
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
|
||||
|
||||
// match current request
|
||||
$match = $router->match();
|
||||
?>
|
||||
<h1>AltoRouter</h1>
|
||||
|
||||
<h3>Current request: </h3>
|
||||
<pre>
|
||||
Target: <?php var_dump($match['target']); ?>
|
||||
Params: <?php var_dump($match['params']); ?>
|
||||
Name: <?php var_dump($match['name']); ?>
|
||||
</pre>
|
||||
|
||||
<h3>Try these requests: </h3>
|
||||
<p><a href="<?php echo $router->generate('home'); ?>">GET <?php echo $router->generate('home'); ?></a></p>
|
||||
<p><a href="<?php echo $router->generate('users_show', array('id' => 5)); ?>">GET <?php echo $router->generate('users_show', array('id' => 5)); ?></a></p>
|
||||
<p><form action="<?php echo $router->generate('users_do', array('id' => 10, 'action' => 'update')); ?>" method="post"><button type="submit"><?php echo $router->generate('users_do', array('id' => 10, 'action' => 'update')); ?></button></form></p>
|
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2020-2023 Graham Campbell <hello@gjcampbell.co.uk>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "graham-campbell/result-type",
|
||||
"description": "An Implementation Of The Result Type",
|
||||
"keywords": ["result", "result-type", "Result", "Result Type", "Result-Type", "Graham Campbell", "GrahamCampbell"],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Graham Campbell",
|
||||
"email": "hello@gjcampbell.co.uk",
|
||||
"homepage": "https://github.com/GrahamCampbell"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.2.5 || ^8.0",
|
||||
"phpoption/phpoption": "^1.9.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^8.5.34 || ^9.6.13 || ^10.4.2"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"GrahamCampbell\\ResultType\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"GrahamCampbell\\Tests\\ResultType\\": "tests/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"preferred-install": "dist"
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of Result Type.
|
||||
*
|
||||
* (c) Graham Campbell <hello@gjcampbell.co.uk>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace GrahamCampbell\ResultType;
|
||||
|
||||
use PhpOption\None;
|
||||
use PhpOption\Some;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @template E
|
||||
*
|
||||
* @extends \GrahamCampbell\ResultType\Result<T,E>
|
||||
*/
|
||||
final class Error extends Result
|
||||
{
|
||||
/**
|
||||
* @var E
|
||||
*/
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* Internal constructor for an error value.
|
||||
*
|
||||
* @param E $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function __construct($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new error value.
|
||||
*
|
||||
* @template F
|
||||
*
|
||||
* @param F $value
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<T,F>
|
||||
*/
|
||||
public static function create($value)
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the success option value.
|
||||
*
|
||||
* @return \PhpOption\Option<T>
|
||||
*/
|
||||
public function success()
|
||||
{
|
||||
return None::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the success value.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable(T):S $f
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<S,E>
|
||||
*/
|
||||
public function map(callable $f)
|
||||
{
|
||||
return self::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flat map over the success value.
|
||||
*
|
||||
* @template S
|
||||
* @template F
|
||||
*
|
||||
* @param callable(T):\GrahamCampbell\ResultType\Result<S,F> $f
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<S,F>
|
||||
*/
|
||||
public function flatMap(callable $f)
|
||||
{
|
||||
/** @var \GrahamCampbell\ResultType\Result<S,F> */
|
||||
return self::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error option value.
|
||||
*
|
||||
* @return \PhpOption\Option<E>
|
||||
*/
|
||||
public function error()
|
||||
{
|
||||
return Some::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the error value.
|
||||
*
|
||||
* @template F
|
||||
*
|
||||
* @param callable(E):F $f
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<T,F>
|
||||
*/
|
||||
public function mapError(callable $f)
|
||||
{
|
||||
return self::create($f($this->value));
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of Result Type.
|
||||
*
|
||||
* (c) Graham Campbell <hello@gjcampbell.co.uk>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace GrahamCampbell\ResultType;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @template E
|
||||
*/
|
||||
abstract class Result
|
||||
{
|
||||
/**
|
||||
* Get the success option value.
|
||||
*
|
||||
* @return \PhpOption\Option<T>
|
||||
*/
|
||||
abstract public function success();
|
||||
|
||||
/**
|
||||
* Map over the success value.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable(T):S $f
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<S,E>
|
||||
*/
|
||||
abstract public function map(callable $f);
|
||||
|
||||
/**
|
||||
* Flat map over the success value.
|
||||
*
|
||||
* @template S
|
||||
* @template F
|
||||
*
|
||||
* @param callable(T):\GrahamCampbell\ResultType\Result<S,F> $f
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<S,F>
|
||||
*/
|
||||
abstract public function flatMap(callable $f);
|
||||
|
||||
/**
|
||||
* Get the error option value.
|
||||
*
|
||||
* @return \PhpOption\Option<E>
|
||||
*/
|
||||
abstract public function error();
|
||||
|
||||
/**
|
||||
* Map over the error value.
|
||||
*
|
||||
* @template F
|
||||
*
|
||||
* @param callable(E):F $f
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<T,F>
|
||||
*/
|
||||
abstract public function mapError(callable $f);
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of Result Type.
|
||||
*
|
||||
* (c) Graham Campbell <hello@gjcampbell.co.uk>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace GrahamCampbell\ResultType;
|
||||
|
||||
use PhpOption\None;
|
||||
use PhpOption\Some;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @template E
|
||||
*
|
||||
* @extends \GrahamCampbell\ResultType\Result<T,E>
|
||||
*/
|
||||
final class Success extends Result
|
||||
{
|
||||
/**
|
||||
* @var T
|
||||
*/
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* Internal constructor for a success value.
|
||||
*
|
||||
* @param T $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function __construct($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new error value.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param S $value
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<S,E>
|
||||
*/
|
||||
public static function create($value)
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the success option value.
|
||||
*
|
||||
* @return \PhpOption\Option<T>
|
||||
*/
|
||||
public function success()
|
||||
{
|
||||
return Some::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the success value.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable(T):S $f
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<S,E>
|
||||
*/
|
||||
public function map(callable $f)
|
||||
{
|
||||
return self::create($f($this->value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Flat map over the success value.
|
||||
*
|
||||
* @template S
|
||||
* @template F
|
||||
*
|
||||
* @param callable(T):\GrahamCampbell\ResultType\Result<S,F> $f
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<S,F>
|
||||
*/
|
||||
public function flatMap(callable $f)
|
||||
{
|
||||
return $f($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error option value.
|
||||
*
|
||||
* @return \PhpOption\Option<E>
|
||||
*/
|
||||
public function error()
|
||||
{
|
||||
return None::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the error value.
|
||||
*
|
||||
* @template F
|
||||
*
|
||||
* @param callable(E):F $f
|
||||
*
|
||||
* @return \GrahamCampbell\ResultType\Result<T,F>
|
||||
*/
|
||||
public function mapError(callable $f)
|
||||
{
|
||||
return self::create($this->value);
|
||||
}
|
||||
}
|
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
@ -0,0 +1,52 @@
|
||||
{
|
||||
"name": "phpoption/phpoption",
|
||||
"description": "Option Type for PHP",
|
||||
"keywords": ["php", "option", "language", "type"],
|
||||
"license": "Apache-2.0",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Johannes M. Schmitt",
|
||||
"email": "schmittjoh@gmail.com",
|
||||
"homepage": "https://github.com/schmittjoh"
|
||||
},
|
||||
{
|
||||
"name": "Graham Campbell",
|
||||
"email": "hello@gjcampbell.co.uk",
|
||||
"homepage": "https://github.com/GrahamCampbell"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.2.5 || ^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"bamarni/composer-bin-plugin": "^1.8.2",
|
||||
"phpunit/phpunit": "^8.5.34 || ^9.6.13 || ^10.4.2"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PhpOption\\": "src/PhpOption/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"PhpOption\\Tests\\": "tests/PhpOption/Tests/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"bamarni/composer-bin-plugin": true
|
||||
},
|
||||
"preferred-install": "dist"
|
||||
},
|
||||
"extra": {
|
||||
"bamarni-bin": {
|
||||
"bin-links": true,
|
||||
"forward-command": true
|
||||
},
|
||||
"branch-alias": {
|
||||
"dev-master": "1.9-dev"
|
||||
}
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true
|
||||
}
|
@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2012 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace PhpOption;
|
||||
|
||||
use Traversable;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*
|
||||
* @extends Option<T>
|
||||
*/
|
||||
final class LazyOption extends Option
|
||||
{
|
||||
/** @var callable(mixed...):(Option<T>) */
|
||||
private $callback;
|
||||
|
||||
/** @var array<int, mixed> */
|
||||
private $arguments;
|
||||
|
||||
/** @var Option<T>|null */
|
||||
private $option;
|
||||
|
||||
/**
|
||||
* @template S
|
||||
* @param callable(mixed...):(Option<S>) $callback
|
||||
* @param array<int, mixed> $arguments
|
||||
*
|
||||
* @return LazyOption<S>
|
||||
*/
|
||||
public static function create($callback, array $arguments = []): self
|
||||
{
|
||||
return new self($callback, $arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable(mixed...):(Option<T>) $callback
|
||||
* @param array<int, mixed> $arguments
|
||||
*/
|
||||
public function __construct($callback, array $arguments = [])
|
||||
{
|
||||
if (!is_callable($callback)) {
|
||||
throw new \InvalidArgumentException('Invalid callback given');
|
||||
}
|
||||
|
||||
$this->callback = $callback;
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
public function isDefined(): bool
|
||||
{
|
||||
return $this->option()->isDefined();
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return $this->option()->isEmpty();
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
return $this->option()->get();
|
||||
}
|
||||
|
||||
public function getOrElse($default)
|
||||
{
|
||||
return $this->option()->getOrElse($default);
|
||||
}
|
||||
|
||||
public function getOrCall($callable)
|
||||
{
|
||||
return $this->option()->getOrCall($callable);
|
||||
}
|
||||
|
||||
public function getOrThrow(\Exception $ex)
|
||||
{
|
||||
return $this->option()->getOrThrow($ex);
|
||||
}
|
||||
|
||||
public function orElse(Option $else)
|
||||
{
|
||||
return $this->option()->orElse($else);
|
||||
}
|
||||
|
||||
public function ifDefined($callable)
|
||||
{
|
||||
$this->option()->forAll($callable);
|
||||
}
|
||||
|
||||
public function forAll($callable)
|
||||
{
|
||||
return $this->option()->forAll($callable);
|
||||
}
|
||||
|
||||
public function map($callable)
|
||||
{
|
||||
return $this->option()->map($callable);
|
||||
}
|
||||
|
||||
public function flatMap($callable)
|
||||
{
|
||||
return $this->option()->flatMap($callable);
|
||||
}
|
||||
|
||||
public function filter($callable)
|
||||
{
|
||||
return $this->option()->filter($callable);
|
||||
}
|
||||
|
||||
public function filterNot($callable)
|
||||
{
|
||||
return $this->option()->filterNot($callable);
|
||||
}
|
||||
|
||||
public function select($value)
|
||||
{
|
||||
return $this->option()->select($value);
|
||||
}
|
||||
|
||||
public function reject($value)
|
||||
{
|
||||
return $this->option()->reject($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Traversable<T>
|
||||
*/
|
||||
public function getIterator(): Traversable
|
||||
{
|
||||
return $this->option()->getIterator();
|
||||
}
|
||||
|
||||
public function foldLeft($initialValue, $callable)
|
||||
{
|
||||
return $this->option()->foldLeft($initialValue, $callable);
|
||||
}
|
||||
|
||||
public function foldRight($initialValue, $callable)
|
||||
{
|
||||
return $this->option()->foldRight($initialValue, $callable);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Option<T>
|
||||
*/
|
||||
private function option(): Option
|
||||
{
|
||||
if (null === $this->option) {
|
||||
/** @var mixed */
|
||||
$option = call_user_func_array($this->callback, $this->arguments);
|
||||
if ($option instanceof Option) {
|
||||
$this->option = $option;
|
||||
} else {
|
||||
throw new \RuntimeException(sprintf('Expected instance of %s', Option::class));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->option;
|
||||
}
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2012 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace PhpOption;
|
||||
|
||||
use EmptyIterator;
|
||||
|
||||
/**
|
||||
* @extends Option<mixed>
|
||||
*/
|
||||
final class None extends Option
|
||||
{
|
||||
/** @var None|null */
|
||||
private static $instance;
|
||||
|
||||
/**
|
||||
* @return None
|
||||
*/
|
||||
public static function create(): self
|
||||
{
|
||||
if (null === self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
throw new \RuntimeException('None has no value.');
|
||||
}
|
||||
|
||||
public function getOrCall($callable)
|
||||
{
|
||||
return $callable();
|
||||
}
|
||||
|
||||
public function getOrElse($default)
|
||||
{
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function getOrThrow(\Exception $ex)
|
||||
{
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isDefined(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function orElse(Option $else)
|
||||
{
|
||||
return $else;
|
||||
}
|
||||
|
||||
public function ifDefined($callable)
|
||||
{
|
||||
// Just do nothing in that case.
|
||||
}
|
||||
|
||||
public function forAll($callable)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function map($callable)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function flatMap($callable)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function filter($callable)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function filterNot($callable)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function select($value)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function reject($value)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getIterator(): EmptyIterator
|
||||
{
|
||||
return new EmptyIterator();
|
||||
}
|
||||
|
||||
public function foldLeft($initialValue, $callable)
|
||||
{
|
||||
return $initialValue;
|
||||
}
|
||||
|
||||
public function foldRight($initialValue, $callable)
|
||||
{
|
||||
return $initialValue;
|
||||
}
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,434 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2012 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace PhpOption;
|
||||
|
||||
use ArrayAccess;
|
||||
use IteratorAggregate;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*
|
||||
* @implements IteratorAggregate<T>
|
||||
*/
|
||||
abstract class Option implements IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* Creates an option given a return value.
|
||||
*
|
||||
* This is intended for consuming existing APIs and allows you to easily
|
||||
* convert them to an option. By default, we treat ``null`` as the None
|
||||
* case, and everything else as Some.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param S $value The actual return value.
|
||||
* @param S $noneValue The value which should be considered "None"; null by
|
||||
* default.
|
||||
*
|
||||
* @return Option<S>
|
||||
*/
|
||||
public static function fromValue($value, $noneValue = null)
|
||||
{
|
||||
if ($value === $noneValue) {
|
||||
return None::create();
|
||||
}
|
||||
|
||||
return new Some($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an option from an array's value.
|
||||
*
|
||||
* If the key does not exist in the array, the array is not actually an
|
||||
* array, or the array's value at the given key is null, None is returned.
|
||||
* Otherwise, Some is returned wrapping the value at the given key.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param array<string|int,S>|ArrayAccess<string|int,S>|null $array A potential array or \ArrayAccess value.
|
||||
* @param string $key The key to check.
|
||||
*
|
||||
* @return Option<S>
|
||||
*/
|
||||
public static function fromArraysValue($array, $key)
|
||||
{
|
||||
if (!(is_array($array) || $array instanceof ArrayAccess) || !isset($array[$key])) {
|
||||
return None::create();
|
||||
}
|
||||
|
||||
return new Some($array[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a lazy-option with the given callback.
|
||||
*
|
||||
* This is also a helper constructor for lazy-consuming existing APIs where
|
||||
* the return value is not yet an option. By default, we treat ``null`` as
|
||||
* None case, and everything else as Some.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable $callback The callback to evaluate.
|
||||
* @param array $arguments The arguments for the callback.
|
||||
* @param S $noneValue The value which should be considered "None";
|
||||
* null by default.
|
||||
*
|
||||
* @return LazyOption<S>
|
||||
*/
|
||||
public static function fromReturn($callback, array $arguments = [], $noneValue = null)
|
||||
{
|
||||
return new LazyOption(static function () use ($callback, $arguments, $noneValue) {
|
||||
/** @var mixed */
|
||||
$return = call_user_func_array($callback, $arguments);
|
||||
|
||||
if ($return === $noneValue) {
|
||||
return None::create();
|
||||
}
|
||||
|
||||
return new Some($return);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Option factory, which creates new option based on passed value.
|
||||
*
|
||||
* If value is already an option, it simply returns. If value is callable,
|
||||
* LazyOption with passed callback created and returned. If Option
|
||||
* returned from callback, it returns directly. On other case value passed
|
||||
* to Option::fromValue() method.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param Option<S>|callable|S $value
|
||||
* @param S $noneValue Used when $value is mixed or
|
||||
* callable, for None-check.
|
||||
*
|
||||
* @return Option<S>|LazyOption<S>
|
||||
*/
|
||||
public static function ensure($value, $noneValue = null)
|
||||
{
|
||||
if ($value instanceof self) {
|
||||
return $value;
|
||||
} elseif (is_callable($value)) {
|
||||
return new LazyOption(static function () use ($value, $noneValue) {
|
||||
/** @var mixed */
|
||||
$return = $value();
|
||||
|
||||
if ($return instanceof self) {
|
||||
return $return;
|
||||
} else {
|
||||
return self::fromValue($return, $noneValue);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return self::fromValue($value, $noneValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lift a function so that it accepts Option as parameters.
|
||||
*
|
||||
* We return a new closure that wraps the original callback. If any of the
|
||||
* parameters passed to the lifted function is empty, the function will
|
||||
* return a value of None. Otherwise, we will pass all parameters to the
|
||||
* original callback and return the value inside a new Option, unless an
|
||||
* Option is returned from the function, in which case, we use that.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable $callback
|
||||
* @param mixed $noneValue
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
public static function lift($callback, $noneValue = null)
|
||||
{
|
||||
return static function () use ($callback, $noneValue) {
|
||||
/** @var array<int, mixed> */
|
||||
$args = func_get_args();
|
||||
|
||||
$reduced_args = array_reduce(
|
||||
$args,
|
||||
/** @param bool $status */
|
||||
static function ($status, self $o) {
|
||||
return $o->isEmpty() ? true : $status;
|
||||
},
|
||||
false
|
||||
);
|
||||
// if at least one parameter is empty, return None
|
||||
if ($reduced_args) {
|
||||
return None::create();
|
||||
}
|
||||
|
||||
$args = array_map(
|
||||
/** @return T */
|
||||
static function (self $o) {
|
||||
// it is safe to do so because the fold above checked
|
||||
// that all arguments are of type Some
|
||||
/** @var T */
|
||||
return $o->get();
|
||||
},
|
||||
$args
|
||||
);
|
||||
|
||||
return self::ensure(call_user_func_array($callback, $args), $noneValue);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value if available, or throws an exception otherwise.
|
||||
*
|
||||
* @throws \RuntimeException If value is not available.
|
||||
*
|
||||
* @return T
|
||||
*/
|
||||
abstract public function get();
|
||||
|
||||
/**
|
||||
* Returns the value if available, or the default value if not.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param S $default
|
||||
*
|
||||
* @return T|S
|
||||
*/
|
||||
abstract public function getOrElse($default);
|
||||
|
||||
/**
|
||||
* Returns the value if available, or the results of the callable.
|
||||
*
|
||||
* This is preferable over ``getOrElse`` if the computation of the default
|
||||
* value is expensive.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable():S $callable
|
||||
*
|
||||
* @return T|S
|
||||
*/
|
||||
abstract public function getOrCall($callable);
|
||||
|
||||
/**
|
||||
* Returns the value if available, or throws the passed exception.
|
||||
*
|
||||
* @param \Exception $ex
|
||||
*
|
||||
* @return T
|
||||
*/
|
||||
abstract public function getOrThrow(\Exception $ex);
|
||||
|
||||
/**
|
||||
* Returns true if no value is available, false otherwise.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function isEmpty();
|
||||
|
||||
/**
|
||||
* Returns true if a value is available, false otherwise.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function isDefined();
|
||||
|
||||
/**
|
||||
* Returns this option if non-empty, or the passed option otherwise.
|
||||
*
|
||||
* This can be used to try multiple alternatives, and is especially useful
|
||||
* with lazy evaluating options:
|
||||
*
|
||||
* ```php
|
||||
* $repo->findSomething()
|
||||
* ->orElse(new LazyOption(array($repo, 'findSomethingElse')))
|
||||
* ->orElse(new LazyOption(array($repo, 'createSomething')));
|
||||
* ```
|
||||
*
|
||||
* @param Option<T> $else
|
||||
*
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function orElse(self $else);
|
||||
|
||||
/**
|
||||
* This is similar to map() below except that the return value has no meaning;
|
||||
* the passed callable is simply executed if the option is non-empty, and
|
||||
* ignored if the option is empty.
|
||||
*
|
||||
* In all cases, the return value of the callable is discarded.
|
||||
*
|
||||
* ```php
|
||||
* $comment->getMaybeFile()->ifDefined(function($file) {
|
||||
* // Do something with $file here.
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* If you're looking for something like ``ifEmpty``, you can use ``getOrCall``
|
||||
* and ``getOrElse`` in these cases.
|
||||
*
|
||||
* @deprecated Use forAll() instead.
|
||||
*
|
||||
* @param callable(T):mixed $callable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function ifDefined($callable);
|
||||
|
||||
/**
|
||||
* This is similar to map() except that the return value of the callable has no meaning.
|
||||
*
|
||||
* The passed callable is simply executed if the option is non-empty, and ignored if the
|
||||
* option is empty. This method is preferred for callables with side-effects, while map()
|
||||
* is intended for callables without side-effects.
|
||||
*
|
||||
* @param callable(T):mixed $callable
|
||||
*
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function forAll($callable);
|
||||
|
||||
/**
|
||||
* Applies the callable to the value of the option if it is non-empty,
|
||||
* and returns the return value of the callable wrapped in Some().
|
||||
*
|
||||
* If the option is empty, then the callable is not applied.
|
||||
*
|
||||
* ```php
|
||||
* (new Some("foo"))->map('strtoupper')->get(); // "FOO"
|
||||
* ```
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable(T):S $callable
|
||||
*
|
||||
* @return Option<S>
|
||||
*/
|
||||
abstract public function map($callable);
|
||||
|
||||
/**
|
||||
* Applies the callable to the value of the option if it is non-empty, and
|
||||
* returns the return value of the callable directly.
|
||||
*
|
||||
* In contrast to ``map``, the return value of the callable is expected to
|
||||
* be an Option itself; it is not automatically wrapped in Some().
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable(T):Option<S> $callable must return an Option
|
||||
*
|
||||
* @return Option<S>
|
||||
*/
|
||||
abstract public function flatMap($callable);
|
||||
|
||||
/**
|
||||
* If the option is empty, it is returned immediately without applying the callable.
|
||||
*
|
||||
* If the option is non-empty, the callable is applied, and if it returns true,
|
||||
* the option itself is returned; otherwise, None is returned.
|
||||
*
|
||||
* @param callable(T):bool $callable
|
||||
*
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function filter($callable);
|
||||
|
||||
/**
|
||||
* If the option is empty, it is returned immediately without applying the callable.
|
||||
*
|
||||
* If the option is non-empty, the callable is applied, and if it returns false,
|
||||
* the option itself is returned; otherwise, None is returned.
|
||||
*
|
||||
* @param callable(T):bool $callable
|
||||
*
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function filterNot($callable);
|
||||
|
||||
/**
|
||||
* If the option is empty, it is returned immediately.
|
||||
*
|
||||
* If the option is non-empty, and its value does not equal the passed value
|
||||
* (via a shallow comparison ===), then None is returned. Otherwise, the
|
||||
* Option is returned.
|
||||
*
|
||||
* In other words, this will filter all but the passed value.
|
||||
*
|
||||
* @param T $value
|
||||
*
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function select($value);
|
||||
|
||||
/**
|
||||
* If the option is empty, it is returned immediately.
|
||||
*
|
||||
* If the option is non-empty, and its value does equal the passed value (via
|
||||
* a shallow comparison ===), then None is returned; otherwise, the Option is
|
||||
* returned.
|
||||
*
|
||||
* In other words, this will let all values through except the passed value.
|
||||
*
|
||||
* @param T $value
|
||||
*
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function reject($value);
|
||||
|
||||
/**
|
||||
* Binary operator for the initial value and the option's value.
|
||||
*
|
||||
* If empty, the initial value is returned. If non-empty, the callable
|
||||
* receives the initial value and the option's value as arguments.
|
||||
*
|
||||
* ```php
|
||||
*
|
||||
* $some = new Some(5);
|
||||
* $none = None::create();
|
||||
* $result = $some->foldLeft(1, function($a, $b) { return $a + $b; }); // int(6)
|
||||
* $result = $none->foldLeft(1, function($a, $b) { return $a + $b; }); // int(1)
|
||||
*
|
||||
* // This can be used instead of something like the following:
|
||||
* $option = Option::fromValue($integerOrNull);
|
||||
* $result = 1;
|
||||
* if ( ! $option->isEmpty()) {
|
||||
* $result += $option->get();
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param S $initialValue
|
||||
* @param callable(S, T):S $callable
|
||||
*
|
||||
* @return S
|
||||
*/
|
||||
abstract public function foldLeft($initialValue, $callable);
|
||||
|
||||
/**
|
||||
* foldLeft() but with reversed arguments for the callable.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param S $initialValue
|
||||
* @param callable(T, S):S $callable
|
||||
*
|
||||
* @return S
|
||||
*/
|
||||
abstract public function foldRight($initialValue, $callable);
|
||||
}
|
@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2012 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace PhpOption;
|
||||
|
||||
use ArrayIterator;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*
|
||||
* @extends Option<T>
|
||||
*/
|
||||
final class Some extends Option
|
||||
{
|
||||
/** @var T */
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* @param T $value
|
||||
*/
|
||||
public function __construct($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @template U
|
||||
*
|
||||
* @param U $value
|
||||
*
|
||||
* @return Some<U>
|
||||
*/
|
||||
public static function create($value): self
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
public function isDefined(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function getOrElse($default)
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function getOrCall($callable)
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function getOrThrow(\Exception $ex)
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function orElse(Option $else)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function ifDefined($callable)
|
||||
{
|
||||
$this->forAll($callable);
|
||||
}
|
||||
|
||||
public function forAll($callable)
|
||||
{
|
||||
$callable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function map($callable)
|
||||
{
|
||||
return new self($callable($this->value));
|
||||
}
|
||||
|
||||
public function flatMap($callable)
|
||||
{
|
||||
/** @var mixed */
|
||||
$rs = $callable($this->value);
|
||||
if (!$rs instanceof Option) {
|
||||
throw new \RuntimeException('Callables passed to flatMap() must return an Option. Maybe you should use map() instead?');
|
||||
}
|
||||
|
||||
return $rs;
|
||||
}
|
||||
|
||||
public function filter($callable)
|
||||
{
|
||||
if (true === $callable($this->value)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return None::create();
|
||||
}
|
||||
|
||||
public function filterNot($callable)
|
||||
{
|
||||
if (false === $callable($this->value)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return None::create();
|
||||
}
|
||||
|
||||
public function select($value)
|
||||
{
|
||||
if ($this->value === $value) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return None::create();
|
||||
}
|
||||
|
||||
public function reject($value)
|
||||
{
|
||||
if ($this->value === $value) {
|
||||
return None::create();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ArrayIterator<int, T>
|
||||
*/
|
||||
public function getIterator(): ArrayIterator
|
||||
{
|
||||
return new ArrayIterator([$this->value]);
|
||||
}
|
||||
|
||||
public function foldLeft($initialValue, $callable)
|
||||
{
|
||||
return $callable($initialValue, $this->value);
|
||||
}
|
||||
|
||||
public function foldRight($initialValue, $callable)
|
||||
{
|
||||
return $callable($this->value, $initialValue);
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
composer.lock
|
||||
composer.phar
|
||||
/vendor/
|
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2016 container-interop
|
||||
Copyright (c) 2016 PHP Framework Interoperability Group
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,13 @@
|
||||
Container interface
|
||||
==============
|
||||
|
||||
This repository holds all interfaces related to [PSR-11 (Container Interface)][psr-url].
|
||||
|
||||
Note that this is not a Container implementation of its own. It is merely abstractions that describe the components of a Dependency Injection Container.
|
||||
|
||||
The installable [package][package-url] and [implementations][implementation-url] are listed on Packagist.
|
||||
|
||||
[psr-url]: https://www.php-fig.org/psr/psr-11/
|
||||
[package-url]: https://packagist.org/packages/psr/container
|
||||
[implementation-url]: https://packagist.org/providers/psr/container-implementation
|
||||
|
@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "psr/container",
|
||||
"type": "library",
|
||||
"description": "Common Container Interface (PHP FIG PSR-11)",
|
||||
"keywords": ["psr", "psr-11", "container", "container-interop", "container-interface"],
|
||||
"homepage": "https://github.com/php-fig/container",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "PHP-FIG",
|
||||
"homepage": "https://www.php-fig.org/"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.4.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Psr\\Container\\": "src/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.0.x-dev"
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Psr\Container;
|
||||
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Base interface representing a generic exception in a container.
|
||||
*/
|
||||
interface ContainerExceptionInterface extends Throwable
|
||||
{
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psr\Container;
|
||||
|
||||
/**
|
||||
* Describes the interface of a container that exposes methods to read its entries.
|
||||
*/
|
||||
interface ContainerInterface
|
||||
{
|
||||
/**
|
||||
* Finds an entry of the container by its identifier and returns it.
|
||||
*
|
||||
* @param string $id Identifier of the entry to look for.
|
||||
*
|
||||
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
|
||||
* @throws ContainerExceptionInterface Error while retrieving the entry.
|
||||
*
|
||||
* @return mixed Entry.
|
||||
*/
|
||||
public function get(string $id);
|
||||
|
||||
/**
|
||||
* Returns true if the container can return an entry for the given identifier.
|
||||
* Returns false otherwise.
|
||||
*
|
||||
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
|
||||
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
|
||||
*
|
||||
* @param string $id Identifier of the entry to look for.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $id): bool;
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Psr\Container;
|
||||
|
||||
/**
|
||||
* No entry was found in the container.
|
||||
*/
|
||||
interface NotFoundExceptionInterface extends ContainerExceptionInterface
|
||||
{
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
Copyright (c) 2020-present Fabien Potencier
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
@ -0,0 +1,115 @@
|
||||
<?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\Polyfill\Php80;
|
||||
|
||||
/**
|
||||
* @author Ion Bazan <ion.bazan@gmail.com>
|
||||
* @author Nico Oelgart <nicoswd@gmail.com>
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class Php80
|
||||
{
|
||||
public static function fdiv(float $dividend, float $divisor): float
|
||||
{
|
||||
return @($dividend / $divisor);
|
||||
}
|
||||
|
||||
public static function get_debug_type($value): string
|
||||
{
|
||||
switch (true) {
|
||||
case null === $value: return 'null';
|
||||
case \is_bool($value): return 'bool';
|
||||
case \is_string($value): return 'string';
|
||||
case \is_array($value): return 'array';
|
||||
case \is_int($value): return 'int';
|
||||
case \is_float($value): return 'float';
|
||||
case \is_object($value): break;
|
||||
case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
|
||||
default:
|
||||
if (null === $type = @get_resource_type($value)) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
if ('Unknown' === $type) {
|
||||
$type = 'closed';
|
||||
}
|
||||
|
||||
return "resource ($type)";
|
||||
}
|
||||
|
||||
$class = \get_class($value);
|
||||
|
||||
if (false === strpos($class, '@')) {
|
||||
return $class;
|
||||
}
|
||||
|
||||
return (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous';
|
||||
}
|
||||
|
||||
public static function get_resource_id($res): int
|
||||
{
|
||||
if (!\is_resource($res) && null === @get_resource_type($res)) {
|
||||
throw new \TypeError(sprintf('Argument 1 passed to get_resource_id() must be of the type resource, %s given', get_debug_type($res)));
|
||||
}
|
||||
|
||||
return (int) $res;
|
||||
}
|
||||
|
||||
public static function preg_last_error_msg(): string
|
||||
{
|
||||
switch (preg_last_error()) {
|
||||
case \PREG_INTERNAL_ERROR:
|
||||
return 'Internal error';
|
||||
case \PREG_BAD_UTF8_ERROR:
|
||||
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
||||
case \PREG_BAD_UTF8_OFFSET_ERROR:
|
||||
return 'The offset did not correspond to the beginning of a valid UTF-8 code point';
|
||||
case \PREG_BACKTRACK_LIMIT_ERROR:
|
||||
return 'Backtrack limit exhausted';
|
||||
case \PREG_RECURSION_LIMIT_ERROR:
|
||||
return 'Recursion limit exhausted';
|
||||
case \PREG_JIT_STACKLIMIT_ERROR:
|
||||
return 'JIT stack limit exhausted';
|
||||
case \PREG_NO_ERROR:
|
||||
return 'No error';
|
||||
default:
|
||||
return 'Unknown error';
|
||||
}
|
||||
}
|
||||
|
||||
public static function str_contains(string $haystack, string $needle): bool
|
||||
{
|
||||
return '' === $needle || false !== strpos($haystack, $needle);
|
||||
}
|
||||
|
||||
public static function str_starts_with(string $haystack, string $needle): bool
|
||||
{
|
||||
return 0 === strncmp($haystack, $needle, \strlen($needle));
|
||||
}
|
||||
|
||||
public static function str_ends_with(string $haystack, string $needle): bool
|
||||
{
|
||||
if ('' === $needle || $needle === $haystack) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ('' === $haystack) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$needleLength = \strlen($needle);
|
||||
|
||||
return $needleLength <= \strlen($haystack) && 0 === substr_compare($haystack, $needle, -$needleLength);
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
<?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\Polyfill\Php80;
|
||||
|
||||
/**
|
||||
* @author Fedonyuk Anton <info@ensostudio.ru>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class PhpToken implements \Stringable
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $text;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $line;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $pos;
|
||||
|
||||
public function __construct(int $id, string $text, int $line = -1, int $position = -1)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->text = $text;
|
||||
$this->line = $line;
|
||||
$this->pos = $position;
|
||||
}
|
||||
|
||||
public function getTokenName(): ?string
|
||||
{
|
||||
if ('UNKNOWN' === $name = token_name($this->id)) {
|
||||
$name = \strlen($this->text) > 1 || \ord($this->text) < 32 ? null : $this->text;
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string|array $kind
|
||||
*/
|
||||
public function is($kind): bool
|
||||
{
|
||||
foreach ((array) $kind as $value) {
|
||||
if (\in_array($value, [$this->id, $this->text], true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isIgnorable(): bool
|
||||
{
|
||||
return \in_array($this->id, [\T_WHITESPACE, \T_COMMENT, \T_DOC_COMMENT, \T_OPEN_TAG], true);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return (string) $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return static[]
|
||||
*/
|
||||
public static function tokenize(string $code, int $flags = 0): array
|
||||
{
|
||||
$line = 1;
|
||||
$position = 0;
|
||||
$tokens = token_get_all($code, $flags);
|
||||
foreach ($tokens as $index => $token) {
|
||||
if (\is_string($token)) {
|
||||
$id = \ord($token);
|
||||
$text = $token;
|
||||
} else {
|
||||
[$id, $text, $line] = $token;
|
||||
}
|
||||
$tokens[$index] = new static($id, $text, $line, $position);
|
||||
$position += \strlen($text);
|
||||
}
|
||||
|
||||
return $tokens;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
Symfony Polyfill / Php80
|
||||
========================
|
||||
|
||||
This component provides features added to PHP 8.0 core:
|
||||
|
||||
- [`Stringable`](https://php.net/stringable) interface
|
||||
- [`fdiv`](https://php.net/fdiv)
|
||||
- [`ValueError`](https://php.net/valueerror) class
|
||||
- [`UnhandledMatchError`](https://php.net/unhandledmatcherror) class
|
||||
- `FILTER_VALIDATE_BOOL` constant
|
||||
- [`get_debug_type`](https://php.net/get_debug_type)
|
||||
- [`PhpToken`](https://php.net/phptoken) class
|
||||
- [`preg_last_error_msg`](https://php.net/preg_last_error_msg)
|
||||
- [`str_contains`](https://php.net/str_contains)
|
||||
- [`str_starts_with`](https://php.net/str_starts_with)
|
||||
- [`str_ends_with`](https://php.net/str_ends_with)
|
||||
- [`get_resource_id`](https://php.net/get_resource_id)
|
||||
|
||||
More information can be found in the
|
||||
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).
|
||||
|
||||
License
|
||||
=======
|
||||
|
||||
This library is released under the [MIT license](LICENSE).
|
@ -0,0 +1,31 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS)]
|
||||
final class Attribute
|
||||
{
|
||||
public const TARGET_CLASS = 1;
|
||||
public const TARGET_FUNCTION = 2;
|
||||
public const TARGET_METHOD = 4;
|
||||
public const TARGET_PROPERTY = 8;
|
||||
public const TARGET_CLASS_CONSTANT = 16;
|
||||
public const TARGET_PARAMETER = 32;
|
||||
public const TARGET_ALL = 63;
|
||||
public const IS_REPEATABLE = 64;
|
||||
|
||||
/** @var int */
|
||||
public $flags;
|
||||
|
||||
public function __construct(int $flags = self::TARGET_ALL)
|
||||
{
|
||||
$this->flags = $flags;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
if (\PHP_VERSION_ID < 80000 && extension_loaded('tokenizer')) {
|
||||
class PhpToken extends Symfony\Polyfill\Php80\PhpToken
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
if (\PHP_VERSION_ID < 80000) {
|
||||
interface Stringable
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
if (\PHP_VERSION_ID < 80000) {
|
||||
class UnhandledMatchError extends Error
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
if (\PHP_VERSION_ID < 80000) {
|
||||
class ValueError extends Error
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
use Symfony\Polyfill\Php80 as p;
|
||||
|
||||
if (\PHP_VERSION_ID >= 80000) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
|
||||
define('FILTER_VALIDATE_BOOL', \FILTER_VALIDATE_BOOLEAN);
|
||||
}
|
||||
|
||||
if (!function_exists('fdiv')) {
|
||||
function fdiv(float $num1, float $num2): float { return p\Php80::fdiv($num1, $num2); }
|
||||
}
|
||||
if (!function_exists('preg_last_error_msg')) {
|
||||
function preg_last_error_msg(): string { return p\Php80::preg_last_error_msg(); }
|
||||
}
|
||||
if (!function_exists('str_contains')) {
|
||||
function str_contains(?string $haystack, ?string $needle): bool { return p\Php80::str_contains($haystack ?? '', $needle ?? ''); }
|
||||
}
|
||||
if (!function_exists('str_starts_with')) {
|
||||
function str_starts_with(?string $haystack, ?string $needle): bool { return p\Php80::str_starts_with($haystack ?? '', $needle ?? ''); }
|
||||
}
|
||||
if (!function_exists('str_ends_with')) {
|
||||
function str_ends_with(?string $haystack, ?string $needle): bool { return p\Php80::str_ends_with($haystack ?? '', $needle ?? ''); }
|
||||
}
|
||||
if (!function_exists('get_debug_type')) {
|
||||
function get_debug_type($value): string { return p\Php80::get_debug_type($value); }
|
||||
}
|
||||
if (!function_exists('get_resource_id')) {
|
||||
function get_resource_id($resource): int { return p\Php80::get_resource_id($resource); }
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "symfony/polyfill-php80",
|
||||
"type": "library",
|
||||
"description": "Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions",
|
||||
"keywords": ["polyfill", "shim", "compatibility", "portable"],
|
||||
"homepage": "https://symfony.com",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Ion Bazan",
|
||||
"email": "ion.bazan@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Nicolas Grekas",
|
||||
"email": "p@tchwork.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": { "Symfony\\Polyfill\\Php80\\": "" },
|
||||
"files": [ "bootstrap.php" ],
|
||||
"classmap": [ "Resources/stubs" ]
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "1.28-dev"
|
||||
},
|
||||
"thanks": {
|
||||
"name": "symfony/polyfill",
|
||||
"url": "https://github.com/symfony/polyfill"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,213 +0,0 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/tools/php-cs-fixer.d/PhpdocSingleLineVarFixer.php';
|
||||
|
||||
return PhpCsFixer\Config::create()
|
||||
->registerCustomFixers([
|
||||
new \PharIo\CSFixer\PhpdocSingleLineVarFixer()
|
||||
])
|
||||
->setRiskyAllowed(true)
|
||||
->setRules(
|
||||
[
|
||||
'PharIo/phpdoc_single_line_var_fixer' => true,
|
||||
|
||||
'align_multiline_comment' => true,
|
||||
'array_indentation' => true,
|
||||
'array_syntax' => ['syntax' => 'short'],
|
||||
'binary_operator_spaces' => [
|
||||
'operators' => [
|
||||
'=' => 'align_single_space_minimal',
|
||||
'=>' => 'align',
|
||||
],
|
||||
],
|
||||
'blank_line_after_namespace' => true,
|
||||
'blank_line_after_opening_tag' => false,
|
||||
'blank_line_before_statement' => [
|
||||
'statements' => [
|
||||
'break',
|
||||
'continue',
|
||||
'declare',
|
||||
'do',
|
||||
'for',
|
||||
'foreach',
|
||||
'if',
|
||||
'include',
|
||||
'include_once',
|
||||
'require',
|
||||
'require_once',
|
||||
'return',
|
||||
'switch',
|
||||
'throw',
|
||||
'try',
|
||||
'while',
|
||||
'yield',
|
||||
],
|
||||
],
|
||||
'braces' => [
|
||||
'allow_single_line_closure' => false,
|
||||
'position_after_anonymous_constructs' => 'same',
|
||||
'position_after_control_structures' => 'same',
|
||||
'position_after_functions_and_oop_constructs' => 'same'
|
||||
],
|
||||
'cast_spaces' => ['space' => 'none'],
|
||||
|
||||
// This fixer removes the blank line at class start, no way to disable that, so we disable the fixer :(
|
||||
//'class_attributes_separation' => ['elements' => ['const', 'method', 'property']],
|
||||
|
||||
'combine_consecutive_issets' => true,
|
||||
'combine_consecutive_unsets' => true,
|
||||
'compact_nullable_typehint' => true,
|
||||
'concat_space' => ['spacing' => 'one'],
|
||||
'date_time_immutable' => true,
|
||||
'declare_equal_normalize' => ['space' => 'single'],
|
||||
'declare_strict_types' => true,
|
||||
'dir_constant' => true,
|
||||
'elseif' => true,
|
||||
'encoding' => true,
|
||||
'full_opening_tag' => true,
|
||||
'fully_qualified_strict_types' => true,
|
||||
'function_declaration' => [
|
||||
'closure_function_spacing' => 'one'
|
||||
],
|
||||
'header_comment' => false,
|
||||
'indentation_type' => true,
|
||||
'is_null' => true,
|
||||
'line_ending' => true,
|
||||
'list_syntax' => ['syntax' => 'short'],
|
||||
'logical_operators' => true,
|
||||
'lowercase_cast' => true,
|
||||
'lowercase_constants' => true,
|
||||
'lowercase_keywords' => true,
|
||||
'lowercase_static_reference' => true,
|
||||
'magic_constant_casing' => true,
|
||||
'method_argument_space' => ['ensure_fully_multiline' => true],
|
||||
'modernize_types_casting' => true,
|
||||
'multiline_comment_opening_closing' => true,
|
||||
'multiline_whitespace_before_semicolons' => true,
|
||||
'native_constant_invocation' => true,
|
||||
'native_function_casing' => true,
|
||||
'native_function_invocation' => true,
|
||||
'new_with_braces' => false,
|
||||
'no_alias_functions' => true,
|
||||
'no_alternative_syntax' => true,
|
||||
'no_blank_lines_after_class_opening' => false,
|
||||
'no_blank_lines_after_phpdoc' => true,
|
||||
'no_blank_lines_before_namespace' => true,
|
||||
'no_closing_tag' => true,
|
||||
'no_empty_comment' => true,
|
||||
'no_empty_phpdoc' => true,
|
||||
'no_empty_statement' => true,
|
||||
'no_extra_blank_lines' => true,
|
||||
'no_homoglyph_names' => true,
|
||||
'no_leading_import_slash' => true,
|
||||
'no_leading_namespace_whitespace' => true,
|
||||
'no_mixed_echo_print' => ['use' => 'print'],
|
||||
'no_multiline_whitespace_around_double_arrow' => true,
|
||||
'no_null_property_initialization' => true,
|
||||
'no_php4_constructor' => true,
|
||||
'no_short_bool_cast' => true,
|
||||
'no_short_echo_tag' => true,
|
||||
'no_singleline_whitespace_before_semicolons' => true,
|
||||
'no_spaces_after_function_name' => true,
|
||||
'no_spaces_inside_parenthesis' => true,
|
||||
'no_superfluous_elseif' => true,
|
||||
'no_superfluous_phpdoc_tags' => true,
|
||||
'no_trailing_comma_in_list_call' => true,
|
||||
'no_trailing_comma_in_singleline_array' => true,
|
||||
'no_trailing_whitespace' => true,
|
||||
'no_trailing_whitespace_in_comment' => true,
|
||||
'no_unneeded_control_parentheses' => false,
|
||||
'no_unneeded_curly_braces' => false,
|
||||
'no_unneeded_final_method' => true,
|
||||
'no_unreachable_default_argument_value' => true,
|
||||
'no_unset_on_property' => true,
|
||||
'no_unused_imports' => true,
|
||||
'no_useless_else' => true,
|
||||
'no_useless_return' => true,
|
||||
'no_whitespace_before_comma_in_array' => true,
|
||||
'no_whitespace_in_blank_line' => true,
|
||||
'non_printable_character' => true,
|
||||
'normalize_index_brace' => true,
|
||||
'object_operator_without_whitespace' => true,
|
||||
'ordered_class_elements' => [
|
||||
'order' => [
|
||||
'use_trait',
|
||||
'constant_public',
|
||||
'constant_protected',
|
||||
'constant_private',
|
||||
'property_public_static',
|
||||
'property_protected_static',
|
||||
'property_private_static',
|
||||
'property_public',
|
||||
'property_protected',
|
||||
'property_private',
|
||||
'method_public_static',
|
||||
'construct',
|
||||
'destruct',
|
||||
'magic',
|
||||
'phpunit',
|
||||
'method_public',
|
||||
'method_protected',
|
||||
'method_private',
|
||||
'method_protected_static',
|
||||
'method_private_static',
|
||||
],
|
||||
],
|
||||
'ordered_imports' => true,
|
||||
'phpdoc_add_missing_param_annotation' => true,
|
||||
'phpdoc_align' => true,
|
||||
'phpdoc_annotation_without_dot' => true,
|
||||
'phpdoc_indent' => true,
|
||||
'phpdoc_no_access' => true,
|
||||
'phpdoc_no_empty_return' => true,
|
||||
'phpdoc_no_package' => true,
|
||||
'phpdoc_order' => true,
|
||||
'phpdoc_return_self_reference' => true,
|
||||
'phpdoc_scalar' => true,
|
||||
'phpdoc_separation' => true,
|
||||
'phpdoc_single_line_var_spacing' => true,
|
||||
'phpdoc_to_comment' => false,
|
||||
'phpdoc_trim' => true,
|
||||
'phpdoc_trim_consecutive_blank_line_separation' => true,
|
||||
'phpdoc_types' => ['groups' => ['simple', 'meta']],
|
||||
'phpdoc_types_order' => true,
|
||||
'phpdoc_to_return_type' => true,
|
||||
'phpdoc_var_without_name' => true,
|
||||
'pow_to_exponentiation' => true,
|
||||
'protected_to_private' => true,
|
||||
'return_assignment' => true,
|
||||
'return_type_declaration' => ['space_before' => 'none'],
|
||||
'self_accessor' => false,
|
||||
'semicolon_after_instruction' => true,
|
||||
'set_type_to_cast' => true,
|
||||
'short_scalar_cast' => true,
|
||||
'simplified_null_return' => true,
|
||||
'single_blank_line_at_eof' => true,
|
||||
'single_import_per_statement' => true,
|
||||
'single_line_after_imports' => true,
|
||||
'single_quote' => true,
|
||||
'standardize_not_equals' => true,
|
||||
'ternary_to_null_coalescing' => true,
|
||||
'trailing_comma_in_multiline_array' => false,
|
||||
'trim_array_spaces' => true,
|
||||
'unary_operator_spaces' => true,
|
||||
'visibility_required' => [
|
||||
'elements' => [
|
||||
'const',
|
||||
'method',
|
||||
'property',
|
||||
],
|
||||
],
|
||||
'void_return' => true,
|
||||
'whitespace_after_comma_in_array' => true,
|
||||
'yoda_style' => false
|
||||
]
|
||||
)
|
||||
->setFinder(
|
||||
PhpCsFixer\Finder::create()
|
||||
->files()
|
||||
->in(__DIR__ . '/src')
|
||||
->in(__DIR__ . '/tests')
|
||||
->notName('*.phpt')
|
||||
->notName('autoload.php')
|
||||
);
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue