-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPluginBundle.php
129 lines (115 loc) · 4.38 KB
/
PluginBundle.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace Flasher\Symfony\Support;
use Flasher\Prime\Plugin\PluginInterface;
use Flasher\Symfony\FlasherSymfonyBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
/**
* PluginBundle - Base class for PHPFlasher plugin bundles.
*
* This abstract class provides common functionality for all PHPFlasher plugin bundles.
* It extends Symfony's AbstractBundle to integrate with the kernel bundle system,
* while implementing PluginBundleInterface to provide PHPFlasher-specific functionality.
*
* Design patterns:
* - Template Method: Defines skeleton of common bundle operations
* - Factory Method: Creates plugin instances
* - Bridge: Connects Symfony bundle system with PHPFlasher plugin system
* - Extension: Extends AbstractBundle with PHPFlasher-specific functionality
*/
abstract class PluginBundle extends AbstractBundle implements PluginBundleInterface
{
/**
* Creates an instance of the plugin.
*
* This factory method must be implemented by child classes to instantiate
* the specific plugin that the bundle integrates.
*
* @return PluginInterface The plugin instance
*/
abstract public function createPlugin(): PluginInterface;
/**
* Loads bundle configuration into the Symfony container.
*
* This method registers the plugin's factory service in the container and
* configures it appropriately. The core FlasherSymfonyBundle is exempt from
* this process since it has special handling.
*
* @param array<string, mixed> $config The processed bundle configuration
* @param ContainerConfigurator $container The container configurator
* @param ContainerBuilder $builder The container builder
*/
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
if ($this instanceof FlasherSymfonyBundle) {
return;
}
$plugin = $this->createPlugin();
$identifier = $plugin->getServiceId();
$container->services()
->set($identifier, $plugin->getFactory())
->parent('flasher.notification_factory')
->tag('flasher.factory', ['alias' => $plugin->getAlias()])
->public()
;
foreach ((array) $plugin->getServiceAliases() as $alias) {
$builder->setAlias($alias, $identifier);
}
}
/**
* Prepends default plugin configuration for Flasher.
*
* This method adds the plugin's scripts, styles, and options to the Flasher
* configuration before the container is compiled.
*
* @param ContainerConfigurator $container The container configurator
* @param ContainerBuilder $builder The container builder
*/
public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
{
if ($this instanceof FlasherSymfonyBundle) {
return;
}
$plugin = $this->createPlugin();
$builder->prependExtensionConfig('flasher', [
'plugins' => [
$plugin->getAlias() => [
'scripts' => (array) $plugin->getScripts(),
'styles' => (array) $plugin->getStyles(),
'options' => $plugin->getOptions(),
],
],
]);
}
/**
* Gets the path to the plugin's configuration file.
*
* Returns the absolute path to the plugin's configuration file
* based on the bundle's path.
*
* @return string Absolute path to the configuration file
*/
public function getConfigurationFile(): string
{
return rtrim($this->getPath(), '/').'/Resources/config/config.yaml';
}
/**
* Gets the bundle's directory path.
*
* Uses reflection to determine the location of the bundle class file,
* then returns its directory.
*
* @return string The bundle directory path
*/
public function getPath(): string
{
if (!isset($this->path)) {
$reflected = new \ReflectionObject($this);
// assume the modern directory structure by default
$this->path = \dirname($reflected->getFileName() ?: '');
}
return $this->path;
}
}