|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the LaravelYaml package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Théo FIDRY <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Fidry\LaravelYaml\Provider; |
|
13
|
|
|
|
|
14
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder; |
|
15
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Extension\ExtensionInterface; |
|
16
|
|
|
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Théo FIDRY <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class AbstractExtensionProvider extends IlluminateServiceProvider implements ProviderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ExtensionInterface[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $extensions = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
final public function register() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->loadExtensions(); |
|
34
|
|
|
|
|
35
|
|
|
$container = new ContainerBuilder(); |
|
36
|
|
|
foreach ($this->extensions as $extension) { |
|
37
|
|
|
$extension->load($container); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$container->build($this->app); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Loads all the extensions registered by the user. |
|
45
|
|
|
*/ |
|
46
|
|
|
private function loadExtensions() |
|
47
|
|
|
{ |
|
48
|
|
|
$extensionsClassNames = array_flip($this->getExtensions()); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($extensionsClassNames as $extensionClassName => $null) { |
|
51
|
|
|
if (false === class_exists($extensionClassName)) { |
|
52
|
|
|
throw new \InvalidArgumentException( |
|
53
|
|
|
sprintf( |
|
54
|
|
|
'Unable to load extension "%s": extension not found', |
|
55
|
|
|
$extensionClassName |
|
56
|
|
|
) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$extension = new $extensionClassName(); |
|
61
|
|
|
if (false === $extension instanceof ExtensionInterface) { |
|
62
|
|
|
throw new \LogicException( |
|
63
|
|
|
sprintf( |
|
64
|
|
|
'Extension "%s" must implement the interface "%s"', |
|
65
|
|
|
get_class($extension), |
|
66
|
|
|
ExtensionInterface::class |
|
67
|
|
|
) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->extensions[] = $extension; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|