Skip to content

Commit 132cbab

Browse files
committed
BundlesLoader. Поддержка окружений при загрузке бандлов.
1 parent bd966ab commit 132cbab

File tree

6 files changed

+79
-21
lines changed

6 files changed

+79
-21
lines changed

src/Bundles/BundlesLoader.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @since 20.12.2020 Сделать все приватные консольные команды публичными.
2222
* @since 04.03.2021 Возможность загрузки бандлов несколькими провайдерами.
2323
* @since 27.04.2021 Баг-фикс: при скомпилированном контейнере не запускался метод boot бандлов.
24+
* @since 03.07.2021 Поддержка ключей окружения при загрузке бандлов.
2425
*/
2526
class BundlesLoader
2627
{
@@ -44,14 +45,21 @@ class BundlesLoader
4445
*/
4546
private static $bundlesMap = [];
4647

48+
/**
49+
* @var string $environment Окружение.
50+
*/
51+
private $environment;
52+
4753
/**
4854
* BundlesLoader constructor.
4955
*
50-
* @param ContainerBuilder $container Контейнер в стадии формирования.
51-
* @param string $configPath Путь к bundles.php (конфигурация бандлов).
56+
* @param ContainerBuilder $container Контейнер в стадии формирования.
57+
* @param string $environment Окружение.
58+
* @param string $configPath Путь к bundles.php (конфигурация бандлов).
5259
*/
5360
public function __construct(
5461
ContainerBuilder $container,
62+
string $environment,
5563
string $configPath = ''
5664
) {
5765
$configPath = $configPath ?: self::PATH_BUNDLES_CONFIG;
@@ -62,6 +70,8 @@ public function __construct(
6270
}
6371

6472
$this->container = $container;
73+
$this->environment = $environment;
74+
6575
static::$bundlesMap[static::class] = [];
6676
}
6777

@@ -77,10 +87,20 @@ public function load() : void
7787
foreach ($this->bundles as $bundleClass => $envs) {
7888
if (!class_exists($bundleClass)) {
7989
throw new InvalidArgumentException(
80-
sprintf(
81-
'Bundle class %s not exist.',
82-
$bundleClass
83-
)
90+
sprintf('Bundle class %s not exist.', $bundleClass)
91+
);
92+
}
93+
94+
if (!array_key_exists($this->environment, (array)$envs)
95+
&&
96+
!array_key_exists('all', (array)$envs)
97+
) {
98+
continue;
99+
}
100+
101+
if (!method_exists($bundleClass, 'getContainerExtension')) {
102+
throw new InvalidArgumentException(
103+
sprintf('Bundle %s dont have implemented getContainerExtension method.', $bundleClass)
84104
);
85105
}
86106

@@ -107,17 +127,10 @@ public function load() : void
107127
$this->container->addCompilerPass(
108128
new MakePrivateCommandsPublic()
109129
);
110-
111-
// Сохраняю инстанцированный бандл в статику.
112-
static::$bundlesMap[static::class][$bundle->getName()] = $bundle;
113-
} else {
114-
throw new InvalidArgumentException(
115-
sprintf(
116-
'Bundle %s dont have implemented getContainerExtension method.',
117-
$bundle->getName()
118-
)
119-
);
120130
}
131+
132+
// Сохраняю инстанцированный бандл в статику.
133+
static::$bundlesMap[static::class][$bundle->getName()] = $bundle;
121134
}
122135
}
123136

