NoteCat/src/Dependencies.php

48 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace NoteCat;
use DI\Container;
use Psr\Container\ContainerInterface;
use Slim\Views\Twig;
class Dependencies
{
private const TWIG = 'view';
private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates';
public static function createContainer(): Container
{
$container = new Container();
// App settings
$container->set(Settings::class, function (): Settings {
return new Settings();
});
// Twig template engine
$container->set(self::TWIG, function (ContainerInterface $c) {
/** @var Settings $settings */
$settings = $c->get(Settings::class);
return Twig::create(self::TWIG_TEMPLATE_DIR, $settings->getTwigSettings());
});
// Controllers
$container->set(HelloWorldController::class, function (ContainerInterface $c) {
return new HelloWorldController(
$c->get(self::TWIG),
$c->get(HelloWorld::class)
);
});
// Services
$container->set(HelloWorld::class, function () {
return new HelloWorld();
});
return $container;
}
}