Test Failed
Push — rename-plugins-to-after-plugin... ( 2b0a51 )
by Chema
02:52
created

Gacela::runPlugins()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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