FalkHe
March 8, 2023, 3:03pm
1
Looks like EventListeners are totally ignored when code is executed via laminas-cli.
The Reason is that neither Module::onBoostrap() nor the config key ‘listeners’ is handled by lamainas-cli.
But what’s the alternative? Where in a Laminas Module can EventListeners be configured / attached savely?
Looks like Module::init() ist the only place where code is excecuted in any case. But from this method there’s no access to the Service-Manager an therefor no way to fetch and attach the Listeners.
Usecase:
On each module, i have a ServiceLayer that triggers events to the EventManager. (i.E. ‘model-xy::entity:saved’, ‘session:sign-in’, ‘bussines-process:abc:finished’, …)
In other modules i need to attach to those Events. Not only on MVC Requests but also on laminas-cli calls. i.E. ‘busines-prozess:abc:finished’ might happen via MVC but might also happen via laminas-cli.
Any suggestion is appreciated
Thanks in advance
Please check the issue tracker of laminas-cli there will you find your answer. For example:
opened 09:10AM - 07 Dec 22 UTC
Bug
### Bug Report
| Q | A
|------------ | ------
| Version(s) | 1… .7.0
#### Summary
When using `laminas/laminas-cli` in the context of a MvcApplication (`ContainerResolver::resolveMvcContainer`), modules don't get bootstrapped completely, which results in services potentially missing configuration.
#### Current behavior
We use the `onBootstrap` event of our MvcApplication to set up listeners for services or set up service state based on context.
When using those services inside of commands executed by `laminas/laminas-cli`, those services are not fully set up, which results in unexpected behavior, since the application/module is missing the `bootstrap` lifecycle stage.
Based on the [Documentation](https://docs.laminas.dev/laminas-mvc/examples/#bootstrapping), the `onBootstrap` method should/could be used for:
* module-specific configuration
* setup event listeners for you module
#### How to reproduce
Use a service somehow modified or configured in the `onBootstrap` method in a `Command` executed through `laminas/laminas-cli`.
The service passed to the command has not gone through the MvcApplications bootstrap lifecycle.
I try to illustrate this on the following pseudo-module:
```php
// Module
class Module implements BootstrapListenerInterface
{
public function onBootstrap(EventInterface $e)
{
$sm = $e->getApplication()->getServiceManager();
$sm->get(SampleService::class)->setBootstrapped(true);
}
}
```
```php
// in SampleCommandFactory
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
{
return new SampleCommand(
$container->get(SampleService::class)
);
}
```
```php
// in SampleCommand
protected function execute(InputInterface $input, OutputInterface $output) : int
{
return $this->sampleService->isBootstrapped() ? 0 : 1;
}
```
`SampleCommand` returns `1`.
#### Expected behavior
From my perspective, using commands through `laminas/laminas-cli` in the context of an MvcApplication, should guarantee the same initialisation level as with `Application::init`.
In the above example, `SampleCommand` should return `0`, as it has access to a bootstrapped version of the `SampleService`.
But apparently no one is interested in solving the problem, but only in demanding that it must work.
Thank you for pushing the topic!
laminas:1.9.x
← FalkHe:fix-mvc-bootrapping
opened 09:13AM - 09 Mar 23 UTC
| Q | A
|-------------- | ------
| Documentation | yes
| Bugfix… | yes
| BC Break | no
| New Feature | yes
| RFC | yes/no
| QA | yes/no
### Description
Provide option to enable MVC Application bootstrapping.
False by default, to not break any existing app.
See Issue #106 .