Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a58816

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 ------- a32ccf0 [HttpKernel] Make Kernel::getShareDir() nullable
2 parents 4e517b7 + a32ccf0 commit 8a58816

File tree

6 files changed

+57
-10
lines changed

6 files changed

+57
-10
lines changed

src/Symfony/Bundle/FrameworkBundle/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</>'],

src/Symfony/Bundle/FrameworkBundle/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
}

src/Symfony/Bundle/FrameworkBundle/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();

src/Symfony/Bundle/FrameworkBundle/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);

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public function getBuildDir(): string
303303
return $this->getCacheDir();
304304
}
305305

306-
public function getShareDir(): string
306+
public function getShareDir(): ?string
307307
{
308308
// Returns $this->getCacheDir() for backward compatibility
309309
return $this->getCacheDir();
@@ -586,13 +586,12 @@ protected function getKernelParameters(): array
586586
'kernel.debug' => $this->debug,
587587
'kernel.build_dir' => realpath($dir = $this->warmupDir ?: $this->getBuildDir()) ?: $dir,
588588
'kernel.cache_dir' => realpath($dir = ($this->getCacheDir() === $this->getBuildDir() ? ($this->warmupDir ?: $this->getCacheDir()) : $this->getCacheDir())) ?: $dir,
589-
'kernel.share_dir' => realpath($dir = $this->getShareDir()) ?: $dir,
590589
'kernel.logs_dir' => realpath($dir = $this->getLogDir()) ?: $dir,
591590
'kernel.bundles' => $bundles,
592591
'kernel.bundles_metadata' => $bundlesMetadata,
593592
'kernel.charset' => $this->getCharset(),
594593
'kernel.container_class' => $this->getContainerClass(),
595-
];
594+
] + (null !== ($dir = $this->getShareDir()) ? ['kernel.share_dir' => realpath($dir) ?: $dir] : []);
596595
}
597596

598597
/**

src/Symfony/Component/HttpKernel/KernelInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
*
2121
* It manages an environment made of application kernel and bundles.
2222
*
23-
* @method string getShareDir() Returns the share directory - not implementing it is deprecated since Symfony 7.4.
24-
* This directory should be used to store data that is shared between all front-end servers.
25-
* This typically fits application caches.
23+
* @method string|null getShareDir() Returns the share directory - not implementing it is deprecated since Symfony 7.4.
24+
* This directory should be used to store data that is shared between all front-end servers; this typically fits application caches.
25+
* `null` should be returned if the application shall not use a share directory.
2626
*
2727
* @author Fabien Potencier <[email protected]>
2828
*/

0 commit comments

Comments
 (0)