picea/docs/99-dependency-injection.md

3.1 KiB

Using Dependency Injection

Here's an excerpt from Lean's definition file 'template.php' which also loads Picea\Ui.

It also insert Tell inside the LanguageHandler which handles the {% _ 'relative.lang.key' %} and {% lang 'my.full.lang.key' %}.

use function DI\autowire, DI\create, DI\get;

use Laminas\Diactoros\Response\HtmlResponse;

use Picea\{ Picea, Caching\Cache, Caching\Opcache, Compiler, Compiler\Context, Compiler\BaseContext, FileFetcher, Language\DefaultRegistrations, Method\Request };
use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension };
use Picea\Ui\{ Method, Ui };

return [
    Picea::class => function($c) {
        return new Picea($c->get(Context::class), $c->get(Cache::class), $c->get(Compiler::class), null, $c->get(FileFetcher::class), null, getenv("DEBUG"));
    },

    Context::class => function($c) {
        return new BaseContext( $c->get(Lean\Lean::class)->getPiceaContext() );
    },

    Compiler::class => function($c) {
        return new Compiler(new class(array_merge([
            $c->get(LanguageExtension::class),
            $c->get(TitleExtension::class),
            $c->get(NumberExtension::class),
            $c->get(UrlExtension::class),
            $c->get(Method\Form::class),
            $c->get(Method\Pagination::class),
            $c->get(Request::class),
        ], class_exists(\Taxus\Picea\Extension::class) ? [ $c->get(\Taxus\Picea\Extension::class) ] : [],
            array_map(fn($class) => $c->get($class), $c->get(Lean\Lean::class)->getPiceaExtensions() ))) extends DefaultRegistrations {

            public function registerAll(Compiler $compiler) : void
            {
                parent::registerAll($compiler);
                ( new Ui() )->registerFormExtension($compiler);
            }

        });
    },

    Request::class => autowire(Request::class),

    Method\Form::class => autowire(Method\Form::class),

    Method\Pagination::class => autowire(Method\Pagination::class),

    LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandlerInterface::class)),

    LanguageHandlerInterface::class => function($c) {
        return new class( $c->get(Tell\I18n::class) ) implements LanguageHandlerInterface {
            public Tell\I18n $tell;

            public function __construct(Tell\I18n $tell) {
                $this->tell = $tell;
            }

            public function languageFromKey(string $key, array $variables = []) : array|string
            {
                return $this->tell->fromKey($key, $variables) ?: "";
            }
        };
    },

    TitleExtension::class => autowire(TitleExtension::class),

    NumberExtension::class => autowire(NumberExtension::class),

    UrlExtension::class => create(UrlExtension::class)->constructor(get(Context::class), getenv("URL_BASE"), get('git.commit')),

    Cache::class => create(Opcache::class)->constructor(getenv("CACHE_PATH"), get(Context::class)),

    FileFetcher::class => function($c) {
        return new FileFetcher($c->get(Lean\Lean::class)->getViewPaths());
    },
];