Test Failed
Push — gacela-get-root-dir ( 210465...884323 )
by Chema
02:53
created

Gacela::rootDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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