diff --git a/src/Service/Container.php b/src/Service/Container.php index 84d9a7c..9ecb3de 100644 --- a/src/Service/Container.php +++ b/src/Service/Container.php @@ -1,29 +1,62 @@ + * + * 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\AddressBook; +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 { - // caveat: Classes are always instantiated - // No autowiring (yet, maybe later, but it might fit for a demo) - private Template $template; - private AddressBook $addressBook; - private Router $router; + 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->template = new Template(templateDir: dirname(path: __DIR__, levels: 2) . '/templates/'); - $this->addressBook = new AddressBook(template: $this->template); - $this->router = new Router(); + $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\AddressBook' => $this->addressBook, + '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")