diff --git a/.phpstan/baseline.neon b/.phpstan/baseline.neon index efacfbc..1d263b4 100644 --- a/.phpstan/baseline.neon +++ b/.phpstan/baseline.neon @@ -120,12 +120,6 @@ parameters: count: 1 path: ../src/InjectorServiceProvider.php - - - message: '#^Method Bigcommerce\\Injector\\InjectorServiceProvider\:\:get\(\) has InvalidArgumentException in PHPDoc @throws tag but it''s not thrown\.$#' - identifier: throws.unusedType - count: 1 - path: ../src/InjectorServiceProvider.php - - message: '#^Call to an undefined method ReflectionType\:\:getName\(\)\.$#' identifier: method.notFound diff --git a/src/ServiceProvider/BindingClosureFactory.php b/src/ServiceProvider/BindingClosureFactory.php index a60e43e..df9bacb 100644 --- a/src/ServiceProvider/BindingClosureFactory.php +++ b/src/ServiceProvider/BindingClosureFactory.php @@ -15,12 +15,12 @@ class BindingClosureFactory /** * @var LazyLoadingValueHolderFactory */ - private $proxyFactory; + private LazyLoadingValueHolderFactory $proxyFactory; /** * @var InjectorInterface */ - private $injector; + private InjectorInterface $injector; /** * BindingClosureFactory constructor. @@ -43,9 +43,10 @@ public function __construct(LazyLoadingValueHolderFactory $proxyFactory, Injecto */ public function createAutoWireClosure($className, ?callable $parameterFactory = null) { - return function (Container $app) use ($className, $parameterFactory) { + $injector = $this->injector; + return static function (Container $app) use ($className, $parameterFactory, $injector) { $parameters = $parameterFactory ? $parameterFactory($app) : []; - return $this->injector->create($className, $parameters); + return $injector->create($className, $parameters); }; } @@ -64,9 +65,18 @@ public function createAutoWireClosure($className, ?callable $parameterFactory = */ public function createAutoWireProxyClosure($className, ?callable $parameterFactory = null) { - return function (Container $app) use ($className, $parameterFactory) { - $serviceFactory = $this->createAutoWireClosure($className, $parameterFactory); - return $this->createProxy($className, $serviceFactory, $app); + $proxyFactory = $this->proxyFactory; + $createAutoWireClosure = [$this, 'createAutoWireClosure']; + return static function (Container $app) use ($className, $parameterFactory, $proxyFactory, $createAutoWireClosure) { + $serviceFactory = $createAutoWireClosure($className, $parameterFactory); + return $proxyFactory->createProxy( + $className, + static function (&$wrappedObject, $proxy, $method, $parameters, &$initializer) use ($serviceFactory, $app) { + $wrappedObject = $serviceFactory($app); + $initializer = null; + return true; + } + ); }; } @@ -83,9 +93,10 @@ public function createAutoWireProxyClosure($className, ?callable $parameterFacto */ public function createServiceProxy(Container $app, string $serviceName, string $serviceClassName) { - return $this->createProxy( + $proxyFactory = $this->proxyFactory; + return $proxyFactory->createProxy( $serviceClassName, - function (Container $app) use ($serviceName, $serviceClassName) { + static function (&$wrappedObject, $proxy, $method, $parameters, &$initializer) use ($app, $serviceName, $serviceClassName) { $service = $app->offsetGet($serviceName); if (! ($service instanceof $serviceClassName)) { $invalidClassName = get_class($service); @@ -96,29 +107,7 @@ function (Container $app) use ($serviceName, $serviceClassName) { "binding and make sure it specifies the actual class name that will be returned by that service." ); } - return $service; - }, - $app - ); - } - - /** - * Create a project object for the specified ClassName bound to the given ServiceFactory method. - * @param string $className - * @param callable $serviceFactory - * @param Container $app - * @return \ProxyManager\Proxy\VirtualProxyInterface - */ - private function createProxy($className, callable $serviceFactory, Container $app) - { - return $this->proxyFactory->createProxy( - $className, - function (&$wrappedObject, $proxy, $method, $parameters, &$initializer) use ( - $className, - $serviceFactory, - $app - ) { - $wrappedObject = $serviceFactory($app); + $wrappedObject = $service; $initializer = null; return true; }