Skip to content

Commit

Permalink
Pass instance of proxied service to the proxy function (#258)
Browse files Browse the repository at this point in the history
* Clean providers

* Pass instance of proxied service to the proxy function
  • Loading branch information
xepozz authored Jun 23, 2024
1 parent cd4fe68 commit c3fd44d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 43 deletions.
6 changes: 3 additions & 3 deletions config/di-providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

use Yiisoft\Yii\Debug\ProxyServiceProvider;
use Yiisoft\Yii\Debug\DebugServiceProvider;

if (!(bool)($params['yiisoft/yii-debug']['enabled'] ?? false)) {
if (!(bool) ($params['yiisoft/yii-debug']['enabled'] ?? false)) {
return [];
}

return [
'yiisoft/yii-debug/Debugger' => ProxyServiceProvider::class,
'yiisoft/yii-debug/' . DebugServiceProvider::class => DebugServiceProvider::class,
];
2 changes: 2 additions & 0 deletions config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
if (isset($params['historySize'])) {
$fileStorage->setHistorySize((int) $params['historySize']);
}

return $fileStorage;
},
];
Expand All @@ -47,6 +48,7 @@
$trackedServices = (array) ($params['trackedServices'] ?? []);
$path = $container->get(Aliases::class)->get('@runtime/cache/container-proxy');
$logLevel = $params['logLevel'] ?? ContainerInterfaceProxy::LOG_NOTHING;

return new ContainerProxyConfig(
$debuggerEnabled,
$trackedServices,
Expand Down
6 changes: 3 additions & 3 deletions src/Collector/ContainerInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function getServiceProxy(string $service, object $instance): ?object
}

if ($this->config->hasDecoratedServiceCallableConfig($service)) {
return $this->getServiceProxyFromCallable($this->config->getDecoratedServiceConfig($service));
return $this->getServiceProxyFromCallable($this->config->getDecoratedServiceConfig($service), $instance);
}

if ($this->config->hasDecoratedServiceArrayConfigWithStringKeys($service)) {
Expand All @@ -126,9 +126,9 @@ interface_exists($service) || class_exists($service) ? $service : $instance::cla
return null;
}

private function getServiceProxyFromCallable(callable $callback): ?object
private function getServiceProxyFromCallable(callable $callback, object $instance): ?object
{
return $callback($this);
return $callback($this, $instance);
}

/**
Expand Down
17 changes: 8 additions & 9 deletions src/DebugServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@
namespace Yiisoft\Yii\Debug;

use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Yiisoft\Di\ServiceProviderInterface;
use Yiisoft\Yii\Debug\Collector\EventCollector;
use Yiisoft\Yii\Debug\Collector\LogCollector;
use Yiisoft\Yii\Debug\Collector\EventDispatcherInterfaceProxy;
use Yiisoft\Yii\Debug\Collector\LoggerInterfaceProxy;
use Yiisoft\Yii\Debug\Collector\ContainerInterfaceProxy;
use Yiisoft\Yii\Debug\Collector\ContainerProxyConfig;

final class DebugServiceProvider implements ServiceProviderInterface
{
public function getDefinitions(): array
{
return [];
return [
ContainerInterface::class => static fn (ContainerInterface $container) => new ContainerInterfaceProxy(
$container,
$container->get(ContainerProxyConfig::class),
),
];
}

public function getExtensions(): array
{
return [
LoggerInterface::class => static fn (ContainerInterface $container, LoggerInterface $logger) => new LoggerInterfaceProxy($logger, $container->get(LogCollector::class)),
EventDispatcherInterface::class => static fn (ContainerInterface $container, EventDispatcherInterface $dispatcher) => new EventDispatcherInterfaceProxy($dispatcher, $container->get(EventCollector::class)),
];
}
}
28 changes: 0 additions & 28 deletions src/ProxyServiceProvider.php

This file was deleted.

0 comments on commit c3fd44d

Please sign in to comment.