how to get presenter factory from container
- jeremy
- Member | 54
I used decorator to add tags to my base presenter and any presenter extending that base presenter automatically gets the tags too.
Neon config:
decorator:
Backend\Presenters\BasePresenter:
tags: [presenter: 'Backend']
Frontend\Presenters\BasePresenter:
tags: [presenter: 'Frontend']
If you don't want a specific presenter (like Error4xx
) to be
included then you can set the tag for that presenter to null
:
decorator:
Backend\Presenters\Error4xxPresenter:
tags: [presenter: null]
Frontend\Presenters\Error4xxPresenter:
tags: [presenter: null]
Backend\Presenters\BasePresenter:
tags: [presenter: 'Backend']
Frontend\Presenters\BasePresenter:
tags: [presenter: 'Frontend']
My config is little bit different, but this should give you an idea.
Then I have an extension that finds all the presenters:
final class Presenters extends CompilerExtension {
public function loadConfiguration(): void {
$builder = $this->getContainerBuilder();
$builder->addDefinition($this->name)
->setFactory(App\Services\Presenters::class);
}
public function beforeCompile(): void {
$builder = $this->getContainerBuilder();
$presentersByTag = $builder->findByTag('presenter');
$presenters = [];
foreach ($presentersByTag as $presenter => $module) { // $module = value of the tag 'presenter'
if (!$module) {
continue;
}
$definition = $builder->getDefinition($presenter);
$presenters[$module][] = (new \ReflectionClass($definition->getType()))->getShortName();
}
$builder->getDefinition($this->name)->setArgument('presenters', $presenters); // Pass all the found presenters to the service constructor
}
}
Again this is a simplified example of my code, just to give you an idea.
Last edited by jeremy (2023-09-03 15:29)
- alnux
- Member | 139
jeremy wrote:
I used decorator to add tags to my base presenter and any presenter extending that base presenter automatically gets the tags too.
Neon config:
decorator: Backend\Presenters\BasePresenter: tags: [presenter: 'Backend'] Frontend\Presenters\BasePresenter: tags: [presenter: 'Frontend']
If you don't want a specific presenter (like
Error4xx
) to be included then you can set the tag for that presenter tonull
:decorator: Backend\Presenters\Error4xxPresenter: tags: [presenter: null] Frontend\Presenters\Error4xxPresenter: tags: [presenter: null] Backend\Presenters\BasePresenter: tags: [presenter: 'Backend'] Frontend\Presenters\BasePresenter: tags: [presenter: 'Frontend']
My config is little bit different, but this should give you an idea.
Then I have an extension that finds all the presenters:
final class Presenters extends CompilerExtension { public function loadConfiguration(): void { $builder = $this->getContainerBuilder(); $builder->addDefinition($this->name) ->setFactory(App\Services\Presenters::class); } public function beforeCompile(): void { $builder = $this->getContainerBuilder(); $presentersByTag = $builder->findByTag('presenter'); $presenters = []; foreach ($presentersByTag as $presenter => $module) { // $module = value of the tag 'presenter' if (!$module) { continue; } $definition = $builder->getDefinition($presenter); $presenters[$module][] = (new \ReflectionClass($definition->getType()))->getShortName(); } $builder->getDefinition($this->name)->setArgument('presenters', $presenters); // Pass all the found presenters to the service constructor } }
Again this is a simplified example of my code, just to give you an idea.
thanks @jeremy, it give me some ideas. you are a crack