Skip to content

Commit b4b29bf

Browse files
authored
[Maintenance] Add a more flexible way to load bundles (#11)
2 parents cf8ddb2 + 8ff1afc commit b4b29bf

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,24 @@ This package solves that problem by:
4141

4242
```dotenv
4343
DATABASE_URL=mysql://root@127.0.0.1/test_application_%kernel.environment%
44-
BUNDLES_TO_ENABLE="Acme\Plugin\AcmePlugin"
4544
CONFIGS_TO_IMPORT="@AcmePlugin/tests/TestApplication/config/config.yaml"
4645
ROUTES_TO_IMPORT="@AcmePlugin/config/routes.yaml"
46+
TEST_APP_BUNDLES_PATH="tests/TestApplication/config/bundles.php"
47+
# Optional: fall back to a semicolon-separated list of bundles
48+
BUNDLES_TO_ENABLE="Acme\Plugin\AcmePlugin"
4749
```
4850

4951
> 💡 The values provided above are examples and should be adjusted for your plugin.
5052

53+
1. Optionally, return conditionally enabled bundles from `tests/TestApplication/bundles.php`:
54+
55+
```php
56+
<?php
57+
58+
return [
59+
Acme\\Plugin\\AcmePlugin::class => ['all' => true],
60+
];
61+
```
5162

5263
1. If needed, place plugin-specific configuration files in the `tests/TestApplication/config` directory
5364
(e.g. `services.yaml`, `routes.yaml`) and load them by env variables.

bin/console

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ if ($_SERVER['APP_DEBUG']) {
3939
}
4040
}
4141

42-
if (isset($_ENV['BUNDLES_TO_ENABLE'])) {
43-
$_SERVER['BUNDLES_TO_ENABLE'] = $_ENV['BUNDLES_TO_ENABLE'];
44-
}
45-
4642
if (isset($_ENV['CONFIGS_TO_IMPORT'])) {
4743
$_SERVER['CONFIGS_TO_IMPORT'] = $_ENV['CONFIGS_TO_IMPORT'];
4844
}
@@ -51,6 +47,13 @@ if (isset($_ENV['ROUTES_TO_IMPORT'])) {
5147
$_SERVER['ROUTES_TO_IMPORT'] = $_ENV['ROUTES_TO_IMPORT'];
5248
}
5349

50+
if (isset($_ENV["BUNDLES_TO_ENABLE"])) {
51+
$_SERVER["BUNDLES_TO_ENABLE"] = $_ENV["BUNDLES_TO_ENABLE"];
52+
}
53+
54+
if (isset($_ENV["TEST_APP_BUNDLES_PATH"])) {
55+
$_SERVER["TEST_APP_BUNDLES_PATH"] = $_ENV["TEST_APP_BUNDLES_PATH"];
56+
}
5457

5558
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
5659
$application = new Application($kernel);

src/Kernel.php

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,26 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
8282
}
8383
}
8484
});
85-
8685
}
86+
8787
public function registerBundles(): iterable
8888
{
89+
$env = $this->getEnvironment();
90+
8991
if (!is_file($bundlesPath = $this->getBundlesPath())) {
9092
yield new FrameworkBundle();
91-
9293
return;
9394
}
9495

95-
$contents = require $bundlesPath;
96+
$contents = $this->loadMainBundles($bundlesPath);
97+
$additionalBundlesLoaded = $this->loadAdditionalBundlesFromEnv($contents);
9698

97-
if (isset($_SERVER['BUNDLES_TO_ENABLE'])) {
98-
foreach (explode(';', $_SERVER['BUNDLES_TO_ENABLE']) as $bundleClass) {
99-
if (class_exists($bundleClass)) {
100-
$contents[$bundleClass] = ['all' => true];
101-
}
102-
}
99+
if (!$additionalBundlesLoaded) {
100+
$this->loadBundlesToEnable($contents);
103101
}
104102

105103
foreach ($contents as $class => $envs) {
106-
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
104+
if ($envs[$env] ?? $envs['all'] ?? false) {
107105
yield new $class();
108106
}
109107
}
@@ -139,4 +137,49 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
139137

140138
return $collection;
141139
}
140+
141+
private function loadMainBundles(string $path): array
142+
{
143+
return require $path;
144+
}
145+
146+
private function loadAdditionalBundlesFromEnv(array &$contents): bool
147+
{
148+
if (!isset($_SERVER['TEST_APP_BUNDLES_PATH'])) {
149+
return false;
150+
}
151+
152+
$relativePath = $_SERVER['TEST_APP_BUNDLES_PATH'];
153+
$absolutePath = \dirname($this->getProjectDir(), 3) . '/' . ltrim($relativePath, '/');
154+
155+
if (!is_file($absolutePath)) {
156+
return false;
157+
}
158+
159+
$additionalBundles = require $absolutePath;
160+
if (!\is_array($additionalBundles)) {
161+
return false;
162+
}
163+
164+
foreach ($additionalBundles as $bundleClass => $envs) {
165+
if (\class_exists($bundleClass)) {
166+
$contents[$bundleClass] = $envs;
167+
}
168+
}
169+
170+
return true;
171+
}
172+
173+
private function loadBundlesToEnable(array &$contents): void
174+
{
175+
if (!isset($_SERVER['BUNDLES_TO_ENABLE'])) {
176+
return;
177+
}
178+
179+
foreach (explode(';', $_SERVER['BUNDLES_TO_ENABLE']) as $bundleClass) {
180+
if (\class_exists($bundleClass)) {
181+
$contents[$bundleClass] = ['all' => true];
182+
}
183+
}
184+
}
142185
}

0 commit comments

Comments
 (0)