Skip to content

Commit 0910ae8

Browse files
committed
AppKernel. Рефакторинг.
1 parent b8c56e6 commit 0910ae8

File tree

2 files changed

+94
-22
lines changed

2 files changed

+94
-22
lines changed

src/Services/AppKernel.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Prokl\ServiceProvider\Services;
44

55
use Bitrix\Main\Application;
6-
use Bitrix\Main\SystemException;
76
use Prokl\ServiceProvider\Bundles\BundlesLoader;
87
use LogicException;
98
use Symfony\Component\Config\Loader\LoaderInterface;
@@ -27,6 +26,11 @@ class AppKernel extends Kernel
2726
*/
2827
protected $environment;
2928

29+
/**
30+
* @var string $bundlesConfigFile Файл с конфигурацией бандлов.
31+
*/
32+
protected $bundlesConfigFile = '/local/configs/bundles.php';
33+
3034
/**
3135
* @var boolean $debug Отладка? Оно же служит для определения типа окружения.
3236
*/
@@ -51,7 +55,7 @@ public function __construct(string $environment, bool $debug)
5155

5256
parent::__construct($this->environment, $this->debug);
5357

54-
$this->registerStandaloneBundles(); // "Standalone" бандлы.
58+
$this->bundles = $this->registerStandaloneBundles(); // "Standalone" бандлы.
5559
}
5660

5761
/**
@@ -162,33 +166,18 @@ public function setContainer(?ContainerInterface $container = null) : void
162166
$this->container = $container;
163167
}
164168

165-
/**
166-
* Хост сайта.
167-
*
168-
* @return string
169-
*
170-
* @since 08.10.2020
171-
*/
172-
private function getSiteHost() : string
173-
{
174-
return $this->getSchema() . $_SERVER['HTTP_HOST'];
175-
}
176-
177169
/**
178170
* REQUEST_URI.
179171
*
180172
* @return string
181173
*
182-
* @throws SystemException Битриксовые ошибки.
183-
*
184174
* @since 16.10.2020
185175
*/
186176
public function getRequestUri() : string
187177
{
188178
$request = Application::getInstance()->getContext()->getRequest();
189-
$uriString = $request->getRequestUri();
190179

191-
return !empty($uriString) ? $uriString : '';
180+
return (string)$request->getRequestUri();
192181
}
193182

194183
/**
@@ -204,10 +193,12 @@ public function registerContainerConfiguration(LoaderInterface $loader)
204193
* @return iterable|BundleInterface[]
205194
*
206195
* @since 02.06.2021 Если файл не существует - игнорим.
196+
*
197+
* @internal пока не используется. Манипуляции с бандлами - через класс BundlesLoader.
207198
*/
208199
public function registerBundles(): iterable
209200
{
210-
$bundleConfigPath = $this->getProjectDir() . '/local/configs/bundles.php';
201+
$bundleConfigPath = $this->getProjectDir() . $this->bundlesConfigFile;
211202

212203
if (!@file_exists($bundleConfigPath)) {
213204
return [];
@@ -229,6 +220,7 @@ public function registerBundles(): iterable
229220
* @param object $bundle Бандл.
230221
*
231222
* @return void
223+
* @throws LogicException Когда проскакивают дубликаты бандлов.
232224
*/
233225
public function registerBundle($bundle) : void
234226
{
@@ -243,15 +235,31 @@ public function registerBundle($bundle) : void
243235
/**
244236
* Регистрация "отдельностоящих" бандлов.
245237
*
246-
* @return void
238+
* @return array
247239
*
248240
* @since 25.10.2020
249241
*/
250-
public function registerStandaloneBundles(): void
242+
public function registerStandaloneBundles(): array
251243
{
252-
foreach (BundlesLoader::getBundlesMap() as $bundle) {
244+
$bundles = BundlesLoader::getBundlesMap();
245+
246+
foreach ($bundles as $bundle) {
253247
$this->registerBundle($bundle);
254248
}
249+
250+
return $bundles;
251+
}
252+
253+
/**
254+
* Хост сайта.
255+
*
256+
* @return string
257+
*
258+
* @since 08.10.2020
259+
*/
260+
private function getSiteHost() : string
261+
{
262+
return $this->getSchema() . $_SERVER['HTTP_HOST'];
255263
}
256264

257265
/**

tests/Cases/AppKernelTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Tests\Cases;
4+
5+
use Prokl\BitrixTestingTools\Base\BitrixableTestCase;
6+
use Prokl\ServiceProvider\Bundles\BundlesLoader;
7+
use Prokl\ServiceProvider\Services\AppKernel;
8+
use ReflectionException;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
11+
/**
12+
* Class ServiceProviderTest
13+
* @package Prokl\ServiceProvider\Tests
14+
*
15+
* @since 05.07.2021
16+
*
17+
*/
18+
class AppKernelTest extends BitrixableTestCase
19+
{
20+
/**
21+
* @var AppKernel $obTestObject
22+
*/
23+
protected $obTestObject;
24+
25+
/**
26+
* Загрузка бандлов по кастомному пути.
27+
*
28+
* @return void
29+
*/
30+
public function testLoadBundlesFromCustomPath() : void
31+
{
32+
$container = new ContainerBuilder();
33+
34+
$bundlesLoader = new BundlesLoader(
35+
$container,
36+
'dev',
37+
'/../../../../tests/Fixtures/bundles.php'
38+
);
39+
$bundlesLoader->load();
40+
41+
$this->obTestObject = new AppKernel('dev', true);
42+
$result = $this->obTestObject->getBundlesMetaData();
43+
44+
$this->assertNotEmpty($result['kernel.bundles']);
45+
$this->assertNotEmpty($result['kernel.bundles_metadata']);
46+
}
47+
48+
/**
49+
* getRequestUri().
50+
*
51+
* @return void
52+
* @throws ReflectionException
53+
*/
54+
public function testGetRequestUri() : void
55+
{
56+
$this->goTo('/test/');
57+
58+
$this->obTestObject = new AppKernel('dev', true);
59+
60+
$result = $this->obTestObject->getRequestUri();
61+
62+
$this->assertSame('/test/', $result);
63+
}
64+
}

0 commit comments

Comments
 (0)