lapaz/aura-di-ext

Aura.Di Vocabulary Extension

0.2.0 2017-06-08 14:22 UTC

This package is auto-updated.

Last update: 2024-12-05 19:45:13 UTC


README

Build Status

  • Optionally ->modifiedBy() and ->modifiedByScript() enabled after $di->lazyNew() and $di->newFactory().
  • New method ->newLocator() to create pure callable object that returns the service.
  • Optional parameter $params = [] added to ->lazyRequire() and lazyInclude().

(newLocator() is simply non lazy version of lazyGet().)

Unlike ContainerConfig::modify(), every modification is called on demand at the 1st time of ->get().

Before

$di->set('routerContainer', $di->lazy(function () use ($di) {
    $routerContainer = $di->newInstance(\Aura\Router\RouterContainer::class, [], [
        'setLoggerFactory' => function () use ($di) {
            return $di->get('logger');
        },
        // Don't use ->lazyGet() because the returned lazy object would be evaluated before injection.
    ]);

    $map = $routerContainer->getMap();
    $map->get('index', '/');
    // ...

    return $routerContainer;
));

After

$dix = ContainerExtension::createFrom($di);

$di->set('routerContainer', $dix->lazyNew(\Aura\Router\RouterContainer::class, [], [
    'setLoggerFactory' => $dix->newLocator('logger'),
])->modifiedBy(function ($routerContainer) {
    $map = $routerContainer->getMap();
    $map->get('index', '/');
    // ...
));

Require/Include

$dix = ContainerExtension::createFrom($di);

$di->params[\Aura\Dispatcher\Dispatcher::class]['objects'] = $dix->lazyRequire(__DIR__ . '/objects.php', [
    'di' => $di,
    // 'anotherConfig' => ...
]);

You can use $di in objects.php to return configured lazy instances.