-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvisionServiceProvider.php
113 lines (99 loc) · 3.17 KB
/
ProvisionServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace Upmind\ProvisionBase\Laravel;
use Illuminate\Contracts\Cache\Repository as CacheInterface;
use Illuminate\Support\ServiceProvider as BaseProvider;
use Throwable;
use Upmind\ProvisionBase\Registry\Registry;
use Upmind\ProvisionBase\Exception\RegistryError;
/**
* A laravel service provider with methods for binding Upmind Provisioning
* Categories and Providers to the Provision Registry.
*/
class ProvisionServiceProvider extends BaseProvider
{
/**
* @var string
*/
public const REGISTRY_CACHE_KEY = 'Upmind/Provision/Registry';
/**
* Binds a Category to the provision registry.
*
* @param string $identifier Unique category identifier
* @param string $class Fully namespaced category class name
*
* @return void
*/
final protected function bindCategory(string $identifier, string $class): void
{
try {
$this->provisionRegistry()->registerCategory($identifier, $class);
} catch (Throwable $e) {
throw new RegistryError(
"Provision Category Bind Error. " . get_class($e) . ": " . $e->getMessage(),
intval($e->getCode()),
$e
);
}
}
/**
* Binds a Provider to the provision registry.
*
* @param string $category Category identifier or class name
* @param string $identifier Unique provider identifier
* @param string $class Fully namespaced provider class name
*
* @return void
*/
final protected function bindProvider(string $category, string $identifier, string $class): void
{
try {
$this->provisionRegistry()->registerProvider($category, $identifier, $class);
} catch (Throwable $e) {
throw new RegistryError(
"Provision Provider Bind Error. " . get_class($e) . ": " . $e->getMessage(),
intval($e->getCode()),
$e
);
}
}
/**
* Obtain singleton instance of the provision registry.
*/
protected function provisionRegistry(): Registry
{
if (!$this->app->has(Registry::class)) {
$this->bootRegistry();
}
return $this->app->make(Registry::class);
}
protected function cache(): CacheInterface
{
return $this->app->make(CacheInterface::class);
}
public function boot()
{
if (!$this->app->has(Registry::class)) {
$this->bootRegistry();
}
}
protected function bootRegistry(): void
{
$cache = $this->cache();
// Attempt to set the Registry instance from cache
if ($cachedRegistry = $cache->get(self::REGISTRY_CACHE_KEY)) {
try {
$registry = unserialize($cachedRegistry);
} catch (Throwable $e) {
$cache->forget(self::REGISTRY_CACHE_KEY);
}
if ($registry instanceof Registry) {
Registry::setInstance($registry);
}
}
// Bind registry as singleton to container
$this->app->singleton(Registry::class, function () {
return Registry::getInstance();
});
}
}