parent
29e611487e
commit
484a87554d
@ -1,17 +0,0 @@
|
|||||||
# Activer la réécriture d'URL
|
|
||||||
RewriteEngine On
|
|
||||||
|
|
||||||
RewriteCond %{REQUEST_FILENAME} !-f
|
|
||||||
RewriteCond %{REQUEST_FILENAME} !-d
|
|
||||||
|
|
||||||
RewriteRule ^ index.php [QSA,L]
|
|
||||||
|
|
||||||
<Files *>
|
|
||||||
Order Allow,Deny
|
|
||||||
Deny from all
|
|
||||||
</Files>
|
|
||||||
|
|
||||||
<Files .htaccess>
|
|
||||||
Order Allow,Deny
|
|
||||||
Deny from all
|
|
||||||
</Files>
|
|
@ -1,21 +1,28 @@
|
|||||||
server {
|
server {
|
||||||
listen 80;
|
listen 80;
|
||||||
server_name localhost;
|
index index.php;
|
||||||
|
root /var/www/public;
|
||||||
|
error_page 404 /index.php;
|
||||||
|
|
||||||
root /var/www/html;
|
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
|
||||||
index index.php index.html;
|
root /var/www/public;
|
||||||
|
|
||||||
location / {
|
|
||||||
try_files $uri $uri/ /index.php?$query_string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
location ~ \.php$ {
|
location ~ \.php$ {
|
||||||
include fastcgi_params;
|
fastcgi_pass web:9000; # service name defined in docker-compose.yml file like web
|
||||||
fastcgi_pass php:9000; # service name defined in docker-compose.yml file
|
fastcgi_param REQUEST_METHOD $request_method;
|
||||||
|
fastcgi_index index.php;
|
||||||
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
|
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
|
||||||
|
include fastcgi_params;
|
||||||
|
}
|
||||||
|
location ~ /\. {
|
||||||
|
deny all;
|
||||||
|
access_log off;
|
||||||
|
log_not_found off;
|
||||||
}
|
}
|
||||||
|
|
||||||
location ~ /\.ht {
|
location / {
|
||||||
deny all;
|
try_files $uri $uri/ /index.php?$query_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,15 +1,42 @@
|
|||||||
version: '3'
|
version: '3'
|
||||||
services:
|
services:
|
||||||
web:
|
|
||||||
|
nginx:
|
||||||
image: nginx:latest
|
image: nginx:latest
|
||||||
ports:
|
ports:
|
||||||
- "8080:80"
|
- "3000:80"
|
||||||
volumes:
|
volumes:
|
||||||
- ./public:/var/www/html
|
- ./config/nginx.conf:/etc/nginx/conf.d/default.conf
|
||||||
- ./config/nginx.conf:/etc/nginx/conf.d
|
- .:/var/www
|
||||||
links:
|
depends_on:
|
||||||
- php
|
- mysql
|
||||||
php:
|
- web
|
||||||
image: php:7.4-fpm
|
|
||||||
|
web:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: ./config/Dockerfile
|
||||||
|
ports:
|
||||||
|
- 9000:9000
|
||||||
volumes:
|
volumes:
|
||||||
- ./src:/var/www/html
|
- .:/var/www
|
||||||
|
depends_on:
|
||||||
|
- mysql
|
||||||
|
environment:
|
||||||
|
DB_HOST: mysql
|
||||||
|
DB_PORT: port
|
||||||
|
DB_DATABASE: test
|
||||||
|
DB_USER: user
|
||||||
|
DB_PASSWORD: pass
|
||||||
|
APP_ENV: development
|
||||||
|
|
||||||
|
mysql:
|
||||||
|
image: mysql:latest
|
||||||
|
container_name: my-mysql-container
|
||||||
|
environment:
|
||||||
|
MYSQL_ROOT_PASSWORD: pass
|
||||||
|
MYSQL_DATABASE: test
|
||||||
|
MYSQL_USER: user
|
||||||
|
MYSQL_PASSWORD: pass
|
||||||
|
ports:
|
||||||
|
- "3307:3306"
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,9 +1,36 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once __DIR__ . '/../vendor/autoload.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
require_once __DIR__ . '/../config/config.php';
|
require_once __DIR__ . '/../config/config.php';
|
||||||
if (APP_ENV === 'console') {
|
|
||||||
require_once __DIR__ . '/../src/console/Console.php';
|
use App\AppCreator;
|
||||||
}
|
use App\Router\Middleware\LoggingMiddleware;
|
||||||
elseif (APP_ENV === 'development') {
|
use App\Router\Request\RequestFactory;
|
||||||
require_once __DIR__ . DIRECTORY_SEPARATOR . '../src/app/index.test.php';
|
use Shared\ArgumentControllerResolver;
|
||||||
|
use Shared\IArgumentResolver;
|
||||||
|
use Twig\Environment;
|
||||||
|
use Twig\Loader\FilesystemLoader;
|
||||||
|
use Shared\Log;
|
||||||
|
|
||||||
|
$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,15 @@
|
|||||||
|
document.getElementById('saveButton').addEventListener('click', function() {
|
||||||
|
var preferences = {
|
||||||
|
notifications: document.getElementById('notif').checked,
|
||||||
|
theme: document.getElementById('theme').value
|
||||||
|
};
|
||||||
|
|
||||||
|
fetch('/index.php/', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(preferences),
|
||||||
|
})
|
||||||
|
.catch((error) => console.error('Error:', error));
|
||||||
|
});
|
@ -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,177 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// namespace App\Controller;
|
||||||
|
|
||||||
|
// use App\Container;
|
||||||
|
// use App\Router\Request\IRequest;
|
||||||
|
// use App\Router\Response\Response;
|
||||||
|
// use Shared\Attributes\Route;
|
||||||
|
// use Twig\Environment;
|
||||||
|
// use Data\Core\Preferences;
|
||||||
|
// use Shared\Log;
|
||||||
|
|
||||||
|
// class AthleteController extends BaseController
|
||||||
|
// {
|
||||||
|
|
||||||
|
// #[Route(path: '/search-user', name: 'search-user', methods: ['GET'])]
|
||||||
|
// public function searchUser(string $username, IRequest $req): Response
|
||||||
|
// {
|
||||||
|
// $taberror = [];
|
||||||
|
// $utiliArray = [
|
||||||
|
// [
|
||||||
|
// 'nom' => 'John',
|
||||||
|
// 'prenom' => 'Doe',
|
||||||
|
// 'img' => 'john_doe',
|
||||||
|
// 'username' => 'johndoe',
|
||||||
|
// ],
|
||||||
|
// [
|
||||||
|
// 'nom' => 'Alice',
|
||||||
|
// 'prenom' => 'Smith',
|
||||||
|
// 'img' => 'alice_smith',
|
||||||
|
// 'username' => 'alicesmith',
|
||||||
|
// ],
|
||||||
|
// ];
|
||||||
|
// // if(!Validation::val_string($name)){
|
||||||
|
// try {
|
||||||
|
// //code...
|
||||||
|
// // $model->userMgr->getUser($name);
|
||||||
|
// return $this->render('./page/addfriend.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => $utiliArray,
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => [],
|
||||||
|
// 'responce' => "Notification d'ajout envoyée à $username"
|
||||||
|
// ]);
|
||||||
|
// } catch (\Throwable $th) {
|
||||||
|
// //throw $th;
|
||||||
|
// // return $this->render("addfriend.html.twig", ['tabError' => $taberror ]);
|
||||||
|
// }
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[Route(path: '/analyses', name: 'analyses', methods: ['GET'])]
|
||||||
|
// public function analyses(): Response
|
||||||
|
// {
|
||||||
|
// return $this->render('./page/analyze.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// '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',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => [],
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => []
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[Route(path: '/add-friend', name: 'add-friend', methods: ['POST'])]
|
||||||
|
// public function addFriend(string $username, IRequest $req): Response
|
||||||
|
// {
|
||||||
|
// $taberror = [];
|
||||||
|
// $utiliArray = [
|
||||||
|
// [
|
||||||
|
// 'nom' => 'John',
|
||||||
|
// 'prenom' => 'Doe',
|
||||||
|
// 'img' => 'john_doe',
|
||||||
|
// 'username' => 'johndoe',
|
||||||
|
// ],
|
||||||
|
// [
|
||||||
|
// 'nom' => 'Alice',
|
||||||
|
// 'prenom' => 'Smith',
|
||||||
|
// 'img' => 'alice_smith',
|
||||||
|
// 'username' => 'alicesmith',
|
||||||
|
// ],
|
||||||
|
// ];
|
||||||
|
// // if(!Validation::val_string($name)){
|
||||||
|
// try {
|
||||||
|
// //code...
|
||||||
|
// // $model->userMgr->addFriend($name);
|
||||||
|
// return $this->render('./page/addfriend.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => $utiliArray,
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => [],
|
||||||
|
// 'responce' => "Notification d'ajout envoyée à $username"
|
||||||
|
// ]);
|
||||||
|
// } catch (\Throwable $th) {
|
||||||
|
// //throw $th;
|
||||||
|
// // return $this->render("addfriend.html.twig", ['tabError' => $taberror ]);
|
||||||
|
// }
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[Route(path: '/delete-friend', name: 'delete-friend', methods: ['POST'])]
|
||||||
|
|
||||||
|
|
||||||
|
// #[Route(path: '/friend', name: 'friend', methods: ['GET'])]
|
||||||
|
// public function friend(): Response
|
||||||
|
// {
|
||||||
|
// $utiliArray = [
|
||||||
|
// [
|
||||||
|
// 'nom' => 'John',
|
||||||
|
// 'prenom' => 'Doe',
|
||||||
|
// 'img' => 'john_doe',
|
||||||
|
// 'username' => 'johndoe',
|
||||||
|
// ],
|
||||||
|
// [
|
||||||
|
// 'nom' => 'Alice',
|
||||||
|
// 'prenom' => 'Smith',
|
||||||
|
// 'img' => 'alice_smith',
|
||||||
|
// 'username' => 'alicesmith',
|
||||||
|
// ],
|
||||||
|
// ];
|
||||||
|
// // $this->Auth->getUser->role->getFriends
|
||||||
|
// return $this->render('./page/addfriend.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => $utiliArray,
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => [],
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// namespace App\Controller;
|
||||||
|
|
||||||
|
// use App\Container;
|
||||||
|
// use App\Router\Request\IRequest;
|
||||||
|
// use App\Router\Response\Response;
|
||||||
|
// use Shared\Attributes\Route;
|
||||||
|
// use Twig\Environment;
|
||||||
|
// use Data\Core\Preferences;
|
||||||
|
// use Shared\Log;
|
||||||
|
|
||||||
|
// class AuthController extends BaseController
|
||||||
|
// {
|
||||||
|
// #[Route('/login', name: 'login')]
|
||||||
|
// public function login(?string $username, ?string $password ,IRequest $request): Response {
|
||||||
|
// // if user is already logged in, don't display the login page again
|
||||||
|
// if ($user) {
|
||||||
|
// return $this->redirectToRoute('blog_index');
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // this statement solves an edge-case: if you change the locale in the login
|
||||||
|
// // page, after a successful login you are redirected to a page in the previous
|
||||||
|
// // locale. This code regenerates the referrer URL whenever the login page is
|
||||||
|
// // browsed, to ensure that its locale is always the current one.
|
||||||
|
// $this->saveTargetPath($request->getSession(), 'main', $this->generateUrl('admin_index'));
|
||||||
|
|
||||||
|
// return $this->render('security/login.html.twig', [
|
||||||
|
// // last username entered by the user (if any)
|
||||||
|
// 'last_username' => $helper->getLastUsername(),
|
||||||
|
// // last authentication error (if any)
|
||||||
|
// 'error' => $helper->getLastAuthenticationError(),
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[Route('/login', name: 'login')]
|
||||||
|
// public function login(?string $username, ?string $password ,IRequest $request): Response {
|
||||||
|
// // if user is already logged in, don't display the login page again
|
||||||
|
// if ($user) {
|
||||||
|
// return $this->redirectToRoute('blog_index');
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // this statement solves an edge-case: if you change the locale in the login
|
||||||
|
// // page, after a successful login you are redirected to a page in the previous
|
||||||
|
// // locale. This code regenerates the referrer URL whenever the login page is
|
||||||
|
// // browsed, to ensure that its locale is always the current one.
|
||||||
|
// $this->saveTargetPath($request->getSession(), 'main', $this->generateUrl('admin_index'));
|
||||||
|
|
||||||
|
// return $this->render('security/login.html.twig', [
|
||||||
|
// // last username entered by the user (if any)
|
||||||
|
// 'last_username' => $helper->getLastUsername(),
|
||||||
|
// // last authentication error (if any)
|
||||||
|
// 'error' => $helper->getLastAuthenticationError(),
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// function inscription() {
|
||||||
|
// $model = new ModelVisitor();
|
||||||
|
// $log=Validation::clean_string($_POST['pseudo']);
|
||||||
|
// $mdp=Validation::clean_string($_POST['password']);
|
||||||
|
// if($model->createAUser($log,$mdp)){
|
||||||
|
// if(ModelUser::login($log, $mdp)){
|
||||||
|
// UserControler::displayView();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// function login() {
|
||||||
|
// $model = new ModelVisitor();
|
||||||
|
// if(!isset($_POST['pseudo']) || !isset($_POST['password'])) throw new Exception(" some wrong with credentials !!!!!");
|
||||||
|
// $log=Validation::clean_string($_POST['pseudo']);
|
||||||
|
// $mdp=Validation::clean_string($_POST['password']);
|
||||||
|
// if(ModelUser::login($log, $mdp)){
|
||||||
|
// UserControler::displayView();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
?>
|
@ -0,0 +1,72 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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,128 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// namespace App\Controller;
|
||||||
|
|
||||||
|
// use App\Container;
|
||||||
|
// use App\Router\Request\IRequest;
|
||||||
|
// use App\Router\Response\Response;
|
||||||
|
// use App\Router\Response\IResponse;
|
||||||
|
|
||||||
|
// use Shared\Attributes\Route;
|
||||||
|
// use Twig\Environment;
|
||||||
|
// use Data\Core\Preferences;
|
||||||
|
// use Shared\Log;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// #[Route(path: '/coach', name: 'coach')]
|
||||||
|
// class CoachController extends BaseController
|
||||||
|
// {
|
||||||
|
|
||||||
|
// private ICoachManager $coachManager;
|
||||||
|
// private $security;
|
||||||
|
|
||||||
|
// public function __construct(DataManager $dataManager, Security $security)
|
||||||
|
// {
|
||||||
|
// $this->coachManager = $dataManager->coachMgr;
|
||||||
|
// $this->security = $security;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[Route(path: '/', name: 'home', methods: ['GET'])]
|
||||||
|
// public function index(): Response
|
||||||
|
// {
|
||||||
|
// return $this->render('./page/home.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => [],
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => []
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[Route('/global-stats', name: 'coach_global_stats', methods: ['GET'])]
|
||||||
|
// public function globalStats(): Response
|
||||||
|
// {
|
||||||
|
// // Add logic to fetch and process global stats
|
||||||
|
// return $this->render('coach/global_stats.html.twig');
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[Route(path: '/exercice', name: 'exercice', methods: ['GET'])] // 8
|
||||||
|
// public function exercice(): Response
|
||||||
|
// {
|
||||||
|
// return $this->render('./page/exercice.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => [],
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => []
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// #[Route(path: '/coaching', name: 'coaching', methods: ['GET'])]
|
||||||
|
// public function coaching(): Response
|
||||||
|
// {
|
||||||
|
// return $this->render('./page/coaching.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => [],
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => []
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[Route('/list-athletes', name: 'coach_list_athletes')]
|
||||||
|
// public function listAthletes(): Response
|
||||||
|
// {
|
||||||
|
// $coach = $this->security->getUser();
|
||||||
|
// $athletes = $this->coachManager->getAthletesForCoach($coach);
|
||||||
|
|
||||||
|
// return $this->render('coach/list_athletes.html.twig', [
|
||||||
|
// 'athletes' => $athletes,
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
// #[Route('/athlete-analysis/{athleteId}', name: 'coach_athlete_analysis', methods: ['GET'])]
|
||||||
|
// public function athleteAnalysis($athleteId): Response
|
||||||
|
// {
|
||||||
|
// // Fetch and process data specific to the athlete
|
||||||
|
// return $this->render('coach/athlete_analysis.html.twig', [
|
||||||
|
// 'athleteId' => $athleteId,
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[Route('/add-athlete/{athleteId}', name: 'coach_add_athlete', methods: ['POST'])]
|
||||||
|
// public function addAthlete(IRequest $request, $athleteId): IResponse
|
||||||
|
// {
|
||||||
|
// // Implement logic to add athlete
|
||||||
|
// // ...
|
||||||
|
|
||||||
|
// return $this->redirectToRoute('coach_list_athletes');
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // #[Route('/remove-athlete', name: 'coach_remove_athlete', methods: ['POST'])]
|
||||||
|
// // public function removeAthlete(int $athleteId, IRequest $request): IResponse
|
||||||
|
// // {
|
||||||
|
|
||||||
|
// // return $this->redirectToRoute("/athletes");
|
||||||
|
// // }
|
||||||
|
|
||||||
|
|
||||||
|
// }
|
@ -0,0 +1,718 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Container;
|
||||||
|
use App\Router\Request\IRequest;
|
||||||
|
use App\Router\Response\Response;
|
||||||
|
use Shared\Attributes\Route;
|
||||||
|
use Twig\Environment;
|
||||||
|
use Data\Core\Preferences;
|
||||||
|
use Shared\Log;
|
||||||
|
|
||||||
|
// TODO : Remove this BaseClass
|
||||||
|
class Controller extends BaseController
|
||||||
|
{
|
||||||
|
private Environment $twig;
|
||||||
|
private Preferences $preference;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
session_start();
|
||||||
|
$this->preference = new Preferences();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/', name: 'home', methods: ['GET'])]
|
||||||
|
public function index(): Response
|
||||||
|
{
|
||||||
|
return $this->render('./page/index.html',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/analyses', name: 'analyses', methods: ['GET'])]
|
||||||
|
public function analyses(): Response
|
||||||
|
{
|
||||||
|
return $this->render('./page/analyze.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/activity', name: 'activity', methods: ['GET'])]
|
||||||
|
public function activity(): Response
|
||||||
|
{
|
||||||
|
return $this->render('./page/activity.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'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',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/exercices', name: 'exercices', methods: ['POST'])] // 8
|
||||||
|
public function exercices(String $type, String $intensite, String $date, IRequest $req): Response
|
||||||
|
{
|
||||||
|
$exercicesArray = [
|
||||||
|
[
|
||||||
|
'date' => $date,
|
||||||
|
'type' => $type,
|
||||||
|
'intensite' => $intensite,
|
||||||
|
'status' => 'A venur',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
return $this->render('./page/exercice.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => $exercicesArray,
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/search-user', name: 'search-user', methods: ['GET'])]
|
||||||
|
public function searchUser(string $username, IRequest $req): Response
|
||||||
|
{
|
||||||
|
$taberror = [];
|
||||||
|
// FILTER
|
||||||
|
$utiliArray = [
|
||||||
|
[
|
||||||
|
'nom' => 'John',
|
||||||
|
'prenom' => 'Doe',
|
||||||
|
'img' => 'john_doe',
|
||||||
|
'username' => 'johndoe',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'nom' => 'Alice',
|
||||||
|
'prenom' => 'Smith',
|
||||||
|
'img' => 'alice_smith',
|
||||||
|
'username' => 'alicesmith',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
// if(!Validation::val_string($name)){
|
||||||
|
try {
|
||||||
|
//code...
|
||||||
|
// $model->userMgr->addFriend($name);
|
||||||
|
return $this->render('./page/addfriend.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => $utiliArray,
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => [],
|
||||||
|
'responce' => "Notification d'ajout envoyée à $username"
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
//throw $th;
|
||||||
|
// return $this->render("addfriend.html.twig", ['tabError' => $taberror ]);
|
||||||
|
}
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/search-member', name: 'search-member', methods: ['GET'])]
|
||||||
|
public function searchMember(string $username, IRequest $req): Response
|
||||||
|
{
|
||||||
|
$taberror = [];
|
||||||
|
// FILTER
|
||||||
|
$utiliArray = [
|
||||||
|
[
|
||||||
|
'nom' => 'John',
|
||||||
|
'prenom' => 'Doe',
|
||||||
|
'img' => 'john_doe',
|
||||||
|
'username' => 'johndoe',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'nom' => 'Alice',
|
||||||
|
'prenom' => 'Smith',
|
||||||
|
'img' => 'alice_smith',
|
||||||
|
'username' => 'alicesmith',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
// if(!Validation::val_string($name)){
|
||||||
|
try {
|
||||||
|
//code...
|
||||||
|
// $model->userMgr->addFriend($name);
|
||||||
|
return $this->render('./page/addmember.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => $utiliArray,
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => [],
|
||||||
|
'responce' => "Notification d'ajout envoyée à $username"
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
//throw $th;
|
||||||
|
// return $this->render("addfriend.html.twig", ['tabError' => $taberror ]);
|
||||||
|
}
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/add-member', name: 'add-member', methods: ['POST'])]
|
||||||
|
public function addmember(string $username, IRequest $req): Response
|
||||||
|
{
|
||||||
|
$taberror = [];
|
||||||
|
$utiliArray = [
|
||||||
|
[
|
||||||
|
'nom' => 'John',
|
||||||
|
'prenom' => 'Doe',
|
||||||
|
'img' => 'john_doe',
|
||||||
|
'username' => 'johndoe',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'nom' => 'Alice',
|
||||||
|
'prenom' => 'Smith',
|
||||||
|
'img' => 'alice_smith',
|
||||||
|
'username' => 'alicesmith',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
// if(!Validation::val_string($name)){
|
||||||
|
try {
|
||||||
|
//code...
|
||||||
|
// $model->userMgr->addFriend($name);
|
||||||
|
return $this->render('./page/addmember.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => $utiliArray,
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => [],
|
||||||
|
'responce' => "Notification d'ajout envoyée à $username"
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
//throw $th;
|
||||||
|
// return $this->render("addfriend.html.twig", ['tabError' => $taberror ]);
|
||||||
|
}
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/member', name: 'member', methods: ['GET'])]
|
||||||
|
public function member(): Response
|
||||||
|
{
|
||||||
|
$utiliArray = [
|
||||||
|
[
|
||||||
|
'nom' => 'John',
|
||||||
|
'prenom' => 'Doe',
|
||||||
|
'img' => 'john_doe',
|
||||||
|
'username' => 'johndoe',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'nom' => 'Alice',
|
||||||
|
'prenom' => 'Smith',
|
||||||
|
'img' => 'alice_smith',
|
||||||
|
'username' => 'alicesmith',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
return $this->render('./page/addmember.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => $utiliArray,
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => [],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/add-friend', name: 'add-friend', methods: ['POST'])]
|
||||||
|
public function addFriend(string $username, IRequest $req): Response
|
||||||
|
{
|
||||||
|
$taberror = [];
|
||||||
|
$utiliArray = [
|
||||||
|
[
|
||||||
|
'nom' => 'John',
|
||||||
|
'prenom' => 'Doe',
|
||||||
|
'img' => 'john_doe',
|
||||||
|
'username' => 'johndoe',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'nom' => 'Alice',
|
||||||
|
'prenom' => 'Smith',
|
||||||
|
'img' => 'alice_smith',
|
||||||
|
'username' => 'alicesmith',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
// if(!Validation::val_string($name)){
|
||||||
|
try {
|
||||||
|
//code...
|
||||||
|
// $model->userMgr->addFriend($name);
|
||||||
|
return $this->render('./page/addfriend.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => $utiliArray,
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => [],
|
||||||
|
'responce' => "Notification d'ajout envoyée à $username"
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
//throw $th;
|
||||||
|
// return $this->render("addfriend.html.twig", ['tabError' => $taberror ]);
|
||||||
|
}
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/friend', name: 'friend', methods: ['GET'])]
|
||||||
|
public function friend(): Response
|
||||||
|
{
|
||||||
|
$utiliArray = [
|
||||||
|
[
|
||||||
|
'nom' => 'John',
|
||||||
|
'prenom' => 'Doe',
|
||||||
|
'img' => 'john_doe',
|
||||||
|
'username' => 'johndoe',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'nom' => 'Alice',
|
||||||
|
'prenom' => 'Smith',
|
||||||
|
'img' => 'alice_smith',
|
||||||
|
'username' => 'alicesmith',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
return $this->render('./page/addfriend.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => $utiliArray,
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => [],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/friendlist', name: 'friendlist', methods: ['POST'])]
|
||||||
|
public function friendlist(string $username, IRequest $req): Response
|
||||||
|
{
|
||||||
|
$utiliArray = [
|
||||||
|
[
|
||||||
|
'nom' => 'John',
|
||||||
|
'prenom' => 'Doe',
|
||||||
|
'img' => 'john_doe',
|
||||||
|
'username' => 'johndoe',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'nom' => 'Alice',
|
||||||
|
'prenom' => 'Smith',
|
||||||
|
'img' => 'alice_smith',
|
||||||
|
'username' => 'alicesmith',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
/* TODO */
|
||||||
|
|
||||||
|
// -> Enlever ou bloquer un utilisateur en fonction de son username
|
||||||
|
|
||||||
|
return $this->render('./page/friend.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => $utiliArray,
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => [],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/friendlist', name: 'friendlist2', methods: ['GET'])]
|
||||||
|
public function friendlist2(): Response
|
||||||
|
{
|
||||||
|
$utiliArray = [
|
||||||
|
[
|
||||||
|
'nom' => 'John',
|
||||||
|
'prenom' => 'Doe',
|
||||||
|
'img' => 'test',
|
||||||
|
'status' => 'johndoe',
|
||||||
|
'username' => 'jdoe',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'nom' => 'Alice',
|
||||||
|
'prenom' => 'Smith',
|
||||||
|
'img' => 'test2',
|
||||||
|
'status' => 'alicesmith',
|
||||||
|
'username' => 'asmith',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
return $this->render('./page/friend.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => $utiliArray,
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => [],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/coaching', name: 'coaching', methods: ['GET'])]
|
||||||
|
public function coaching(): Response
|
||||||
|
{
|
||||||
|
return $this->render('./page/coaching.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/mail', name: 'mail', methods: ['GET'])]
|
||||||
|
public function mail(): Response
|
||||||
|
{
|
||||||
|
return $this->render('./page/mail.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/import', name: 'import', methods: ['GET'])]
|
||||||
|
public function import(): Response
|
||||||
|
{
|
||||||
|
return $this->render('./page/import.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/settings', name: 'settings', methods: ['GET'])]
|
||||||
|
public function settings(IRequest $req): Response
|
||||||
|
{
|
||||||
|
return $this->render('./page/settings.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/profile', name: 'profile', methods: ['GET'])]
|
||||||
|
public function profile(): Response
|
||||||
|
{
|
||||||
|
return $this->render('./page/profile.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/preferences', name: 'preferences', methods: ['POST'])]
|
||||||
|
public function preferences(string $theme, IRequest $req): Response
|
||||||
|
{
|
||||||
|
/*TODO*/
|
||||||
|
|
||||||
|
// VALIDER LES DONNEES
|
||||||
|
$this->preference->majCookie($theme);
|
||||||
|
|
||||||
|
return $this->render('./page/settings.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/psettings', name: 'psettings', methods: ['POST'])]
|
||||||
|
public function psettings(string $nom,string $prenom,string $dateNaissance,string $mail,string $tel, IRequest $req): Response
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
return $this->render('./page/settings.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => $prenom,
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/mdp', name: 'mdp', methods: ['POST'])]
|
||||||
|
public function mdp(string $ancienMotDePasse,string $nouveauMotDePasse,string $confirmerMotDePasse, IRequest $req): Response
|
||||||
|
{
|
||||||
|
|
||||||
|
// CONFIRMER LES DONNESS !!!!! IMPORTANT
|
||||||
|
|
||||||
|
return $this->render('./page/settings.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/login', name: 'login', methods: ['POST'])]
|
||||||
|
public function login(string $username,string $mdp, IRequest $req): Response
|
||||||
|
{
|
||||||
|
|
||||||
|
// CONFIRMER LES DONNESS !!!!! IMPORTANT
|
||||||
|
|
||||||
|
return $this->render('./page/home.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/log', name: 'log', methods: ['GET'])]
|
||||||
|
public function login2(): Response
|
||||||
|
{
|
||||||
|
|
||||||
|
// CONFIRMER LES DONNESS !!!!! IMPORTANT
|
||||||
|
|
||||||
|
return $this->render('./page/login.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/register', name: 'register', methods: ['POST'])]
|
||||||
|
public function register(string $username,string $mdp,string $confirmMdp,string $nom,string $prenom,string $dateNaissance,string $sexe,string $taille,string $poids, IRequest $req): Response
|
||||||
|
{
|
||||||
|
|
||||||
|
// CONFIRMER LES DONNESS !!!!! IMPORTANT
|
||||||
|
|
||||||
|
return $this->render('./page/home.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/regist', name: 'regist', methods: ['GET'])]
|
||||||
|
public function register2(): Response
|
||||||
|
{
|
||||||
|
|
||||||
|
// CONFIRMER LES DONNESS !!!!! IMPORTANT
|
||||||
|
|
||||||
|
return $this->render('./page/register.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/pass', name: 'pass', methods: ['GET'])]
|
||||||
|
public function pass(): Response
|
||||||
|
{
|
||||||
|
|
||||||
|
// CONFIRMER LES DONNESS !!!!! IMPORTANT
|
||||||
|
|
||||||
|
return $this->render('./page/password.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Route(path: '/password', name: 'password', methods: ['POST'])]
|
||||||
|
public function password(string $email, IRequest $req): Response
|
||||||
|
{
|
||||||
|
|
||||||
|
// CONFIRMER LES DONNESS !!!!! IMPORTANT
|
||||||
|
|
||||||
|
return $this->render('./page/login.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,74 @@
|
|||||||
|
<?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('./error/error.html.twig',['title'=> $message , "code" => $statusCode, "name" => $message, "descr" => $message ]),$statusCode);
|
||||||
|
$response->send();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// namespace App\Controller;
|
||||||
|
|
||||||
|
// use App\Container;
|
||||||
|
// use App\Router\Request\IRequest;
|
||||||
|
// use App\Router\Response\Response;
|
||||||
|
// use Shared\Attributes\Route;
|
||||||
|
// use Twig\Environment;
|
||||||
|
// use Data\Core\Preferences;
|
||||||
|
// use Shared\Log;
|
||||||
|
|
||||||
|
// class HeartRateController extends BaseController
|
||||||
|
// {
|
||||||
|
|
||||||
|
|
||||||
|
// #[Route(path: '/import', name: 'import', methods: ['GET'])]
|
||||||
|
// public function import(): Response
|
||||||
|
// {
|
||||||
|
// return $this->render('./page/import.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => [],
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => []
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// }
|
@ -0,0 +1,17 @@
|
|||||||
|
<!-- #[Route(path: '/mail', name: 'mail', methods: ['GET'])]
|
||||||
|
public function mail(): Response
|
||||||
|
{
|
||||||
|
return $this->render('./page/mail.html.twig',[
|
||||||
|
'css' => $this->preference->getCookie(),
|
||||||
|
'pp' => "test2",
|
||||||
|
'user' => "Doe",
|
||||||
|
'role' => "Athlète",
|
||||||
|
'friendship' => [],
|
||||||
|
'analyzes' => [],
|
||||||
|
'mails' => [],
|
||||||
|
'users' => [],
|
||||||
|
'infoUser' => [],
|
||||||
|
'exos' => [],
|
||||||
|
'member' => []
|
||||||
|
]);
|
||||||
|
} -->
|
@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// namespace App\Controller;
|
||||||
|
|
||||||
|
// use App\Container;
|
||||||
|
// use App\Router\Request\IRequest;
|
||||||
|
// use App\Router\Response\Response;
|
||||||
|
// use Shared\Attributes\Route;
|
||||||
|
// use Twig\Environment;
|
||||||
|
// use Data\Core\Preferences;
|
||||||
|
// use Shared\Log;
|
||||||
|
|
||||||
|
// class UserController extends BaseController
|
||||||
|
// {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// #[Route(path: '/', name: 'home', methods: ['GET'])]
|
||||||
|
// public function index(): Response
|
||||||
|
// {
|
||||||
|
// return $this->render('./page/home.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => [],
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => []
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// #[Route(path: '/settings', name: 'settings', methods: ['GET'])]
|
||||||
|
// public function settings(IRequest $req): Response
|
||||||
|
// {
|
||||||
|
// return $this->render('./page/settings.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => [],
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => []
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// #[Route(path: '/preferences', name: 'preferences', methods: ['POST'])]
|
||||||
|
// public function preferences(string $theme, IRequest $req): Response
|
||||||
|
// {
|
||||||
|
// /*TODO*/
|
||||||
|
|
||||||
|
// // VALIDER LES DONNEES
|
||||||
|
// $this->preference->majCookie($theme);
|
||||||
|
|
||||||
|
// return $this->render('./page/settings.html.twig',[
|
||||||
|
// 'css' => $this->preference->getCookie(),
|
||||||
|
// 'pp' => "test2",
|
||||||
|
// 'user' => "Doe",
|
||||||
|
// 'role' => "Athlète",
|
||||||
|
// 'friendship' => [],
|
||||||
|
// 'analyzes' => [],
|
||||||
|
// 'mails' => [],
|
||||||
|
// 'users' => [],
|
||||||
|
// 'infoUser' => [],
|
||||||
|
// 'exos' => [],
|
||||||
|
// 'member' => []
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
@ -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,71 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
|
||||||
|
<meta name="description" content="" />
|
||||||
|
<meta name="author" content="" />
|
||||||
|
<title>{% block title %}{% endblock %}</title>
|
||||||
|
<link href="/css/{% block css %}base_theme{% endblock %}.css" rel="stylesheet" />
|
||||||
|
<script src="https://use.fontawesome.com/releases/v6.3.0/js/all.js" crossorigin="anonymous"></script>
|
||||||
|
</head>
|
||||||
|
<body class="bg-primary">
|
||||||
|
<div id="layoutAuthentication">
|
||||||
|
<div id="layoutAuthentication_content">
|
||||||
|
<main>
|
||||||
|
{% block main %}
|
||||||
|
<div class="container">
|
||||||
|
<div class="row justify-content-center">
|
||||||
|
<div class="col-lg-5">
|
||||||
|
<div class="card shadow-lg border-0 rounded-lg mt-5">
|
||||||
|
<div class="card-header"><h3 class="text-center font-weight-light my-4">Connexion</h3></div>
|
||||||
|
<div class="card-body">
|
||||||
|
<form>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input class="form-control" id="inputEmail" type="email" placeholder="nom@exemple.com" />
|
||||||
|
<label for="inputEmail">Adresse eMail</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input class="form-control" id="inputPassword" type="password" placeholder="Mot de passe" />
|
||||||
|
<label for="inputPassword">Mot de passe</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-check mb-3">
|
||||||
|
<input class="form-check-input" id="inputRememberPassword" type="checkbox" value="" />
|
||||||
|
<label class="form-check-label" for="inputRememberPassword">Mémoriser le mot de passe</label>
|
||||||
|
</div>
|
||||||
|
<div class="d-flex align-items-center justify-content-between mt-4 mb-0">
|
||||||
|
<a class="small" href="password.html">Mot de passe oublié ?</a>
|
||||||
|
<a class="btn btn-primary" href="index.html">Se connecter</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="card-footer text-center py-3">
|
||||||
|
<div class="small"><a href="register.html">Besoin d'un compte ? Inscrivez-vous !</a></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
<div id="layoutAuthentication_footer">
|
||||||
|
<footer class="py-4 bg-light mt-auto">
|
||||||
|
<div class="container-fluid px-4">
|
||||||
|
<div class="d-flex align-items-center justify-content-between small">
|
||||||
|
<div class="text-muted">Copyright © HeartTrack 2023</div>
|
||||||
|
<div>
|
||||||
|
<a href="#">Politique de confidentialité</a>
|
||||||
|
·
|
||||||
|
<a href="#">Termes & Conditions d'utilisations</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
|
||||||
|
<script src="js/scripts.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,14 @@
|
|||||||
|
{% extends "errorbase.html.twig" %}
|
||||||
|
|
||||||
|
{% block title %}{{code}} : {{title}}{% endblock %}
|
||||||
|
|
||||||
|
|
||||||
|
{% block nb %}
|
||||||
|
{% if code == 404 %}
|
||||||
|
<img class="mb-4 img-error" src="assets/img/error-404-monochrome.svg"/>
|
||||||
|
{% else %}
|
||||||
|
<h1 class="display-1">{{code}}</h1>
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block descr %}{{descr}}{% endblock %}
|
@ -0,0 +1,33 @@
|
|||||||
|
{% extends "base.html.twig" %}
|
||||||
|
|
||||||
|
{% block pp %}{{pp}}{% endblock %}
|
||||||
|
|
||||||
|
{% block css %}{{css}}{% endblock %}
|
||||||
|
|
||||||
|
{% block title %}Exercices - HearthTrack{% endblock %}
|
||||||
|
|
||||||
|
{% block user %}{{user}} - {{role}}{% endblock %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<div class="container-fluid px-4">
|
||||||
|
<h1 class="mt-4">Exercices</h1>
|
||||||
|
<ol class="breadcrumb mb-4">
|
||||||
|
<li class="breadcrumb-item"><a href="/">Accueil</a></li>
|
||||||
|
<li class="breadcrumb-item active">Exercices</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="datatable-container">
|
||||||
|
<form method="post" action="/exercices">
|
||||||
|
Type : <input type="text" name="type"/>
|
||||||
|
Intensité : <input type="text" name="intensite"/>
|
||||||
|
Date : <input type="text" name="date"/>
|
||||||
|
<button class="btn btn-primary btn-mrg" id="btnNavbarSearch" type="submit">Ajouter l'exercice</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<h1>{{responce}}</h1>
|
||||||
|
{% endblock %}
|
@ -0,0 +1,64 @@
|
|||||||
|
{% extends "base.html.twig" %}
|
||||||
|
|
||||||
|
{% block pp %}{{pp}}{% endblock %}
|
||||||
|
|
||||||
|
{% block css %}{{css}}{% endblock %}
|
||||||
|
|
||||||
|
{% block title %}Coaching - HearthTrack{% endblock %}
|
||||||
|
|
||||||
|
{% block user %}{{user}} - {{role}}{% endblock %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<div class="container-fluid px-4">
|
||||||
|
<h1 class="mt-4">Coaching</h1>
|
||||||
|
<ol class="breadcrumb mb-4">
|
||||||
|
<li class="breadcrumb-item"><a href="/">Accueil</a></li>
|
||||||
|
<li class="breadcrumb-item active">Coaching</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="datatable-container">
|
||||||
|
<table id="datatablesSimple" class="datatable-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th></th>
|
||||||
|
<th>Nom</th>
|
||||||
|
<th>Prénom</th>
|
||||||
|
<th style="
|
||||||
|
padding-left: 42%;
|
||||||
|
">
|
||||||
|
<form class="d-none d-md-inline-block form-inline ms-auto me-0 me-md-3 my-2 my-md-0" method="get" action="/search-member">
|
||||||
|
<div class="input-group">
|
||||||
|
<input class="form-control" name="username" type="text" placeholder="Rechercher..." aria-label="Rechercher..." aria-describedby="btnNavbarSearch" />
|
||||||
|
<button class="btn btn-primary" id="btnNavbarSearch" type="submit"><i class="fas fa-search"></i></button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for utili in users %}
|
||||||
|
<tr>
|
||||||
|
<td><style> .image-ronde{ clip-path:ellipse(50% 50%); } </style>
|
||||||
|
<img src="/assets/img/person/{{utili.img}}.png" width="35px" height="35px" class="image-ronde"></td>
|
||||||
|
<td>{{utili.nom}}</td>
|
||||||
|
<td>{{utili.prenom}}</td>
|
||||||
|
<td style="
|
||||||
|
padding-left: 45%;">
|
||||||
|
<form method="post" action="/add-member">
|
||||||
|
<input type="hidden" name="username" value="{{ utili.username }}">
|
||||||
|
<button class="btn btn-primary" type="submit">Ajouter un membre</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<h1>{{responce}}</h1>
|
||||||
|
{% endblock %}
|
@ -0,0 +1 @@
|
|||||||
|
<a href="/log">Se connecter</button>
|
@ -0,0 +1,40 @@
|
|||||||
|
{% extends "authbase.html.twig" %}
|
||||||
|
|
||||||
|
{% block css %}{{css}}{% endblock %}
|
||||||
|
|
||||||
|
{% block title %}Connexion - HearthTrack{% endblock %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
<div class="container">
|
||||||
|
<div class="row justify-content-center">
|
||||||
|
<div class="col-lg-5">
|
||||||
|
<div class="card shadow-lg border-0 rounded-lg mt-5">
|
||||||
|
<div class="card-header"><h3 class="text-center font-weight-light my-4">Connexion</h3></div>
|
||||||
|
<div class="card-body">
|
||||||
|
<form method="post" action="/login">
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input class="form-control" id="username" name="username" type="text" placeholder="Nom d'utilisateur" />
|
||||||
|
<label for="username">Nom d'utilisateur</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input class="form-control" id="mdp" name="mdp" type="password" placeholder="Mot de passe" />
|
||||||
|
<label for="mdp">Mot de passe</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-check mb-3">
|
||||||
|
<input class="form-check-input" id="inputRememberPassword" type="checkbox" value="" />
|
||||||
|
<label class="form-check-label" for="inputRememberPassword">Mémoriser le mot de passe</label>
|
||||||
|
</div>
|
||||||
|
<div class="d-flex align-items-center justify-content-between mt-4 mb-0">
|
||||||
|
<a class="small" href="/pass">Mot de passe oublié ?</a>
|
||||||
|
<button class="btn btn-primary" type="submit">Se connecter</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="card-footer text-center py-3">
|
||||||
|
<div class="small"><a href="/regist">Besoin d'un compte ? Inscrivez-vous !</a></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
@ -0,0 +1,33 @@
|
|||||||
|
{% extends "authbase.html.twig" %}
|
||||||
|
|
||||||
|
{% block css %}{{css}}{% endblock %}
|
||||||
|
|
||||||
|
{% block title %}Mot de passe oublié - HearthTrack{% endblock %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
<div class="container">
|
||||||
|
<div class="row justify-content-center">
|
||||||
|
<div class="col-lg-5">
|
||||||
|
<div class="card shadow-lg border-0 rounded-lg mt-5">
|
||||||
|
<div class="card-header"><h3 class="text-center font-weight-light my-4">Récupération du mot de passe</h3></div>
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="small mb-3 text-muted">Entrez votre adresse eMail pour recevoir un lien pour changer de mot de passe</div>
|
||||||
|
<form method="post" action="/password">
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input class="form-control" id="email" name="email" type="email" placeholder="name@example.com" />
|
||||||
|
<label for="email">Adresse eMail</label>
|
||||||
|
</div>
|
||||||
|
<div class="d-flex align-items-center justify-content-between mt-4 mb-0">
|
||||||
|
<a class="small" href="/log">Retour à la connexion</a>
|
||||||
|
<button class="btn btn-primary" type="submit">Réinitialiser votre mot de passe</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="card-footer text-center py-3">
|
||||||
|
<div class="small"><a href="/regist">Besoin d'un compte ? Inscrivez-vous !</a></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
@ -0,0 +1,98 @@
|
|||||||
|
{% extends "authbase.html.twig" %}
|
||||||
|
|
||||||
|
{% block css %}{{css}}{% endblock %}
|
||||||
|
|
||||||
|
{% block title %}Inscription - HearthTrack{% endblock %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
<div class="container">
|
||||||
|
<div class="row justify-content-center">
|
||||||
|
<div class="col-lg-7">
|
||||||
|
<div class="card shadow-lg border-0 rounded-lg mt-5">
|
||||||
|
<div class="card-header"><h3 class="text-center font-weight-light my-4">Créer un compte</h3></div>
|
||||||
|
<div class="card-body">
|
||||||
|
<form method="post" action="/register">
|
||||||
|
<div class="row mb-3">
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-floating mb-3 mb-md-0">
|
||||||
|
<input class="form-control" id="nom" name="nom" type="text" placeholder="Entrez votre nom" />
|
||||||
|
<label for="nom">Nom de famille</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-floating">
|
||||||
|
<input class="form-control" id="prenom" name="prenom" type="text" placeholder="Entrez votre prénom" />
|
||||||
|
<label for="prenom">Prénom</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row mb-3">
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-floating mb-3 mb-md-0">
|
||||||
|
<input class="form-control" id="username" name="username" type="text" placeholder="Entrez votre pseudonyme" />
|
||||||
|
<label for="username">Nom d'utilisateur</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-floating">
|
||||||
|
<input class="form-control" id="sexe" name="sexe" type="hidden">
|
||||||
|
<select id="sexe" name="sexe">
|
||||||
|
<option value="male">Homme</option>
|
||||||
|
<option value="female">Femme</option>
|
||||||
|
<option value="unknown">Ne se prononce pas</option>
|
||||||
|
</select>
|
||||||
|
</input>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input class="form-control" id="mail" type="mail" placeholder="nom@exemple.com" />
|
||||||
|
<label for="mail">Adresse eMail</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input class="form-control" id="dateNaissance" name="dateNaissance" type="date" placeholder="" />
|
||||||
|
<label for="dateNaissance">Date de naissance</label>
|
||||||
|
</div>
|
||||||
|
<div class="row mb-3">
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-floating mb-3 mb-md-0">
|
||||||
|
<input class="form-control" id="taille" name="taille" type="text" placeholder="Entrez votre taille" />
|
||||||
|
<label for="taille">Taille</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-floating mb-3 mb-md-0">
|
||||||
|
<input class="form-control" id="poids" name="poids" type="text" placeholder="Entrez votre poids" />
|
||||||
|
<label for="poids">Poids</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row mb-3">
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-floating mb-3 mb-md-0">
|
||||||
|
<input class="form-control" id="mdp" name="mdp" type="password" placeholder="Créez un mot de passe" />
|
||||||
|
<label for="mdp">Mot de passe</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-6">
|
||||||
|
<div class="form-floating mb-3 mb-md-0">
|
||||||
|
<input class="form-control" id="confirmMdp" name="confirmMdp" type="password" placeholder="Confirmez votre mot de passe" />
|
||||||
|
<label for="confirmMdp">Confirmer le mot de passe</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="mt-4 mb-0">
|
||||||
|
<div class="d-grid">
|
||||||
|
<button type="submit" class="btn btn-primary btn-block">Créer un compte</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="card-footer text-center py-3">
|
||||||
|
<div class="small"><a href="/log">Avez-vous déjà un compte ? Connectez-vous ?</a></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
@ -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,17 @@
|
|||||||
|
document.getElementById('saveButton').addEventListener('click', function() {
|
||||||
|
var preferences = {
|
||||||
|
notifications: document.getElementById('notif').checked,
|
||||||
|
theme: document.getElementById('theme').value
|
||||||
|
};
|
||||||
|
|
||||||
|
fetch('/savePreferences', {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(preferences),
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => console.log(data))
|
||||||
|
.catch((error) => console.error('Error:', error));
|
||||||
|
});
|
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Data\Core;
|
||||||
|
|
||||||
|
class Preferences {
|
||||||
|
private String $cookie;
|
||||||
|
private Array $theme;
|
||||||
|
|
||||||
|
public function __construct(){
|
||||||
|
if (isset($_COOKIE['preferences'])){
|
||||||
|
$this->cookie = $_COOKIE['preferences'];
|
||||||
|
} else {
|
||||||
|
$this->cookie = setcookie('preferences', 'base_theme', time()+(3600*24)*7);
|
||||||
|
}
|
||||||
|
$this->theme = array(
|
||||||
|
'base_theme',
|
||||||
|
'dark_theme',
|
||||||
|
'pink_theme'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function majCookie(String $maj){
|
||||||
|
try{
|
||||||
|
foreach($this->theme as $t){
|
||||||
|
$this->cookie = $maj;
|
||||||
|
setcookie('preferences', $maj);
|
||||||
|
}
|
||||||
|
} catch (Exception $e){
|
||||||
|
throw new ValueError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCookie():String{
|
||||||
|
return $this->cookie;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -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,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,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
|
||||||
|
{
|
||||||
|
}
|
Loading…
Reference in new issue