Skip to content

Commit 4623bcd

Browse files
[FrameworkBundle] Make APP_*_DIR relative to the project directory
1 parent ed9f4e9 commit 4623bcd

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

Kernel/MicroKernelTrait.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,30 @@ private function getBundlesPath(): string
108108

109109
public function getCacheDir(): string
110110
{
111-
if (isset($_SERVER['APP_CACHE_DIR'])) {
112-
return $_SERVER['APP_CACHE_DIR'].'/'.$this->environment;
111+
if (null !== $dir = $_SERVER['APP_CACHE_DIR'] ?? null) {
112+
return $this->getEnvDir($dir);
113113
}
114114

115115
return parent::getCacheDir();
116116
}
117117

118118
public function getBuildDir(): string
119119
{
120-
if (isset($_SERVER['APP_BUILD_DIR'])) {
121-
return $_SERVER['APP_BUILD_DIR'].'/'.$this->environment;
120+
if (null !== $dir = $_SERVER['APP_BUILD_DIR'] ?? null) {
121+
return $this->getEnvDir($dir);
122122
}
123123

124124
return parent::getBuildDir();
125125
}
126126

127127
public function getShareDir(): ?string
128128
{
129-
if (isset($_SERVER['APP_SHARE_DIR'])) {
130-
if (false === $dir = filter_var($_SERVER['APP_SHARE_DIR'], \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE) ?? $_SERVER['APP_SHARE_DIR']) {
129+
if (null !== $dir = $_SERVER['APP_SHARE_DIR'] ?? null) {
130+
if (false === $dir = filter_var($dir, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE) ?? $dir) {
131131
return null;
132132
}
133133
if (\is_string($dir)) {
134-
return $dir.'/'.$this->environment;
134+
return $this->getEnvDir($dir);
135135
}
136136
}
137137

@@ -267,4 +267,16 @@ protected function getKernelParameters(): array
267267

268268
return $parameters;
269269
}
270+
271+
private function getEnvDir(string $dir): string
272+
{
273+
if ('' !== $dir && \in_array($dir[0], ['/', '\\'], true)) {
274+
return $dir.'/'.$this->environment;
275+
}
276+
if ('\\' === \DIRECTORY_SEPARATOR && ':' === ($dir[1] ?? '') && 65 <= \ord($dir[0]) && \ord($dir[0]) <= 122 && !\in_array($dir[0], ['[', ']', '^', '_', '`'], true)) {
277+
return $dir.'/'.$this->environment;
278+
}
279+
280+
return $this->getProjectDir().'/'.$dir.'/'.$this->environment;
281+
}
270282
}

Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Psr\Log\NullLogger;
1717
use Symfony\Bundle\FrameworkBundle\Console\Application;
18+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
1819
use Symfony\Component\Console\Attribute\AsCommand;
1920
use Symfony\Component\Console\Input\ArrayInput;
2021
use Symfony\Component\Console\Output\BufferedOutput;
@@ -245,4 +246,33 @@ public function testGetKernelParametersWithBundlesFile()
245246
'TestBundle' => ['test' => true, 'dev' => true],
246247
], $parameters['.kernel.bundles_definition']);
247248
}
249+
250+
public function testRelativeEnvDirsAreResolvedFromProjectDir()
251+
{
252+
$_SERVER['APP_CACHE_DIR'] = 'var/custom-cache';
253+
$_SERVER['APP_BUILD_DIR'] = 'var/custom-build';
254+
$_SERVER['APP_SHARE_DIR'] = 'var/custom-share';
255+
256+
$projectDir = sys_get_temp_dir().'/sf_env_dir_kernel';
257+
$kernel = new EnvDirKernel($projectDir);
258+
259+
$this->assertSame($projectDir.'/var/custom-cache/test', $kernel->getCacheDir());
260+
$this->assertSame($projectDir.'/var/custom-build/test', $kernel->getBuildDir());
261+
$this->assertSame($projectDir.'/var/custom-share/test', $kernel->getShareDir());
262+
}
263+
}
264+
265+
class EnvDirKernel extends Kernel
266+
{
267+
use MicroKernelTrait;
268+
269+
public function __construct(private readonly string $projectDir)
270+
{
271+
parent::__construct('test', false);
272+
}
273+
274+
public function getProjectDir(): string
275+
{
276+
return $this->projectDir;
277+
}
248278
}

0 commit comments

Comments
 (0)