55namespace LaminasPhpStan ;
66
77use Interop \Container \ContainerInterface as InteropContainerInterface ;
8- use Laminas \Cache \ConfigProvider ;
9- use Laminas \Mvc \Service \ServiceListenerFactory ;
10- use Laminas \Mvc \Service \ServiceManagerConfig ;
118use Laminas \ServiceManager \ServiceLocatorInterface ;
129use Laminas \ServiceManager \ServiceManager ;
1310use PHPStan \ShouldNotHappenException ;
1411use Psr \Container \ContainerInterface as PsrContainerInterface ;
15- use ReflectionProperty ;
1612
17- final class ServiceManagerLoader
13+ final readonly class ServiceManagerLoader
1814{
19- private ?UnmappedAliasServiceLocatorProxy $ serviceLocator = null ;
20-
21- /** @var string[] */
22- private array $ knownModules = [
23- ConfigProvider::class,
24- \Laminas \Filter \ConfigProvider::class,
25- \Laminas \Form \ConfigProvider::class,
26- \Laminas \Hydrator \ConfigProvider::class,
27- \Laminas \I18n \ConfigProvider::class,
28- \Laminas \InputFilter \ConfigProvider::class,
29- \Laminas \Mail \ConfigProvider::class,
30- \Laminas \Paginator \ConfigProvider::class,
31- \Laminas \Router \ConfigProvider::class,
32- \Laminas \Validator \ConfigProvider::class,
33- ];
34-
35- /** @var array<string, true> */
36- private array $ serviceManagerNames = [
15+ private const serviceManagerNames = [
3716 ServiceManager::class => true ,
3817 ServiceLocatorInterface::class => true ,
3918 InteropContainerInterface::class => true ,
4019 PsrContainerInterface::class => true ,
4120 ];
4221
43- public function __construct (?string $ serviceManagerLoader )
44- {
45- if (null === $ serviceManagerLoader ) {
46- return ;
47- }
22+ private UnmappedAliasServiceLocatorProxy $ serviceLocator ;
4823
24+ public function __construct (string $ serviceManagerLoader )
25+ {
4926 if (! \file_exists ($ serviceManagerLoader ) || ! \is_readable ($ serviceManagerLoader )) {
5027 throw new ShouldNotHappenException ('Service manager could not be loaded ' );
5128 }
@@ -60,32 +37,8 @@ public function __construct(?string $serviceManagerLoader)
6037
6138 public function getServiceLocator (string $ serviceManagerName ): ServiceLocatorInterface
6239 {
63- if (null === $ this ->serviceLocator ) {
64- $ serviceManager = new ServiceManager (['services ' => ['config ' => []]]);
65- if (\class_exists (ServiceManagerConfig::class)) {
66- (new ServiceManagerConfig ())->configureServiceManager ($ serviceManager );
67- }
68- if (\class_exists (ServiceListenerFactory::class)) {
69- $ refProp = new ReflectionProperty (ServiceListenerFactory::class, 'defaultServiceConfig ' );
70- $ config = $ refProp ->getValue (new ServiceListenerFactory ());
71- \assert (\is_array ($ config ));
72- \assert (\is_array ($ config ['factories ' ]));
73- unset($ config ['factories ' ]['config ' ]);
74- $ serviceManager ->configure ($ config );
75- }
76- foreach ($ this ->knownModules as $ module ) {
77- if (\class_exists ($ module )) {
78- $ module = new $ module ();
79- \assert (\method_exists ($ module , 'getDependencyConfig ' ));
80- $ serviceManager ->configure ($ module ->getDependencyConfig ());
81- }
82- }
83-
84- $ this ->serviceLocator = new UnmappedAliasServiceLocatorProxy ($ serviceManager );
85- }
86-
8740 $ serviceLocator = $ this ->serviceLocator ;
88- if (! isset ($ this -> serviceManagerNames [$ serviceManagerName ])) {
41+ if (! isset (self :: serviceManagerNames[$ serviceManagerName ])) {
8942 $ serviceLocator = $ serviceLocator ->get ($ serviceManagerName );
9043 \assert ($ serviceLocator instanceof ServiceLocatorInterface);
9144 }
0 commit comments