Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add container proxy #116

Merged
merged 44 commits into from
Mar 5, 2020
Merged

Add container proxy #116

merged 44 commits into from
Mar 5, 2020

Conversation

yiiliveext
Copy link
Contributor

@yiiliveext yiiliveext commented Mar 1, 2020

Q A
Is bugfix? ✔️
New feature? ✔️
Breaks BC? ✔️
Tests pass? ✔️
Fixed issues #107 #110

Don't merge! This is a demo PR.

Old container syntax

$container = new Container($definitions);

New container syntax

$container = Container::getBuilder()->registerDefinitions($definitions)->build();

index.php container&debugger registration expample

$containerBuilder = Container::getBuilder()
    ->registerDefinitions(require Builder::path('web'));

/**
 * @var array $params The variable is available after requiring config files.
 */
$debugEnabled = (bool)($params['debugger.enabled'] ?? false) && class_exists(Debugger::class);
if ($debugEnabled) {
   $debuggerServiceProvider = new DebugServiceProvider();
   $containerBuilder->registerServiceProviders([$debuggerServiceProvider]);
}
$container = $containerBuilder->build();

Container proxy config

// /config/web.php
CommonServiceCollectorInterface::class => CommonServiceCollector::class,
ContainerProxyInterface::class => static function (ContainerInterface $container) use ($params) {
        $collector = $container->get(CommonServiceCollectorInterface::class);
        $dispatcher = $container->get(EventDispatcherInterface::class);
        $debuggerEnabled = (bool)($params['debugger.enabled'] ?? false);
        $trackedServices = (array)($params['debugger.trackedServices'] ?? []);
        $decoratedServices = (array)($params['container.decorators'] ?? []);
        $runtime = $container->get(Aliases::class)->get('@runtime');
        $path = "$runtime/cache/container-proxy";
        if (!is_dir($path)) {
            mkdir($path);
        }
        $logLevel = ContainerProxy::LOG_ARGUMENTS | ContainerProxy::LOG_RESULT | ContainerProxy::LOG_ERROR;
        return new ContainerProxy(
            $debuggerEnabled,
            array_merge($trackedServices, $decoratedServices),
            $container,
            $dispatcher,
            $collector,
            $path,
            $logLevel
        );
},
// /config/params.php
'debugger.enabled' => true,
'debugger.trackedServices' => [
    \Cycle\ORM\FactoryInterface::class,
    \Yiisoft\Mailer\MailerInterface::class,
    \Psr\Http\Message\ServerRequestFactoryInterface::class,
    \Yiisoft\Yii\Web\Session\SessionInterface::class,
    \Yiisoft\Router\RouteCollectorInterface::class,
],
'debugger.collectors' => [
    \Yiisoft\Di\CommonServiceCollector::class
],
'container.decorators' => [
    // custom method decorator
    \Yiisoft\Router\UrlGeneratorInterface::class => [
        'generate' =>function ($result, $name, $params = []) {
            if ($name === 'site/contact') {
                return $result .  '/me';
            }
            return $result;
        }
    ],
],

Also, you can remove debugger enable from the index.php, and enable it from config

'debugger.trackedServices' => [
    //array-configured interface decorator/proxy, the first param is a proxy/decorator class, other params are decorator's constructor parameters
    LoggerInterface::class => [LoggerInterfaceProxy::class, LogCollectorInterface::class],
    //callback-cofigured interface decorator/proxy
    EventDispatcherInterface::class => function (ContainerInterface $container) {
        $dispatcher = $container->get(EventDispatcherInterface::class);
        $compositeDispatcher = new CompositeDispatcher();
        $compositeDispatcher->attach($container->get(DebugStartupDispatcher::class));
        $compositeDispatcher->attach($container->get(DebugEventDispatcher::class));
        $compositeDispatcher->attach($dispatcher);
        $compositeDispatcher->attach($container->get(DebugShutdownDispatcher::class));

        return new EventDispatcherInterfaceProxy($compositeDispatcher, $container->get(EventCollectorInterface::class));
    },
],

@samdark samdark requested a review from a team March 2, 2020 12:18
@samdark samdark added the status:code review The pull request needs review. label Mar 2, 2020
composer.json Outdated Show resolved Hide resolved
src/CommonMethodProxy.php Outdated Show resolved Hide resolved
composer.json Outdated Show resolved Hide resolved
src/CompositeContainer.php Outdated Show resolved Hide resolved
src/CompositeContainer.php Outdated Show resolved Hide resolved
src/Container.php Outdated Show resolved Hide resolved
src/CompositeContainer.php Outdated Show resolved Hide resolved
src/CompositeContainer.php Outdated Show resolved Hide resolved
src/CompositeContainer.php Outdated Show resolved Hide resolved
src/Container.php Outdated Show resolved Hide resolved
@yiiliveext yiiliveext requested a review from samdark March 3, 2020 08:57
src/CompositeContainer.php Outdated Show resolved Hide resolved
src/Container.php Outdated Show resolved Hide resolved
src/Container.php Outdated Show resolved Hide resolved
src/Container.php Outdated Show resolved Hide resolved
src/ContainerBuilder.php Outdated Show resolved Hide resolved
src/ContainerProxyInterface.php Outdated Show resolved Hide resolved
@samdark
Copy link
Member

samdark commented Mar 3, 2020

#109 could be merged into it to validate issue is fixed.

src/CompositeContainer.php Outdated Show resolved Hide resolved
src/Container.php Outdated Show resolved Hide resolved
@samdark samdark merged commit 086e3b3 into yiisoft:master Mar 5, 2020
@yiiliveext yiiliveext deleted the di-proxy branch March 5, 2020 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:code review The pull request needs review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants