Skip to content

Commit fb1a65e

Browse files
committed
AppKernel. Доработки.
1 parent dae759c commit fb1a65e

File tree

4 files changed

+88
-19
lines changed

4 files changed

+88
-19
lines changed

readme.MD

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,43 @@ $serviceProvider = new ServiceProvider('local/configs/services.yaml');
5959
1) Опция `compile.container` в подтягиваемом конфиге - компилировать ли контейнер в файл. Если не задана, то "нет, не компилировать".
6060
Имеет смысл для окружения, не равного "dev". Т.е. опция управляет дампированием контейнера на проде.
6161

62-
Место, где хранятся дампы контейнеров: `/bitrix/cache/<SITE_ID>/containers`
62+
Место, где хранятся дампы контейнеров: `<значение переменной контейнера kernel.cache_dir>/<SITE_ID>/containers`
63+
64+
#### Пути к кэшу и логам
65+
66+
Определяются классом `AppKernel`. По умолчанию:
67+
68+
- путь к кэшу (`kernel.cache_dir`) - `/bitrix/cache`
69+
- путь к логам (`kernel.logs_dir`) - `'/../../logs'` (два уровня выше DOCUMENT_ROOT - особенности используемой
70+
сборки Битрикс)
71+
72+
Чтобы это изменить нужно отнаследоваться от класса `AppKernel` и переопределить несколько переменных:
73+
74+
```php
75+
use Prokl\ServiceProvider\Services\AppKernel;
76+
77+
class MyKernel extends AppKernel
78+
{
79+
protected $cacheDir = '/bitrix/cache/mycache';
80+
81+
protected $logDir = '/logs-saver';
82+
}
83+
```
84+
(второй вариант - отнаследоваться от `AppKernel` и переопределить методы `getCacheDir` и `getLogDir`).
85+
86+
Изменить через наследование класс ядра:
87+
88+
```php
89+
class MyServiceProvider extends ServiceProvider
90+
{
91+
protected $kernelServiceClass = MyKernel::class;
92+
93+
protected $cacheDir = '/bitrix/cache/mycache';
94+
95+
}
96+
```
97+
98+
Второй вариант - отнаследоваться от `ServiceProvider` и заменить метод `getPathCacheDirectory` своей логикой.
6399

64100
## Поддержка бандлов
65101

@@ -119,10 +155,6 @@ class ExampleMicroServiceProvider extends AbstractStandaloneServiceProvider
119155
Пример класса `ExampleAppKernel`:
120156

