Passed
Push — install-infection ( aaa610...d2f8ae )
by Chema
03:53
created

Gacela::bootstrap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 2
eloc 18
nc 2
nop 2
dl 0
loc 25
ccs 19
cts 19
cp 1
crap 2
rs 9.6666
c 6
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework;
6
7
use Closure;
8
use Gacela\Framework\Bootstrap\GacelaConfig;
9
use Gacela\Framework\Bootstrap\SetupGacela;
10
use Gacela\Framework\Bootstrap\SetupGacelaInterface;
11
use Gacela\Framework\ClassResolver\AbstractClassResolver;
12
use Gacela\Framework\ClassResolver\Cache\GacelaFileCache;
13
use Gacela\Framework\ClassResolver\Cache\InMemoryCache;
14
use Gacela\Framework\ClassResolver\ClassResolverCache;
15
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal;
16
use Gacela\Framework\Config\Config;
17
use Gacela\Framework\Config\ConfigFactory;
18
use Gacela\Framework\Container\Container;
19
use Gacela\Framework\Container\Locator;
20
use Gacela\Framework\DocBlockResolver\DocBlockResolverCache;
21
22
final class Gacela
23
{
24
    private static ?Container $mainContainer = null;
25
26
    /**
27
     * Define the entry point of Gacela.
28
     *
29
     * @param null|Closure(GacelaConfig):void $configFn
30
     */
31 86
    public static function bootstrap(string $appRootDir, Closure $configFn = null): void
32
    {
33 86
        self::$mainContainer = null;
34
35 86
        $setup = self::processConfigFnIntoSetup($appRootDir, $configFn);
36
37 86
        if ($setup->shouldResetInMemoryCache()) {
38 46
            AbstractFacade::resetCache();
39 46
            AnonymousGlobal::resetCache();
40 46
            AbstractFactory::resetCache();
41 46
            GacelaFileCache::resetCache();
42 46
            DocBlockResolverCache::resetCache();
43 46
            ClassResolverCache::resetCache();
44 46
            InMemoryCache::resetCache();
45 46
            AbstractClassResolver::resetCache();
46 46
            ConfigFactory::resetCache();
47 46
            Config::resetInstance();
48 46
            Locator::resetInstance();
49
        }
50
51 86
        $config = Config::createWithSetup($setup);
52 86
        $config->setAppRootDir($appRootDir)
53 86
            ->init();
54
55 86
        self::runPlugins($config);
56
    }
57
58
    /**
59
     * @template T
60
     *
61
     * @param class-string<T> $className
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
62
     *
63
     * @return T|null
64
     */
65 4
    public static function get(string $className): mixed
66
    {
67 4
        return Locator::getSingleton($className, self::$mainContainer);
68
    }
69
70
    /**
71
     * @param null|Closure(GacelaConfig):void $configFn
72
     */
73 86
    private static function processConfigFnIntoSetup(string $appRootDir, Closure $configFn = null): SetupGacelaInterface
74
    {
75 86
        if ($configFn !== null) {
76 60
            return SetupGacela::fromCallable($configFn);
77
        }
78
79 26
        $gacelaFilePath = $appRootDir . '/gacela.php';
80
81 26
        if (is_file($gacelaFilePath)) {
82
            return SetupGacela::fromFile($gacelaFilePath);
83
        }
84
85 26
        return new SetupGacela();
86
    }
87
88 86
    private static function runPlugins(Config $config): void
89
    {
90 86
        self::$mainContainer = Container::withConfig($config);
91
92 86
        $plugins = $config->getSetupGacela()->getPlugins();
93
94 86
        foreach ($plugins as $pluginName) {
95
            /** @var callable $plugin */
96 3
            $plugin = self::$mainContainer->get($pluginName);
97 3
            $plugin();
98
        }
99
    }
100
}
101