Passed
Push — callable-plugins ( ef3f06 )
by Chema
03:43
created

Gacela::processConfigFnIntoSetup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

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