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
|
|
|
* @param null|Closure(GacelaConfig):void $configFn |
38
|
|
|
*/ |
39
|
|
|
public static function bootstrap(string $appRootDir, Closure $configFn = null): void |
40
|
|
|
{ |
41
|
|
|
self::$appRootDir = $appRootDir; |
42
|
|
|
self::$mainContainer = null; |
43
|
|
|
|
44
|
|
|
$setup = self::processConfigFnIntoSetup($configFn); |
45
|
|
|
|
46
|
|
|
if ($setup->shouldResetInMemoryCache()) { |
47
|
|
|
self::resetCache(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$config = Config::createWithSetup($setup); |
51
|
|
|
$config->setAppRootDir($appRootDir) |
52
|
|
|
->init(); |
53
|
|
|
|
54
|
|
|
self::runPlugins($config); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @template T |
59
|
|
|
* |
60
|
|
|
* @param class-string<T> $className |
|
|
|
|
61
|
|
|
* |
62
|
|
|
* @return T|null |
63
|
|
|
*/ |
64
|
|
|
public static function get(string $className): mixed |
65
|
|
|
{ |
66
|
|
|
return Locator::getSingleton($className, self::$mainContainer); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the application root dir set when bootstrapping gacela |
71
|
|
|
*/ |
72
|
|
|
public static function rootDir(): string |
73
|
|
|
{ |
74
|
|
|
if (self::$appRootDir === null) { |
75
|
|
|
throw new GacelaNotBootstrappedException(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return self::$appRootDir; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add an anonymous class as 'Config', 'Factory' or 'DependencyProvider' as a global resource |
83
|
|
|
* bound to the context that it is passed as first argument. |
84
|
|
|
* It can be the string-key (from a non-class/file context) or the class/object itself. |
85
|
|
|
*/ |
86
|
|
|
public static function addGlobal(object $resolvedClass, object|string $context = ''): void |
87
|
|
|
{ |
88
|
|
|
if ($context === '') { |
89
|
|
|
$context = basename(debug_backtrace()[0]['file'] ?? __FILE__, '.php'); |
90
|
|
|
} |
91
|
|
|
AnonymousGlobal::addGlobal($context, $resolvedClass); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function overrideExistingResolvedClass(string $className, object $resolvedClass): void |
95
|
|
|
{ |
96
|
|
|
AnonymousGlobal::overrideExistingResolvedClass($className, $resolvedClass); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param null|Closure(GacelaConfig):void $configFn |
101
|
|
|
*/ |
102
|
|
|
private static function processConfigFnIntoSetup(Closure $configFn = null): SetupGacelaInterface |
103
|
|
|
{ |
104
|
|
|
if ($configFn instanceof Closure) { |
105
|
|
|
return SetupGacela::fromCallable($configFn); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$gacelaFilePath = sprintf( |
109
|
|
|
'%s%s%s', |
110
|
|
|
self::rootDir(), |
111
|
|
|
DIRECTORY_SEPARATOR, |
112
|
|
|
self::GACELA_PHP_FILENAME, |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
if (is_file($gacelaFilePath)) { |
116
|
|
|
return SetupGacela::fromFile($gacelaFilePath); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return new SetupGacela(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private static function resetCache(): void |
123
|
|
|
{ |
124
|
|
|
AnonymousGlobal::resetCache(); |
125
|
|
|
AbstractFacade::resetCache(); |
126
|
|
|
AbstractFactory::resetCache(); |
127
|
|
|
AbstractClassResolver::resetCache(); |
128
|
|
|
InMemoryCache::resetCache(); |
129
|
|
|
GacelaFileCache::resetCache(); |
130
|
|
|
DocBlockResolverCache::resetCache(); |
131
|
|
|
ClassResolverCache::resetCache(); |
132
|
|
|
ConfigFactory::resetCache(); |
133
|
|
|
Config::resetInstance(); |
134
|
|
|
Locator::resetInstance(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private static function runPlugins(Config $config): void |
138
|
|
|
{ |
139
|
|
|
self::$mainContainer = Container::withConfig($config); |
140
|
|
|
|
141
|
|
|
$plugins = $config->getSetupGacela()->getPlugins(); |
142
|
|
|
|
143
|
|
|
foreach ($plugins as $plugin) { |
144
|
|
|
/** @var callable $current */ |
145
|
|
|
$current = is_string($plugin) |
146
|
|
|
? self::$mainContainer->get($plugin) |
147
|
|
|
: $plugin; |
148
|
|
|
|
149
|
|
|
self::$mainContainer->resolve($current); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|