Compare commits

...

3 Commits

3 changed files with 71 additions and 4 deletions

View File

@ -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
]);
}
}

View File

@ -18,9 +18,15 @@ 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)
{ {
@ -47,6 +53,12 @@ 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;
} }
@ -57,7 +69,19 @@ 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) {

View File

@ -12,6 +12,9 @@ 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
{ {
/* /*
@ -25,13 +28,20 @@ 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;
if (file_exists(filename: $templateFile)) {
include $this->templateDir . $templateName; include $this->templateDir . $templateName;
} else {
die("Missing template: $templateFile");
}
exit(0); exit(0);
} }
} }