2022-12-21 04:20:11 +00:00
|
|
|
# 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' %}`.
|
|
|
|
|
|
|
|
```php
|
|
|
|
use function DI\autowire, DI\create, DI\get;
|
|
|
|
|
2023-02-01 18:08:47 +00:00
|
|
|
use Laminas\Diactoros\Response\HtmlResponse;
|
2022-12-21 04:20:11 +00:00
|
|
|
|
|
|
|
use Picea\{ Picea, Caching\Cache, Caching\Opcache, Compiler, Compiler\Context, Compiler\BaseContext, FileFetcher, Language\DefaultRegistrations, Method\Request };
|
2023-10-18 00:12:39 +00:00
|
|
|
use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension };
|
2022-12-21 04:20:11 +00:00
|
|
|
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),
|
2023-03-29 15:13:41 +00:00
|
|
|
$c->get(NumberExtension::class),
|
2022-12-21 04:20:11 +00:00
|
|
|
$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),
|
|
|
|
|
2023-10-18 00:12:39 +00:00
|
|
|
LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandlerInterface::class)),
|
2022-12-21 04:20:11 +00:00
|
|
|
|
2023-10-18 00:12:39 +00:00
|
|
|
LanguageHandlerInterface::class => function($c) {
|
|
|
|
return new class( $c->get(Tell\I18n::class) ) implements LanguageHandlerInterface {
|
2022-12-21 04:20:11 +00:00
|
|
|
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),
|
|
|
|
|
2023-03-29 15:13:41 +00:00
|
|
|
NumberExtension::class => autowire(NumberExtension::class),
|
2022-12-21 04:20:11 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
},
|
|
|
|
];
|
|
|
|
```
|
|
|
|
|