bug #22728 [HttpKernel] Fix kernel.project_dir extensibility (chalasr)

This PR was merged into the 3.3 branch.

Discussion
----------

[HttpKernel] Fix kernel.project_dir extensibility

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/pull/22727
| License       | MIT
| Doc PR        | n/a

Alternative to #22727 that makes use of the existing public api.

Commits
-------

3230fc7e70 Fix kernel.project_dir extensibility
This commit is contained in:
Fabien Potencier 2017-05-25 16:46:48 -07:00
commit 26afd2c1bf
2 changed files with 42 additions and 2 deletions

View File

@ -82,7 +82,6 @@ abstract class Kernel implements KernelInterface, TerminableInterface
$this->environment = $environment;
$this->debug = (bool) $debug;
$this->rootDir = $this->getRootDir();
$this->projectDir = $this->getProjectDir();
$this->name = $this->getName();
if ($this->debug) {
@ -604,7 +603,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface
return array_merge(
array(
'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir,
'kernel.project_dir' => realpath($this->projectDir) ?: $this->projectDir,
'kernel.project_dir' => realpath($this->getProjectDir()) ?: $this->getProjectDir(),
'kernel.environment' => $this->environment,
'kernel.debug' => $this->debug,
'kernel.name' => $this->name,

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Config\EnvParametersResource;
@ -761,6 +762,15 @@ EOF;
unset($_SERVER['SYMFONY__FOO__BAR']);
}
public function testProjectDirExtension()
{
$kernel = new CustomProjectDirKernel('test', true);
$kernel->boot();
$this->assertSame('foo', $kernel->getProjectDir());
$this->assertSame('foo', $kernel->getContainer()->getParameter('kernel.project_dir'));
}
/**
* Returns a mock for the BundleInterface.
*
@ -857,3 +867,34 @@ class TestKernel implements HttpKernelInterface
{
}
}
class CustomProjectDirKernel extends Kernel
{
private $baseDir;
public function __construct()
{
parent::__construct('test', false);
$this->baseDir = 'foo';
}
public function registerBundles()
{
return array();
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
}
public function getProjectDir()
{
return $this->baseDir;
}
public function getRootDir()
{
return __DIR__.'/Fixtures';
}
}