1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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 Sylius\Bundle\CoreBundle\Application; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Container; |
15
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @mixin \Symfony\Component\HttpKernel\Bundle\Bundle |
19
|
|
|
* @see \Symfony\Component\HttpKernel\Bundle\Bundle |
20
|
|
|
* |
21
|
|
|
* Provides a common logic for Sylius Plugins. |
22
|
|
|
* Each of a plugins should be created with Plugin instead of Bundle suffix for the root class. |
23
|
|
|
* |
24
|
|
|
* @author Łukasz Chruściel <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
trait SyliusPluginTrait |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ExtensionInterface|bool |
30
|
|
|
*/ |
31
|
|
|
private $containerExtension; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns the plugin's container extension. |
35
|
|
|
* |
36
|
|
|
* @return ExtensionInterface|null The container extension |
37
|
|
|
* |
38
|
|
|
* @throws \LogicException |
39
|
|
|
*/ |
40
|
|
|
public function getContainerExtension() |
41
|
|
|
{ |
42
|
|
|
if (null === $this->containerExtension) { |
43
|
|
|
$extension = $this->createContainerExtension(); |
44
|
|
|
|
45
|
|
|
if (null !== $extension) { |
46
|
|
|
if (!$extension instanceof ExtensionInterface) { |
47
|
|
|
throw new \LogicException(sprintf('Extension %s must implement %s.', get_class($extension), ExtensionInterface::class)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// check naming convention for Sylius Plugins |
51
|
|
|
$basename = preg_replace('/Plugin$/', '', $this->getName()); |
52
|
|
|
$expectedAlias = Container::underscore($basename); |
53
|
|
|
|
54
|
|
|
if ($expectedAlias != $extension->getAlias()) { |
55
|
|
|
throw new \LogicException(sprintf( |
56
|
|
|
'Users will expect the alias of the default extension of a plugin to be the underscored version of the plugin name ("%s"). You can override "Bundle::getContainerExtension()" if you want to use "%s" or another alias.', |
57
|
|
|
$expectedAlias, $extension->getAlias() |
58
|
|
|
)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->containerExtension = $extension; |
62
|
|
|
} else { |
63
|
|
|
$this->containerExtension = false; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->containerExtension) { |
68
|
|
|
return $this->containerExtension; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Creates the bundle's container extension. |
74
|
|
|
* |
75
|
|
|
* @return ExtensionInterface|null |
76
|
|
|
*/ |
77
|
|
|
abstract protected function createContainerExtension(); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the bundle name (the class short name). |
81
|
|
|
* |
82
|
|
|
* @return string The Bundle name |
83
|
|
|
*/ |
84
|
|
|
abstract protected function getName(); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets the Bundle namespace. |
88
|
|
|
* |
89
|
|
|
* @return string The Bundle namespace |
90
|
|
|
*/ |
91
|
|
|
abstract protected function getNamespace(); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns the plugin's container extension class. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected function getContainerExtensionClass() |
99
|
|
|
{ |
100
|
|
|
$basename = preg_replace('/Plugin$/', '', $this->getName()); |
101
|
|
|
|
102
|
|
|
return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension'; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|