Passed
Branch main (9d44a0)
by Chema
02:57
created

Gacela::bootstrap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
nc 2
nop 2
dl 0
loc 22
ccs 17
cts 17
cp 1
crap 2
rs 9.7333
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
use Gacela\Framework\Plugin\PluginInterface;
22
use RuntimeException;
23
24
final class Gacela
25
{
26
    private static ?Container $mainContainer = null;
27
28
    /**
29
     * Define the entry point of Gacela.
30
     *
31
     * @param null|Closure(GacelaConfig):void $configFn
32
     */
33 85
    public static function bootstrap(string $appRootDir, Closure $configFn = null): void
34
    {
35 85
        $setup = self::processConfigFnIntoSetup($appRootDir, $configFn);
36
37 85
        if ($setup->shouldResetInMemoryCache()) {
38 45
            AbstractFacade::resetCache();
39 45
            AnonymousGlobal::resetCache();
40 45
            AbstractFactory::resetCache();
41 45
            GacelaFileCache::resetCache();
42 45
            DocBlockResolverCache::resetCache();
43 45
            ClassResolverCache::resetCache();
44 45
            InMemoryCache::resetCache();
45 45
            AbstractClassResolver::resetCache();
46 45
            ConfigFactory::resetCache();
47 45
            Config::resetInstance();
48
        }
49
50 85
        $config = Config::createWithSetup($setup);
51 85
        $config->setAppRootDir($appRootDir)
52 85
            ->init();
53
54 85
        self::runPlugins($config);
55
    }
56
57
    /**
58
     * @template T
59
     *
60
     * @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...
61
     *
62
     * @return T|null
63
     */
64 3
    public static function get(string $className): mixed
65
    {
66 3
        if (self::$mainContainer === null) {
67
            throw new RuntimeException('You forgot to call first `Gacela::bootstrap()`');
68
        }
69
70 3
        return Locator::getSingleton($className, self::$mainContainer);
71
    }
72
73
    /**
74
     * @param null|Closure(GacelaConfig):void $configFn
75
     */
76 85
    private static function processConfigFnIntoSetup(string $appRootDir, Closure $configFn = null): SetupGacelaInterface
77
    {
78 85
        if ($configFn !== null) {
79 59
            return SetupGacela::fromCallable($configFn);
80
        }
81
82 26
        $gacelaFilePath = $appRootDir . '/gacela.php';
83
84 26
        if (is_file($gacelaFilePath)) {
85
            return SetupGacela::fromFile($gacelaFilePath);
86
        }
87
88 26
        return new SetupGacela();
89
    }
90
91 85
    private static function runPlugins(Config $config): void
92
    {
93 85
        $plugins = $config->getSetupGacela()->getPlugins();
94
95 85
        if ($plugins === []) {
96 82
            return;
97
        }
98
99 3
        self::$mainContainer = Container::withConfig($config);
100
101 3
        foreach ($plugins as $pluginName) {
102
            /** @var PluginInterface $plugin */
103 3
            $plugin = self::$mainContainer->get($pluginName);
104 3
            $plugin->run();
105
        }
106
    }
107
}
108