Speedup Dependency Injection Configuration for Aura V2

Aura v2 added auto resolution in-order to help lazy people writing configuration manually. Even though it was introduced to help, it introduced a few issues.

So auto resolution will be disabled in the future. Some of the complains/suggestions are how to easily write the di configuration.

So introducing you FOA.DiConfig

Installation

1
composer require foa/di-config

Usage

1
2
3
vendor/bin/di-config-dump
Usage : vendor/bin/di-config-dump /real/path/to/file.php
Usage : vendor/bin/di-config-dump /real/path/to/directory

Example 1

Let’s assume you have

1
2
3
4
5
6
7
8
9
10
<?php
// src/Vendor/World.php
namespace Vendor;

class World
{
    public function __construct(Baz $baz)
    {
    }
}
1
2
3
4
5
6
7
<?php
// src/Vendor/Baz.php
namespace Vendor;

class Baz
{
}

Now you can make use of

1
vendor/bin/di-config-dump src/Vendor/World.php

will output

1
$di->params['Vendor\World']['baz'] = $di->lazyNew('Vendor\Baz');

You can also pass directory path instead of file. It will read the files and display the configuration.

Example 2

Let us look into another example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// src/Vendor/Hello.php
namespace Vendor;

class Hello
{
    public function __construct(
        \Aura\Web\Response $response,
        \Aura\Web\Request $request,
        \Aura\Router\Router $router,
        World $word
    ) {
    }
}
1
vendor/bin/di-config-dump src/Vendor/Hello.php

will output

1
2
3
4
$di->params['Vendor\Hello']['response'] = $di->lazyGet('aura/web-kernel:response');
$di->params['Vendor\Hello']['request'] = $di->lazyGet('aura/web-kernel:request');
$di->params['Vendor\Hello']['router'] = $di->lazyGet('aura/web-kernel:router');
$di->params['Vendor\Hello']['word'] = $di->lazyNew('Vendor\World');

If you look carefully the Aura\Web\Response, Aura\Web\Request and Aura\Router\Router are making use of lazyGet which gets the shared instance of the Aura.Web_Kernel .

If you are not using inside the framework just pass something as 2nd argument.

1
2
3
4
5
vendor/bin/di-config-dump src/Vendor/Hello.php h
$di->params['Vendor\Hello']['response'] = $di->lazyNew('Aura\Web\Response');
$di->params['Vendor\Hello']['request'] = $di->lazyNew('Aura\Web\Request');
$di->params['Vendor\Hello']['router'] = $di->lazyNew('Aura\Router\Router');
$di->params['Vendor\Hello']['word'] = $di->lazyNew('Vendor\World');

Please make sure all the files need to be autoloadable in-order to generate this.

If you like to improve something fork and contribute.

I have purposefully left not to make use of Aura.Cli in this library. Not sure if we need to integrate or not.

auraphp

Comments