From 58cb9cc2a1d7ce4b4c36a76fcaf821c45a1c56ce Mon Sep 17 00:00:00 2001 From: Falk Hermann Date: Thu, 9 Mar 2023 09:47:50 +0100 Subject: [PATCH 1/4] enabled bootstrapping of MVC Application optional via $appConfig['laminas-cli']['bootstrap_mvc_application'] Signed-off-by: Falk Hermann Signed-off-by: Falk Hermann --- src/ContainerResolver.php | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/ContainerResolver.php b/src/ContainerResolver.php index b7a2036..6234fa4 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; @@ -98,18 +99,31 @@ private function resolveMvcContainer(string $path): ContainerInterface Assert::isMap($appConfig); } - $servicesConfig = $appConfig['service_manager'] ?? []; - Assert::isMap($servicesConfig); + Assert::classExists(Application::class); - $smConfig = new ServiceManagerConfig($servicesConfig); + 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); - $serviceManager = new ServiceManager(); - $smConfig->configureServiceManager($serviceManager); - $serviceManager->setService('ApplicationConfig', $appConfig); + $servicesConfig = $appConfig['service_manager'] ?? []; + Assert::isMap($servicesConfig); - $moduleManager = $serviceManager->get('ModuleManager'); - Assert::isInstanceOf($moduleManager, ModuleManagerInterface::class); - $moduleManager->loadModules(); + $smConfig = new ServiceManagerConfig($servicesConfig); + + $serviceManager = new ServiceManager(); + $smConfig->configureServiceManager($serviceManager); + $serviceManager->setService('ApplicationConfig', $appConfig); + + $moduleManager = $serviceManager->get('ModuleManager'); + Assert::isInstanceOf($moduleManager, ModuleManagerInterface::class); + $moduleManager->loadModules(); + } return $serviceManager; } From 890a8b8d6620bc504cac5426e349a597739a8788 Mon Sep 17 00:00:00 2001 From: Falk Hermann Date: Thu, 9 Mar 2023 10:10:41 +0100 Subject: [PATCH 2/4] added documentation for config option 'bootstrap_mvc_application' Signed-off-by: Falk Hermann Signed-off-by: Falk Hermann --- docs/book/intro.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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) From 141e18858a10187ccf8bb99251c48a6c5d61ed35 Mon Sep 17 00:00:00 2001 From: Falk Hermann Date: Thu, 9 Mar 2023 10:19:48 +0100 Subject: [PATCH 3/4] PHPCS fixes Signed-off-by: Falk Hermann Signed-off-by: Falk Hermann --- src/ContainerResolver.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ContainerResolver.php b/src/ContainerResolver.php index 6234fa4..8378a16 100644 --- a/src/ContainerResolver.php +++ b/src/ContainerResolver.php @@ -15,10 +15,13 @@ use Symfony\Component\Console\Input\InputInterface; use Webmozart\Assert\Assert; +use const E_USER_DEPRECATED; + use function class_exists; use function file_exists; use function sprintf; use function str_contains; +use function trigger_error; /** * @internal @@ -107,9 +110,9 @@ private function resolveMvcContainer(string $path): ContainerInterface $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); + 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); $servicesConfig = $appConfig['service_manager'] ?? []; Assert::isMap($servicesConfig); From 794e80a907c05b4d52e2e655a095f8bf7db8e250 Mon Sep 17 00:00:00 2001 From: Falk Hermann Date: Thu, 9 Mar 2023 10:29:46 +0100 Subject: [PATCH 4/4] PHPCS fixes Signed-off-by: Falk Hermann Signed-off-by: Falk Hermann --- src/ContainerResolver.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ContainerResolver.php b/src/ContainerResolver.php index 8378a16..f97c2da 100644 --- a/src/ContainerResolver.php +++ b/src/ContainerResolver.php @@ -15,14 +15,14 @@ use Symfony\Component\Console\Input\InputInterface; use Webmozart\Assert\Assert; -use const E_USER_DEPRECATED; - use function class_exists; use function file_exists; use function sprintf; use function str_contains; use function trigger_error; +use const E_USER_DEPRECATED; + /** * @internal */ @@ -112,7 +112,7 @@ private function resolveMvcContainer(string $path): ContainerInterface /* @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); + . '@see https://github.com/laminas/laminas-cli/issues/106', E_USER_DEPRECATED); $servicesConfig = $appConfig['service_manager'] ?? []; Assert::isMap($servicesConfig);