Skip to content

Commit

Permalink
16
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 19, 2024
1 parent c323c83 commit 71645c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Collector/ContainerInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Yiisoft\Proxy\ObjectProxy;
use Yiisoft\Proxy\ProxyManager;
use Yiisoft\Proxy\ProxyTrait;
use Yiisoft\Yii\Debug\ProxyDecoratedCalls;
Expand All @@ -30,6 +31,9 @@ final class ContainerInterfaceProxy implements ContainerInterface

private array $decoratedServices = [];

/**
* @psalm-var array<string, object>
*/
private array $serviceProxy = [];

public function __construct(
Expand All @@ -40,6 +44,9 @@ public function __construct(
$this->proxyManager = new ProxyManager($this->config->getProxyCachePath());
}

/**
* @psalm-param array<string, mixed> $decoratedServices
*/
public function withDecoratedServices(array $decoratedServices): self
{
$new = clone $this;
Expand Down Expand Up @@ -108,10 +115,12 @@ private function getServiceProxy(string $service, object $instance): ?object
}

if ($this->config->hasDecoratedServiceCallableConfig($service)) {
/** @psalm-suppress MixedArgument */
return $this->getServiceProxyFromCallable($this->config->getDecoratedServiceConfig($service), $instance);
}

if ($this->config->hasDecoratedServiceArrayConfigWithStringKeys($service)) {
/** @psalm-suppress MixedArgument */
return $this->getCommonMethodProxy(
interface_exists($service) || class_exists($service) ? $service : $instance::class,
$instance,
Expand All @@ -120,6 +129,7 @@ interface_exists($service) || class_exists($service) ? $service : $instance::cla
}

if ($this->config->hasDecoratedServiceArrayConfig($service)) {
/** @psalm-suppress MixedArgument */
return $this->getServiceProxyFromArray($instance, $this->config->getDecoratedServiceConfig($service));
}

Expand All @@ -130,6 +140,9 @@ interface_exists($service) || class_exists($service) ? $service : $instance::cla
return null;
}

/**
* @psalm-param callable(ContainerInterface, object):(object|null) $callback
*/
private function getServiceProxyFromCallable(callable $callback, object $instance): ?object
{
return $callback($this, $instance);
Expand All @@ -138,7 +151,7 @@ private function getServiceProxyFromCallable(callable $callback, object $instanc
/**
* @psalm-param class-string $service
*/
private function getCommonMethodProxy(string $service, object $instance, array $callbacks): ?object
private function getCommonMethodProxy(string $service, object $instance, array $callbacks): ObjectProxy
{
$methods = [];
foreach ($callbacks as $method => $callback) {
Expand All @@ -163,10 +176,11 @@ private function getServiceProxyFromArray(object $instance, array $params): ?obj
try {
$params[$index] = $this->get($param);
} catch (Exception) {
//leave as is
// leave as is
}
}
}
/** @psalm-suppress MixedMethodCall */
return new $proxyClass($instance, ...$params);
} catch (Exception) {
return null;
Expand Down
4 changes: 4 additions & 0 deletions src/Collector/ContainerProxyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\EventDispatcher\EventDispatcherInterface;

use function in_array;
use function is_array;
use function is_callable;

final class ContainerProxyConfig
Expand Down Expand Up @@ -64,6 +65,9 @@ public function withCollector(ServiceCollector $collector): self
return $config;
}

/**
* @psalm-param array<string, mixed> $decoratedServices
*/
public function withDecoratedServices(array $decoratedServices): self
{
$config = clone $this;
Expand Down

0 comments on commit 71645c4

Please sign in to comment.