Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .phpstan/baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 21 additions & 32 deletions src/ServiceProvider/BindingClosureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class BindingClosureFactory
/**
* @var LazyLoadingValueHolderFactory
*/
private $proxyFactory;
private LazyLoadingValueHolderFactory $proxyFactory;

/**
* @var InjectorInterface
*/
private $injector;
private InjectorInterface $injector;

/**
* BindingClosureFactory constructor.
Expand All @@ -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);
};
}

Expand All @@ -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;
}
);
};
}

Expand All @@ -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);
Expand All @@ -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;
}
Expand Down