65 lines
2.7 KiB
PHP
65 lines
2.7 KiB
PHP
<?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\Service;
|
|
|
|
use App\Controller\AddressBookAdminController;
|
|
use App\Controller\AddressBookController;
|
|
use App\Controller\SecurityController;
|
|
use App\Entity\User;
|
|
use App\Repository\UserRepository;
|
|
|
|
/*
|
|
* A quick and dirty class container for DI.
|
|
* Caveat: Classes are always instantiated
|
|
* No autowiring (yet, maybe later, but it might fit for a demo)
|
|
*/
|
|
|
|
class Container
|
|
{
|
|
|
|
private AddressBookController $addressBook;
|
|
private AddressBookAdminController $addressBookAdmin;
|
|
private Config $config;
|
|
private DatabaseConnection $databaseConnection;
|
|
private Router $router;
|
|
private SecurityController $securityController;
|
|
private Template $template;
|
|
private User $user;
|
|
private UserRepository $userRepository;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->config = new Config();
|
|
$this->databaseConnection = new DatabaseConnection(config: $this->config);
|
|
$this->template = new Template(templateDir: dirname(path: __DIR__, levels: 2) . '/templates/');
|
|
$this->router = new Router(template: $this->template);
|
|
$this->userRepository = new UserRepository(databaseConnection: $this->databaseConnection);
|
|
$this->securityController = new SecurityController(template: $this->template, userRepository: $this->userRepository, router: $this->router);
|
|
if (empty($_SESSION['user_id'])) {
|
|
$this->user = new User(); // ANONYMOUS
|
|
} else {
|
|
$this->user = $this->userRepository->findByID(id: $_SESSION['user_id']);
|
|
}
|
|
$this->addressBook = new AddressBookController(template: $this->template, user: $this->user, userRepository: $this->userRepository, router: $this->router);
|
|
$this->addressBookAdmin = new AddressBookAdminController(template: $this->template, user: $this->user, userRepository: $this->userRepository, router: $this->router);
|
|
}
|
|
|
|
public function get(string $className): object
|
|
{
|
|
return match ($className) {
|
|
'App\Controller\AddressBookController' => $this->addressBook,
|
|
'App\Controller\AddressBookAdminController' => $this->addressBookAdmin,
|
|
'App\Controller\SecurityController' => $this->securityController,
|
|
'App\Service\Router' => $this->router,
|
|
//default => throw new Exception(message: "Missing class definition: $class")
|
|
default => die("Missing class definition: $className")
|
|
};
|
|
}
|
|
} |