src/ServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ private function loadSymfonyBundles() : void
685685
{
686686
$this->bundlesLoader = new BundlesLoader(
687687
static::$containerBuilder,
688+
$this->environment,
688689
$this->pathBundlesConfig
689690
);
690691

tests/Cases/BundlesLoaderTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Prokl\ServiceProvider\Bundles\BundlesLoader;
77
use Prokl\ServiceProvider\Tests\Fixtures\DummyService;
88
use Prokl\ServiceProvider\Tests\Fixtures\TestingBundle;
9+
use Prokl\ServiceProvider\Tests\Fixtures\TestingBundleDev;
910
use Prokl\TestingTools\Base\BaseTestCase;
1011
use Prokl\TestingTools\Tools\PHPUnitUtils;
1112
use ReflectionException;
@@ -16,6 +17,7 @@
1617
* @package Prokl\ServiceProvider\Tests\Cases
1718
*
1819
* @since 01.06.2021
20+
* @since 03.07.2021 Актуализация.
1921
*/
2022
class BundlesLoaderTest extends BaseTestCase
2123
{
@@ -41,6 +43,7 @@ protected function setUp(): void
4143
$this->dummyContainer = new ContainerBuilder();
4244
$this->obTestObject = new BundlesLoader(
4345
$this->dummyContainer,
46+
'dev',
4447
'/../Fixtures/bundles.php'
4548
);
4649
}
@@ -59,6 +62,8 @@ public function testLoad() : void
5962
$this->assertCount(1, $result);
6063
$this->assertSame('TestingBundle', array_key_first($result));
6164
$this->assertInstanceOf(TestingBundle::class, $result['TestingBundle']);
65+
// Не загрузился ли бандл для другого окружения.
66+
$this->assertArrayNotHasKey(TestingBundleDev::class, $result);
6267
}
6368

6469
/**
@@ -70,6 +75,7 @@ public function testLoadDefaultPath() : void
7075
{
7176
$this->obTestObject = new BundlesLoader(
7277
$this->dummyContainer,
78+
'dev',
7379
'/../Fixtures/fake.php' // Несуществующий конфиг
7480
);
7581

@@ -80,6 +86,28 @@ public function testLoadDefaultPath() : void
8086
$this->assertEmpty($result);
8187
}
8288

89+
/**
90+
* load(). Другое окружение.
91+
*
92+
* @return void
93+
*/
94+
public function testLoadAnotherEnv() : void
95+
{
96+
$this->obTestObject = new BundlesLoader(
97+
$this->dummyContainer,
98+
'test',
99+
'/../Fixtures/bundles.php'
100+
);
101+
102+
$this->obTestObject->load();
103+
104+
$result = $this->obTestObject->bundles();
105+
106+
$this->assertArrayHasKey('TestingBundle', $result);
107+
$this->assertArrayHasKey('TestingBundleDev', $result);
108+
$this->assertInstanceOf(TestingBundleDev::class, $result['TestingBundleDev']);
109+
}
110+
83111
/**
84112
* load(). Бандл без метода RegisterExtension.
85113
*
@@ -89,11 +117,14 @@ public function testLoadWithoutRegisterExtension() : void
89117
{
90118
$this->obTestObject = new BundlesLoader(
91119
$this->dummyContainer,
120+
'dev',
92121
'/../Fixtures/invalid_bundles.php'
93122
);
94123

95124
$this->expectException(InvalidArgumentException::class);
96-
$this->expectExceptionMessage('Bundle TestingInvalidBundle dont have implemented getContainerExtension method.');
125+
$this->expectExceptionMessage(
126+
'Bundle Prokl\ServiceProvider\Tests\Fixtures\TestingInvalidBundle dont have implemented getContainerExtension method.'
127+
);
97128

98129
$this->obTestObject->load();
99130
}
@@ -107,6 +138,7 @@ public function testLoadInvalidClass() : void
107138
{
108139
$this->obTestObject = new BundlesLoader(
109140
$this->dummyContainer,
141+
'dev',
110142
'/../Fixtures/fake_bundles.php'
111143
);
112144

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Tests\Fixtures;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
/**
8+
* Class TestingBundleDev
9+
* @package Prokl\ServiceProvider\Tests\Fixtures
10+
*/
11+
class TestingBundleDev extends Bundle
12+
{
13+
}

tests/Fixtures/TestingInvalidBundle.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
namespace Prokl\ServiceProvider\Tests\Fixtures;
44

5-
use Symfony\Component\HttpKernel\Bundle\Bundle;
6-
75
/**
86
* Class TestingInvalidBundle
97
* @package Prokl\ServiceProvider\Tests\Fixtures
108
*/
11-
class TestingInvalidBundle extends Bundle
9+
class TestingInvalidBundle
1210
{
1311

1412
}

tests/Fixtures/bundles.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
22
return [
3-
Prokl\ServiceProvider\Tests\Fixtures\TestingBundle::class => ['all' => true]
3+
Prokl\ServiceProvider\Tests\Fixtures\TestingBundle::class => ['all' => true],
4+
Prokl\ServiceProvider\Tests\Fixtures\TestingBundleDev::class => ['test' => true]
45
];

0 commit comments

Comments
 (0)