Completed
Pull Request — 1.x (#336)
by Akihito
05:45
created

Injector::factory()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.584
cc 4
nc 6
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package;
6
7
use BEAR\AppMeta\Meta;
8
use BEAR\Package\Context\Provider\ProdCacheProvider;
9
use BEAR\Sunday\Extension\Application\AppInterface;
10
use Ray\Compiler\CachedFactory;
11
use Ray\Di\AbstractModule;
12
use Ray\Di\InjectorInterface;
13
use function sprintf;
14
15
final class Injector
16
{
17
    private function __construct()
18
    {
19
    }
20
21
    public static function getInstance(string $appName, string $context, string $appDir, string $cacheNamespace = '') : InjectorInterface
22
    {
23
        $injectorId = $appName . $context . $cacheNamespace;
24
        $meta = new Meta($appName, $context, $appDir);
25
        $cache = (new ProdCacheProvider($meta, $injectorId))->get();
26
        $module = function () use ($meta, $context, $cacheNamespace) : AbstractModule {
27
            return (new Module)($meta, $context, $cacheNamespace);
28
        };
29
        $scriptDir = sprintf('%s/di', $meta->tmpDir);
30
31
        return CachedFactory::getInstance($module, $scriptDir, $cache, [AppInterface::class]);
32
    }
33
}
34