Passed
Push — fix/solve-windows-ci ( 003700...709697 )
by Chema
07:40 queued 03:14
created

Gacela   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 97.92%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 44
c 8
b 0
f 0
dl 0
loc 104
ccs 47
cts 48
cp 0.9792
rs 10
wmc 12

6 Methods

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