121157
```php
122-
/**
123-
* Class ExampleAppKernel
124-
* @package Prokl\ServiceProvider\Micro
125-
*/
126158
use Prokl\ServiceProvider\Micro\AbstractKernel;
127159
128160
class ExampleAppKernel extends AbstractKernel

src/ServiceProvider.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ class ServiceProvider
7676
*/
7777
private const COMPILED_CONTAINER_FILE = '/container.php';
7878

79-
/**
80-
* @const string COMPILED_CONTAINER_DIR Путь к скомпилированному контейнеру.
81-
*/
82-
private const COMPILED_CONTAINER_DIR = '/bitrix/cache/';
83-
8479
/**
8580
* @const string CONFIG_EXTS Расширения конфигурационных файлов.
8681
*/
@@ -116,6 +111,11 @@ class ServiceProvider
116111
*/
117112
protected $symfonyCompilerClass = SymfonyCompilerPassBag::class;
118113

114+
/**
115+
* @var string $cacheDir Путь к кэшу.
116+
*/
117+
protected $cacheDir = '/bitrix/cache/';
118+
119119
/**
120120
* @var ErrorScreen $errorHandler Обработчик ошибок.
121121
*/
@@ -698,14 +698,15 @@ private function createCacheDirectory() : void
698698
* @since 03.03.2021
699699
* @since 28.06.2021 Путь к кэшу в зависимости от SITE_ID.
700700
*/
701-
private function getPathCacheDirectory(string $filename) : string
701+
protected function getPathCacheDirectory(string $filename) : string
702702
{
703703
$siteId = 's1';
704704
if (defined(SITE_ID)) {
705705
$siteId = SITE_ID;
706706
}
707707

708-
return $this->projectRoot . self::COMPILED_CONTAINER_DIR . $siteId . '/containers/'. md5($filename);
708+
return $this->projectRoot . $this->cacheDir . '/' .
709+
$siteId . '/containers/'. md5($filename);
709710
}
710711

711712
/**

src/Services/AppKernel.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,24 @@ class AppKernel extends Kernel
3737
protected $debug;
3838

3939
/**
40-
* @var string $projectDir DOCUMENT_ROOT.
40+
* @var ContainerInterface $kernelContainer Копия контейнера.
4141
*/
42-
private $projectDir;
42+
protected static $kernelContainer;
4343

4444
/**
45-
* @var ContainerInterface $kernelContainer Копия контейнера.
45+
* @var string $cacheDir Путь к директории с кэшом.
4646
*/
47-
protected static $kernelContainer;
47+
protected $cacheDir = '/bitrix/cache';
48+
49+
/**
50+
* @var string $logDir Путь к директории с логами.
51+
*/
52+
protected $logDir = '/../../logs';
53+
54+
/**
55+
* @var string $projectDir DOCUMENT_ROOT.
56+
*/
57+
protected $projectDir;
4858

4959
/**
5060
* AppKernel constructor.
@@ -56,7 +66,7 @@ public function __construct(string $environment, bool $debug)
5666
{
5767
$this->debug = $debug;
5868
$this->environment = $environment;
59-
$this->projectDir = $_SERVER['DOCUMENT_ROOT'];
69+
$this->projectDir = $this->getProjectDir();
6070

6171
parent::__construct($this->environment, $this->debug);
6272

@@ -72,7 +82,7 @@ public function __construct(string $environment, bool $debug)
7282
*/
7383
public function getCacheDir(): string
7484
{
75-
$cachePath = $this->getProjectDir() . '/bitrix/cache/';
85+
$cachePath = $this->getProjectDir() . $this->cacheDir;
7686
if (!@file_exists($cachePath)) {
7787
@mkdir($cachePath, 0777, true);
7888
}
@@ -85,7 +95,7 @@ public function getCacheDir(): string
8595
*/
8696
public function getLogDir()
8797
{
88-
return $this->getProjectDir() . '../../logs';
98+
return $this->getProjectDir() . $this->logDir;
8999
}
90100

91101
/**

tests/Cases/ServiceProviderTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Prokl\BitrixTestingTools\Base\BitrixableTestCase;
88
use Prokl\ServiceProvider\ServiceProvider;
99
use Prokl\ServiceProvider\Services\AppKernel;
10+
use Prokl\TestingTools\Tools\PHPUnitUtils;
11+
use ReflectionException;
1012
use ReflectionProperty;
1113
use RuntimeException;
1214

@@ -259,6 +261,30 @@ public function testLoadBundles() : void
259261
$this->assertNotEmpty($bundlesMeta);
260262
}
261263

264+
/**
265+
* getPathCacheDirectory().
266+
*
267+
* @return void
268+
* @throws ReflectionException
269+
* @throws Exception
270+
*/
271+
public function testGetPathCacheDirectory() : void
272+
{
273+
$this->obTestObject = new ServiceProvider($this->pathYamlConfig);
274+
275+
$filename = 'test';
276+
$result = PHPUnitUtils::callMethod(
277+
$this->obTestObject,
278+
'getPathCacheDirectory',
279+
[$filename]
280+
);
281+
282+
$this->assertStringContainsString(
283+
'/bitrix/cache/',
284+
$result
285+
);
286+
}
287+
262288
/**
263289
* Рекурсивно удалить папку со всем файлами и папками.
264290
*

0 commit comments

Comments
 (0)