Skip to content

Commit 7311853

Browse files
minor #62204 [HttpKernel] Make Kernel::getShareDir() nullable (nicolas-grekas)
This PR was merged into the 7.4 branch. Discussion ---------- [HttpKernel] Make Kernel::getShareDir() nullable | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | #62202 | License | MIT This allows specifying explicitly when the app has no share dir. If the method returns `null`, the `kernel.share_dir` parameter is not defined. This gives a nice way to spot where references to this parameter remain with `You have requested a non-existent parameter "kernel.share_dir"` exceptions as a hint. Setting the `APP_SHARE_DIR` env var to any falsy value (`off`, `false`, `0`, etc as supported by `filter_var()`) will lead to configuring the app with no share dir. Commits ------- a32ccf081d2 [HttpKernel] Make Kernel::getShareDir() nullable
2 parents fb9c1e8 + 1181ebb commit 7311853

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

Command/AboutCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8282
['Charset', $kernel->getCharset()],
8383
['Cache directory', self::formatPath($kernel->getCacheDir(), $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($kernel->getCacheDir()).'</>)'],
8484
['Build directory', self::formatPath($buildDir, $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($buildDir).'</>)'],
85-
['Share directory', self::formatPath($shareDir, $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($shareDir).'</>)'],
85+
['Share directory', null === $shareDir ? 'none' : self::formatPath($shareDir, $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($shareDir).'</>)'],
8686
['Log directory', self::formatPath($kernel->getLogDir(), $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($kernel->getLogDir()).'</>)'],
8787
new TableSeparator(),
8888
['<info>PHP</>'],

HttpCache/HttpCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ protected function createSurrogate(): SurrogateInterface
8383

8484
protected function createStore(): StoreInterface
8585
{
86-
return $this->store ?? new Store($this->cacheDir ?: $this->kernel->getShareDir().'/http_cache');
86+
return $this->store ?? new Store($this->cacheDir ?: ($this->kernel->getShareDir() ?? $this->kernel->getCacheDir()).'/http_cache');
8787
}
8888
}

Kernel/MicroKernelTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,15 @@ public function getBuildDir(): string
124124
return parent::getBuildDir();
125125
}
126126

127-
public function getShareDir(): string
127+
public function getShareDir(): ?string
128128
{
129129
if (isset($_SERVER['APP_SHARE_DIR'])) {
130-
return $_SERVER['APP_SHARE_DIR'].'/'.$this->environment;
130+
if (false === $dir = filter_var($_SERVER['APP_SHARE_DIR'], \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE) ?? $_SERVER['APP_SHARE_DIR']) {
131+
return null;
132+
}
133+
if (\is_string($dir)) {
134+
return $dir.'/'.$this->environment;
135+
}
131136
}
132137

133138
return parent::getShareDir();

Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,49 @@ protected function tearDown(): void
4646
}
4747
}
4848

49+
public function testGetShareDirDisabledByEnv()
50+
{
51+
$previous = $_SERVER['APP_SHARE_DIR'] ?? null;
52+
$_SERVER['APP_SHARE_DIR'] = 'false';
53+
54+
try {
55+
$kernel = $this->kernel = new ConcreteMicroKernel('test', false);
56+
57+
$this->assertNull($kernel->getShareDir());
58+
59+
$parameters = $kernel->getKernelParameters();
60+
$this->assertArrayNotHasKey('kernel.share_dir', $parameters);
61+
} finally {
62+
if (null === $previous) {
63+
unset($_SERVER['APP_SHARE_DIR']);
64+
} else {
65+
$_SERVER['APP_SHARE_DIR'] = $previous;
66+
}
67+
}
68+
}
69+
70+
public function testGetShareDirCustomPathFromEnv()
71+
{
72+
$previous = $_SERVER['APP_SHARE_DIR'] ?? null;
73+
$_SERVER['APP_SHARE_DIR'] = sys_get_temp_dir();
74+
75+
try {
76+
$kernel = $this->kernel = new ConcreteMicroKernel('test', false);
77+
78+
$expected = rtrim(sys_get_temp_dir(), '/').'/test';
79+
$this->assertSame($expected, $kernel->getShareDir());
80+
81+
$parameters = $kernel->getKernelParameters();
82+
$this->assertSame($expected, $parameters['kernel.share_dir'] ?? null);
83+
} finally {
84+
if (null === $previous) {
85+
unset($_SERVER['APP_SHARE_DIR']);
86+
} else {
87+
$_SERVER['APP_SHARE_DIR'] = $previous;
88+
}
89+
}
90+
}
91+
4992
public function test()
5093
{
5194
$kernel = $this->kernel = new ConcreteMicroKernel('test', false);

0 commit comments

Comments
 (0)