Passed
Push — main ( 0c23a7...251707 )
by Chema
03:52
created

Gacela::createRecursiveIterator()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 10
c 1
b 1
f 0
dl 0
loc 21
rs 9.9332
ccs 0
cts 0
cp 0
cc 3
nc 1
nop 0
crap 12
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
use function sprintf;
25
26
final class Gacela
27
{
28
    private const GACELA_PHP_FILENAME = 'gacela.php';
29
30
    private static ?Container $mainContainer = null;
31
32
    private static ?string $appRootDir = null;
33
34
    /**
35
     * Define the entry point of Gacela.
36
     *
37 106
     * @param  null|Closure(GacelaConfig):void  $configFn
38
     */
39 106
    public static function bootstrap(string $appRootDir, Closure $configFn = null): void
40 106
    {
41
        self::$appRootDir = $appRootDir;
42 106
        self::$mainContainer = null;
43
44 106
        $setup = self::processConfigFnIntoSetup($configFn);
45 58
46
        if ($setup->shouldResetInMemoryCache()) {
47
            self::resetCache();
48 106
        }
49 106
50 106
        $config = Config::createWithSetup($setup);
51
        $config->setAppRootDir($appRootDir)
52 106
            ->init();
53
54
        self::runPlugins($config);
55
    }
56
57
    /**
58
     * @template T
59
     *
60
     * @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...
61
     *
62 7
     * @return T|null
63
     */
64 7
    public static function get(string $className): mixed
65
    {
66
        return Locator::getSingleton($className, self::$mainContainer);
67
    }
68
69
    /**
70 38
     * Get the application root dir set when bootstrapping gacela
71
     */
72 38
    public static function rootDir(): string
73 1
    {
74
        if (self::$appRootDir === null) {
75 37
            throw new GacelaNotBootstrappedException();
76
        }
77
78
        return self::$appRootDir;
79
    }
80
81 106
    /**
82
     * Add an anonymous class as 'Config', 'Factory' or 'Provider' as a global resource
83 106
     * bound to the context that it is passed as second argument.
84 74
     *
85
     * @param  object|string  $context  It can be the string-key (file path) or the class/object itself.
86
     *                               If empty then the caller's file will be use
87 32
     */
88 32
    public static function addGlobal(object $resolvedClass, object|string $context = ''): void
89 32
    {
90 32
        if (is_string($context) && is_file($context)) {
91 32
            $context = basename($context, '.php');
92 32
        } elseif ($context === '') {
93
            // Use the caller's file as context
94 32
            $context = basename(debug_backtrace()[0]['file'] ?? __FILE__, '.php');
95
        }
96
97
        AnonymousGlobal::addGlobal($context, $resolvedClass);
98 32
    }
99
100
    public static function overrideExistingResolvedClass(string $className, object $resolvedClass): void
101 58
    {
102
        AnonymousGlobal::overrideExistingResolvedClass($className, $resolvedClass);
103 58
    }
104 58
105 58
    /**
106 58
     * @param  null|Closure(GacelaConfig):void  $configFn
107 58
     */
108 58
    private static function processConfigFnIntoSetup(Closure $configFn = null): SetupGacelaInterface
109 58
    {
110 58
        if ($configFn instanceof Closure) {
111 58
            return SetupGacela::fromCallable($configFn);
112 58
        }
113 58
114
        $gacelaFilePath = sprintf(
115
            '%s%s%s',
116 106
            self::rootDir(),
117
            DIRECTORY_SEPARATOR,
118 106
            self::GACELA_PHP_FILENAME,
119
        );
120 106
121
        if (is_file($gacelaFilePath)) {
122 106
            return SetupGacela::fromFile($gacelaFilePath);
123
        }
124 5
125 4
        return new SetupGacela();
126 1
    }
127
128 5
    private static function resetCache(): void
129
    {
130
        AnonymousGlobal::resetCache();
131
        AbstractFacade::resetCache();
132
        AbstractFactory::resetCache();
133
        AbstractClassResolver::resetCache();
134
        InMemoryCache::resetCache();
135
        GacelaFileCache::resetCache();
136
        DocBlockResolverCache::resetCache();
137
        ClassResolverCache::resetCache();
138
        ConfigFactory::resetCache();
139
        Config::resetInstance();
140
        Locator::resetInstance();
141
    }
142
143
    private static function runPlugins(Config $config): void
144
    {
145
        self::$mainContainer = Container::withConfig($config);
146
147
        $plugins = $config->getSetupGacela()->getPlugins();
148
149
        foreach ($plugins as $plugin) {
150
            /** @var callable $current */
151
            $current = is_string($plugin)
152
                ? self::$mainContainer->get($plugin)
153
                : $plugin;
154
155
            self::$mainContainer->resolve($current);
156
        }
157
    }
158
}
159