basic website, basic templating
This commit is contained in:
14
src/Controller/AddressBook.php
Normal file
14
src/Controller/AddressBook.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\Template;
|
||||
use stdClass;
|
||||
|
||||
class AddressBook extends stdClass
|
||||
{
|
||||
public function __construct(Template $template)
|
||||
{
|
||||
$template->render(templateName: 'index.tpl');
|
||||
}
|
||||
}
|
16
src/Entity/AddressBookEntry.php
Normal file
16
src/Entity/AddressBookEntry.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
class AddressBookEntry
|
||||
{
|
||||
public function __construct(
|
||||
private int $userid,
|
||||
private string $first,
|
||||
private string $last,
|
||||
private string $nick,
|
||||
)
|
||||
{
|
||||
// empty body
|
||||
}
|
||||
}
|
17
src/Entity/User.php
Normal file
17
src/Entity/User.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
class User
|
||||
{
|
||||
public function __construct(
|
||||
private string $nick,
|
||||
private string $password,
|
||||
private string $first = '',
|
||||
private string $last = '',
|
||||
private int $id = 0
|
||||
)
|
||||
{
|
||||
// empty body
|
||||
}
|
||||
}
|
33
src/Service/Container.php
Normal file
33
src/Service/Container.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Controller\AddressBook;
|
||||
use Exception;
|
||||
use stdClass;
|
||||
|
||||
class Container
|
||||
{
|
||||
// no autowiring yet, maybe later, but it might fit for a demo
|
||||
|
||||
private Template $template;
|
||||
private AddressBook $addressBook;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->template = new Template(templateDir: dirname(path: __DIR__, levels: 2) . '/templates/');
|
||||
$this->addressBook = new AddressBook(template: $this->template);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(string $class): stdClass
|
||||
{
|
||||
return match($class) {
|
||||
'App\Controller\AddressBook' => $this->addressBook,
|
||||
default => throw new Exception(message: "Missing class definition: $class")
|
||||
};
|
||||
}
|
||||
}
|
40
src/Service/Template.php
Normal file
40
src/Service/Template.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Template
|
||||
{
|
||||
public function __construct(private readonly string $templateDir)
|
||||
{
|
||||
// empty body
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function render(string $templateName): void
|
||||
{
|
||||
$template = file_get_contents(filename: $this->templateDir . $templateName);
|
||||
|
||||
// search for includes
|
||||
preg_match_all(pattern: '/{% include ?\'?(.*?)\'? ?%}/i', subject: $template, matches: $matches, flags: PREG_SET_ORDER);
|
||||
|
||||
foreach ($matches as $value) {
|
||||
$token = $value[0];
|
||||
$include = $this->templateDir . $value[1];
|
||||
if (file_exists(filename: $include)) {
|
||||
$replacement = file_get_contents(filename: $include);
|
||||
} else {
|
||||
throw new Exception(message: "Missing included file: $include");
|
||||
}
|
||||
$template = str_replace(search: $token, replace: $replacement, subject: $template);
|
||||
}
|
||||
|
||||
// remove the original template code
|
||||
$template = preg_replace(pattern: '/{% include ?\'?(.*?)\'? ?%}/i', replacement: '', subject: $template);
|
||||
|
||||
echo $template;
|
||||
}
|
||||
}
|
18
src/bootstrap.php
Normal file
18
src/bootstrap.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
spl_autoload_register(callback: function($className) {
|
||||
$prefix = 'App';
|
||||
$baseDir = __DIR__;
|
||||
|
||||
$len = strlen(string: $prefix);
|
||||
if (strncmp(string1: $prefix, string2: $className, length: $len) !== 0) {
|
||||
return;
|
||||
}
|
||||
$realClassName = substr(string: $className, offset: $len);
|
||||
$classLocation = $baseDir . str_replace(search: '\\', replace: '/', subject: $realClassName) . '.php';
|
||||
if (file_exists(filename: $classLocation)) {
|
||||
require $classLocation;
|
||||
} else {
|
||||
die("Invalid class: $className");
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user