mirror of
https://github.com/symfony/framework-bundle.git
synced 2026-03-24 01:12:20 +01:00
[HttpKernel] Make Kernel::getShareDir() nullable
This commit is contained in:
@@ -82,7 +82,7 @@ class AboutCommand extends Command
|
||||
['Charset', $kernel->getCharset()],
|
||||
['Cache directory', self::formatPath($kernel->getCacheDir(), $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($kernel->getCacheDir()).'</>)'],
|
||||
['Build directory', self::formatPath($buildDir, $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($buildDir).'</>)'],
|
||||
['Share directory', self::formatPath($shareDir, $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($shareDir).'</>)'],
|
||||
['Share directory', null === $shareDir ? 'none' : self::formatPath($shareDir, $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($shareDir).'</>)'],
|
||||
['Log directory', self::formatPath($kernel->getLogDir(), $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($kernel->getLogDir()).'</>)'],
|
||||
new TableSeparator(),
|
||||
['<info>PHP</>'],
|
||||
|
||||
@@ -83,6 +83,6 @@ class HttpCache extends BaseHttpCache
|
||||
|
||||
protected function createStore(): StoreInterface
|
||||
{
|
||||
return $this->store ?? new Store($this->cacheDir ?: $this->kernel->getShareDir().'/http_cache');
|
||||
return $this->store ?? new Store($this->cacheDir ?: ($this->kernel->getShareDir() ?? $this->kernel->getCacheDir()).'/http_cache');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,10 +124,15 @@ trait MicroKernelTrait
|
||||
return parent::getBuildDir();
|
||||
}
|
||||
|
||||
public function getShareDir(): string
|
||||
public function getShareDir(): ?string
|
||||
{
|
||||
if (isset($_SERVER['APP_SHARE_DIR'])) {
|
||||
return $_SERVER['APP_SHARE_DIR'].'/'.$this->environment;
|
||||
if (false === $dir = filter_var($_SERVER['APP_SHARE_DIR'], \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE) ?? $_SERVER['APP_SHARE_DIR']) {
|
||||
return null;
|
||||
}
|
||||
if (\is_string($dir)) {
|
||||
return $dir.'/'.$this->environment;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::getShareDir();
|
||||
|
||||
@@ -46,6 +46,49 @@ class MicroKernelTraitTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetShareDirDisabledByEnv()
|
||||
{
|
||||
$previous = $_SERVER['APP_SHARE_DIR'] ?? null;
|
||||
$_SERVER['APP_SHARE_DIR'] = 'false';
|
||||
|
||||
try {
|
||||
$kernel = $this->kernel = new ConcreteMicroKernel('test', false);
|
||||
|
||||
$this->assertNull($kernel->getShareDir());
|
||||
|
||||
$parameters = $kernel->getKernelParameters();
|
||||
$this->assertArrayNotHasKey('kernel.share_dir', $parameters);
|
||||
} finally {
|
||||
if (null === $previous) {
|
||||
unset($_SERVER['APP_SHARE_DIR']);
|
||||
} else {
|
||||
$_SERVER['APP_SHARE_DIR'] = $previous;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetShareDirCustomPathFromEnv()
|
||||
{
|
||||
$previous = $_SERVER['APP_SHARE_DIR'] ?? null;
|
||||
$_SERVER['APP_SHARE_DIR'] = sys_get_temp_dir();
|
||||
|
||||
try {
|
||||
$kernel = $this->kernel = new ConcreteMicroKernel('test', false);
|
||||
|
||||
$expected = rtrim(sys_get_temp_dir(), '/').'/test';
|
||||
$this->assertSame($expected, $kernel->getShareDir());
|
||||
|
||||
$parameters = $kernel->getKernelParameters();
|
||||
$this->assertSame($expected, $parameters['kernel.share_dir'] ?? null);
|
||||
} finally {
|
||||
if (null === $previous) {
|
||||
unset($_SERVER['APP_SHARE_DIR']);
|
||||
} else {
|
||||
$_SERVER['APP_SHARE_DIR'] = $previous;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
$kernel = $this->kernel = new ConcreteMicroKernel('test', false);
|
||||
|
||||
Reference in New Issue
Block a user