diff --git a/docs/book/intro.md b/docs/book/intro.md index 94e3fd9..73773aa 100644 --- a/docs/book/intro.md +++ b/docs/book/intro.md @@ -120,6 +120,30 @@ class Module } ``` +### MVC Application bootstrapping + +Until 1.9.x laminas-cli did not bootrap the MVC Application and therefore left the Application in an unready state +wehen executing commands via laminas-cli. (read this [Issue](https://github.com/laminas/laminas-cli/issues/106)) + +To allow bootstrapping the MVC Application, without breaking backward compatibility, the option `'bootstrap_mvc_application'` +was introduced. Currently it's `false` by default to not break any Apps. This might change in the future. + +When enabling it, make sure not to do any HTTP-Only related stuff on Module's `onBootstrap` method. + +```php +// File config/application.config.php + + [ + // execute Laminas\Mvc\Application::init(), including ::boostrap() during initialization of cli app + 'bootstrap_mvc_application' => true, + ] +]; +``` + ## Integration in Other Applications laminas-cli supports [Laminas MVC](https://github.com/laminas/laminas-mvc-skeleton) diff --git a/src/ContainerResolver.php b/src/ContainerResolver.php index b7a2036..f97c2da 100644 --- a/src/ContainerResolver.php +++ b/src/ContainerResolver.php @@ -6,6 +6,7 @@ use InvalidArgumentException; use Laminas\ModuleManager\ModuleManagerInterface; +use Laminas\Mvc\Application; use Laminas\Mvc\Service\ServiceManagerConfig; use Laminas\ServiceManager\ServiceManager; use Laminas\Stdlib\ArrayUtils; @@ -18,6 +19,9 @@ use function file_exists; use function sprintf; use function str_contains; +use function trigger_error; + +use const E_USER_DEPRECATED; /** * @internal @@ -98,18 +102,31 @@ private function resolveMvcContainer(string $path): ContainerInterface Assert::isMap($appConfig); } - $servicesConfig = $appConfig['service_manager'] ?? []; - Assert::isMap($servicesConfig); + Assert::classExists(Application::class); + + if ($appConfig['laminas-cli']['bootstrap_mvc_application'] ?? false) { + // initialize & bootstrap MVC Application + $mvcApplication = Application::init($appConfig); + $serviceManager = $mvcApplication->getServiceManager(); + } else { + /* @deprecated MVC Application is not bootstrapped */ + trigger_error('Running laminas-cli in MVC Environment without bootstrapping ' + . 'the MVC Application is deprecated. ' + . '@see https://github.com/laminas/laminas-cli/issues/106', E_USER_DEPRECATED); - $smConfig = new ServiceManagerConfig($servicesConfig); + $servicesConfig = $appConfig['service_manager'] ?? []; + Assert::isMap($servicesConfig); - $serviceManager = new ServiceManager(); - $smConfig->configureServiceManager($serviceManager); - $serviceManager->setService('ApplicationConfig', $appConfig); + $smConfig = new ServiceManagerConfig($servicesConfig); - $moduleManager = $serviceManager->get('ModuleManager'); - Assert::isInstanceOf($moduleManager, ModuleManagerInterface::class); - $moduleManager->loadModules(); + $serviceManager = new ServiceManager(); + $smConfig->configureServiceManager($serviceManager); + $serviceManager->setService('ApplicationConfig', $appConfig); + + $moduleManager = $serviceManager->get('ModuleManager'); + Assert::isInstanceOf($moduleManager, ModuleManagerInterface::class); + $moduleManager->loadModules(); + } return $serviceManager; }