Compare commits
3 Commits
b60d9481ab
...
a88e861792
Author | SHA1 | Date |
---|---|---|
tracer | a88e861792 | |
tracer | 6284918056 | |
tracer | 33618b3be0 |
|
@ -0,0 +1,33 @@
|
|||
<?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,9 +18,15 @@ use Closure;
|
|||
* 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.
|
||||
*/
|
||||
|
||||
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)
|
||||
{
|
||||
|
@ -47,6 +53,12 @@ class Router
|
|||
$regex = '/^' . str_replace(search: "/", replace: '\\/', subject: $regex) . '$/i';
|
||||
$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;
|
||||
}
|
||||
|
||||
|
@ -57,11 +69,23 @@ class Router
|
|||
{
|
||||
$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)) {
|
||||
$parameters = [];
|
||||
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
|
||||
// Uncaught Error: Unknown named parameter $args in …
|
||||
|
|
|
@ -12,6 +12,9 @@ namespace App\Service;
|
|||
* As I'm not allowed to use 3rd party code like Twig or Smarty I ended up
|
||||
* using PHP as a templating engine.
|
||||
*/
|
||||
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
class Template
|
||||
{
|
||||
/*
|
||||
|
@ -25,13 +28,20 @@ class Template
|
|||
/*
|
||||
* Add variables to template and throw it out
|
||||
*/
|
||||
#[NoReturn]
|
||||
public function render(string $templateName, array $vars = []): void
|
||||
{
|
||||
// assign template vars
|
||||
foreach ($vars as $name => $value) {
|
||||
$$name = $value;
|
||||
}
|
||||
|
||||
include $this->templateDir . $templateName;
|
||||
$templateFile = $this->templateDir . $templateName;
|
||||
if (file_exists(filename: $templateFile)) {
|
||||
include $this->templateDir . $templateName;
|
||||
} else {
|
||||
die("Missing template: $templateFile");
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue