Compare commits
No commits in common. "a88e861792c20bc878262183e6bed130156d8053" and "b60d9481ab654476882d29984e1e645537279cee" have entirely different histories.
a88e861792
...
b60d9481ab
|
@ -1,33 +0,0 @@
|
||||||
<?php
|
|
||||||
/*
|
|
||||||
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
|
|
||||||
*
|
|
||||||
* For the full copyright and license information, please view the LICENSE
|
|
||||||
* file that was distributed with this source code.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace App\Controller;
|
|
||||||
|
|
||||||
use App\Entity\User;
|
|
||||||
use App\Service\Template;
|
|
||||||
use App\Repository\UserRepository;
|
|
||||||
|
|
||||||
class AddressBookController
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
private readonly Template $template,
|
|
||||||
private readonly User $user,
|
|
||||||
private readonly UserRepository $userRepository
|
|
||||||
)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function main(): void
|
|
||||||
{
|
|
||||||
$this->template->render(templateName: 'index.html.php', vars: [
|
|
||||||
'user' => $this->user
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -18,15 +18,9 @@ use Closure;
|
||||||
* Currently it doesn't handle GET requests, as not needed.
|
* Currently it doesn't handle GET requests, as not needed.
|
||||||
* But if I reuse the code in my bind Api I'll enable GET as well.
|
* But if I reuse the code in my bind Api I'll enable GET as well.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Router
|
class Router
|
||||||
{
|
{
|
||||||
/*
|
private array $routes;
|
||||||
* The easiest wy to differentiate between static and dynamic routes is using
|
|
||||||
* two arrays, no need to pollute the class Route with that information
|
|
||||||
*/
|
|
||||||
private array $staticRoutes;
|
|
||||||
private array $dynamicRoutes;
|
|
||||||
|
|
||||||
public function __construct(private readonly Template $template)
|
public function __construct(private readonly Template $template)
|
||||||
{
|
{
|
||||||
|
@ -53,12 +47,6 @@ class Router
|
||||||
$regex = '/^' . str_replace(search: "/", replace: '\\/', subject: $regex) . '$/i';
|
$regex = '/^' . str_replace(search: "/", replace: '\\/', subject: $regex) . '$/i';
|
||||||
$route = new Route(name: $name, route: $route, regEx: $regex, parameters: $parameters, callback: $callback);
|
$route = new Route(name: $name, route: $route, regEx: $regex, parameters: $parameters, callback: $callback);
|
||||||
|
|
||||||
if ($parameters) {
|
|
||||||
$this->dynamicRoutes[] = $route;
|
|
||||||
} else {
|
|
||||||
$this->staticRoutes[] = $route;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->routes[] = $route;
|
$this->routes[] = $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,23 +57,11 @@ class Router
|
||||||
{
|
{
|
||||||
$requestUri = $_SERVER['REQUEST_URI'];
|
$requestUri = $_SERVER['REQUEST_URI'];
|
||||||
|
|
||||||
/*
|
foreach ($this->routes as $route) {
|
||||||
* Static routes have preference over dynamic ones, so
|
|
||||||
* /admin/user/add to add and
|
|
||||||
* /admin/user/{name} to edit is possible.
|
|
||||||
*/
|
|
||||||
foreach ($this->staticRoutes as $route) {
|
|
||||||
if (preg_match(pattern: $route->getRegex(), subject: $requestUri, matches: $matches)) {
|
|
||||||
call_user_func(callback: $route->getCallback());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->dynamicRoutes as $route) {
|
|
||||||
if (preg_match(pattern: $route->getRegex(), subject: $requestUri, matches: $matches)) {
|
if (preg_match(pattern: $route->getRegex(), subject: $requestUri, matches: $matches)) {
|
||||||
$parameters = [];
|
$parameters = [];
|
||||||
foreach ($route->getParameters() as $id => $parameter) {
|
foreach ($route->getParameters() as $id => $parameter) {
|
||||||
$parameters[$parameter] = $matches[$id + 1];
|
$parameters[$parameter] = $matches[$id +1];
|
||||||
}
|
}
|
||||||
// PHP is mad about named parameters in call_user_func
|
// PHP is mad about named parameters in call_user_func
|
||||||
// Uncaught Error: Unknown named parameter $args in …
|
// Uncaught Error: Unknown named parameter $args in …
|
||||||
|
|
|
@ -12,9 +12,6 @@ namespace App\Service;
|
||||||
* As I'm not allowed to use 3rd party code like Twig or Smarty I ended up
|
* As I'm not allowed to use 3rd party code like Twig or Smarty I ended up
|
||||||
* using PHP as a templating engine.
|
* using PHP as a templating engine.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use JetBrains\PhpStorm\NoReturn;
|
|
||||||
|
|
||||||
class Template
|
class Template
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
|
@ -28,20 +25,13 @@ class Template
|
||||||
/*
|
/*
|
||||||
* Add variables to template and throw it out
|
* Add variables to template and throw it out
|
||||||
*/
|
*/
|
||||||
#[NoReturn]
|
|
||||||
public function render(string $templateName, array $vars = []): void
|
public function render(string $templateName, array $vars = []): void
|
||||||
{
|
{
|
||||||
// assign template vars
|
|
||||||
foreach ($vars as $name => $value) {
|
foreach ($vars as $name => $value) {
|
||||||
$$name = $value;
|
$$name = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
$templateFile = $this->templateDir . $templateName;
|
include $this->templateDir . $templateName;
|
||||||
if (file_exists(filename: $templateFile)) {
|
|
||||||
include $this->templateDir . $templateName;
|
|
||||||
} else {
|
|
||||||
die("Missing template: $templateFile");
|
|
||||||
}
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue