Passed
Push — bugfix/support-windows ( 0e99e7...b5ecf8 )
by Jesús
06:33 queued 02:55
created

Gacela   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 97.67%

Importance

Changes 11
Bugs 0 Features 0
Metric Value
wmc 12
eloc 40
c 11
b 0
f 0
dl 0
loc 99
ccs 42
cts 43
cp 0.9767
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A processConfigFnIntoSetup() 0 13 3
A resetCache() 0 13 1
A get() 0 3 1
A bootstrap() 0 16 2
A runPlugins() 0 13 3
A rootDir() 0 6 2
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 const GACELA_PHP_FILENAME = 'gacela.php';
28
29
    private static ?Container $mainContainer = null;
30
    private static ?string $appRootDir = null;
31
32
    /**
33
     * Define the entry point of Gacela.
34
     *
35
     * @param null|Closure(GacelaConfig):void $configFn
36
     */
37 94
    public static function bootstrap(string $appRootDir, Closure $configFn = null): void
38
    {
39 94
        self::$appRootDir = $appRootDir;
40 94
        self::$mainContainer = null;
41
42 94
        $setup = self::processConfigFnIntoSetup($configFn);
43
44 94
        if ($setup->shouldResetInMemoryCache()) {
45 47
            self::resetCache();
46
        }
47
48 94
        $config = Config::createWithSetup($setup);
49 94
        $config->setAppRootDir($appRootDir)
50 94
            ->init();
51
52 94
        self::runPlugins($config);
53
    }
54
55
    /**
56
     * @template T
57
     *
58
     * @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...
59
     *
60
     * @return T|null
61
     */
62 6
    public static function get(string $className): mixed
63
    {
64 6
        return Locator::getSingleton($className, self::$mainContainer);
65
    }
66
67
    /**
68
     * Get the application root dir set when bootstrapping gacela
69
     */
70 33
    public static function rootDir(): string
71
    {
72 33
        if (self::$appRootDir === null) {
73 1
            throw new GacelaNotBootstrappedException();
74
        }
75 32
        return self::$appRootDir;
76
    }
77
78
    /**
79
     * @param null|Closure(GacelaConfig):void $configFn
80
     */
81 94
    private static function processConfigFnIntoSetup(Closure $configFn = null): SetupGacelaInterface
82
    {
83 94
        if ($configFn !== null) {
84 63
            return SetupGacela::fromCallable($configFn);
85
        }
86
87 31
        $gacelaFilePath = sprintf('%s%s%s', self::rootDir(), DIRECTORY_SEPARATOR, self::GACELA_PHP_FILENAME);
88
89 31
        if (is_file($gacelaFilePath)) {
90
            return SetupGacela::fromFile($gacelaFilePath);
91
        }
92
93 31
        return new SetupGacela();
94
    }
95
96 47
    private static function resetCache(): void
97
    {
98 47
        AbstractFacade::resetCache();
99 47
        AnonymousGlobal::resetCache();
100 47
        AbstractFactory::resetCache();
101 47
        GacelaFileCache::resetCache();
102 47
        DocBlockResolverCache::resetCache();
103 47
        ClassResolverCache::resetCache();
104 47
        InMemoryCache::resetCache();
105 47
        AbstractClassResolver::resetCache();
106 47
        ConfigFactory::resetCache();
107 47
        Config::resetInstance();
108 47
        Locator::resetInstance();
109
    }
110
111 94
    private static function runPlugins(Config $config): void
112
    {
113 94
        self::$mainContainer = Container::withConfig($config);
114
115 94
        $plugins = $config->getSetupGacela()->getPlugins();
116
117 94
        foreach ($plugins as $plugin) {
118
            /** @var callable $current */
119 5
            $current = is_string($plugin)
120 4
                ? self::$mainContainer->get($plugin)
121 1
                : $plugin;
122
123 5
            self::$mainContainer->resolve($current);
124
        }
125
    }
126
}
127