mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUG Fix nesting bug in Kernel
This commit is contained in:
parent
188ce35d82
commit
9379834cb4
@ -37,6 +37,11 @@ class ConfigLoader
|
|||||||
*/
|
*/
|
||||||
public function getManifest()
|
public function getManifest()
|
||||||
{
|
{
|
||||||
|
if ($this !== self::$instance) {
|
||||||
|
throw new BadMethodCallException(
|
||||||
|
"Non-current config manifest cannot be accessed. Please call ->activate() first"
|
||||||
|
);
|
||||||
|
}
|
||||||
if (empty($this->manifests)) {
|
if (empty($this->manifests)) {
|
||||||
throw new BadMethodCallException("No config manifests available");
|
throw new BadMethodCallException("No config manifests available");
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,11 @@ class InjectorLoader
|
|||||||
*/
|
*/
|
||||||
public function getManifest()
|
public function getManifest()
|
||||||
{
|
{
|
||||||
|
if ($this !== self::$instance) {
|
||||||
|
throw new BadMethodCallException(
|
||||||
|
"Non-current injector manifest cannot be accessed. Please call ->activate() first"
|
||||||
|
);
|
||||||
|
}
|
||||||
if (empty($this->manifests)) {
|
if (empty($this->manifests)) {
|
||||||
throw new BadMethodCallException("No injector manifests available");
|
throw new BadMethodCallException("No injector manifests available");
|
||||||
}
|
}
|
||||||
@ -87,8 +92,8 @@ class InjectorLoader
|
|||||||
*/
|
*/
|
||||||
public function nest()
|
public function nest()
|
||||||
{
|
{
|
||||||
// Nest config
|
// Nest injector (note: Don't call getManifest()->nest() since that self-pushes a new manifest)
|
||||||
$manifest = $this->getManifest()->nest();
|
$manifest = clone $this->getManifest();
|
||||||
|
|
||||||
// Create new blank loader with new stack (top level nesting)
|
// Create new blank loader with new stack (top level nesting)
|
||||||
$newLoader = new static;
|
$newLoader = new static;
|
||||||
@ -101,9 +106,12 @@ class InjectorLoader
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Mark this instance as the current instance
|
* Mark this instance as the current instance
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function activate()
|
public function activate()
|
||||||
{
|
{
|
||||||
static::$instance = $this;
|
static::$instance = $this;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
81
tests/php/Core/KernelTest.php
Normal file
81
tests/php/Core/KernelTest.php
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Core\Tests;
|
||||||
|
|
||||||
|
use BadMethodCallException;
|
||||||
|
use SilverStripe\Control\Director;
|
||||||
|
use SilverStripe\Core\Config\Config;
|
||||||
|
use SilverStripe\Core\Config\ConfigLoader;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
use SilverStripe\Core\Injector\InjectorLoader;
|
||||||
|
use SilverStripe\Core\Kernel;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
|
||||||
|
class KernelTest extends SapphireTest
|
||||||
|
{
|
||||||
|
public function testNesting()
|
||||||
|
{
|
||||||
|
/** @var Kernel $kernel */
|
||||||
|
$kernel = Injector::inst()->get(Kernel::class);
|
||||||
|
|
||||||
|
$nested1 = $kernel->nest();
|
||||||
|
Director::config()->set('alternate_base_url', '/mysite/');
|
||||||
|
$this->assertEquals($nested1->getConfigLoader(), ConfigLoader::inst());
|
||||||
|
$this->assertEquals($nested1->getInjectorLoader(), InjectorLoader::inst());
|
||||||
|
$this->assertEquals(1, ConfigLoader::inst()->countManifests());
|
||||||
|
$this->assertEquals(1, InjectorLoader::inst()->countManifests());
|
||||||
|
|
||||||
|
// Re-nest
|
||||||
|
$nested2 = $nested1->nest();
|
||||||
|
|
||||||
|
// Nesting config / injector should increase this count
|
||||||
|
Injector::nest();
|
||||||
|
Config::nest();
|
||||||
|
$this->assertEquals($nested2->getConfigLoader(), ConfigLoader::inst());
|
||||||
|
$this->assertEquals($nested2->getInjectorLoader(), InjectorLoader::inst());
|
||||||
|
$this->assertEquals(2, ConfigLoader::inst()->countManifests());
|
||||||
|
$this->assertEquals(2, InjectorLoader::inst()->countManifests());
|
||||||
|
Director::config()->set('alternate_base_url', '/anothersite/');
|
||||||
|
|
||||||
|
// Nesting always resets sub-loaders to 1
|
||||||
|
$nested2->nest();
|
||||||
|
$this->assertEquals(1, ConfigLoader::inst()->countManifests());
|
||||||
|
$this->assertEquals(1, InjectorLoader::inst()->countManifests());
|
||||||
|
|
||||||
|
// Calling ->activate() on a previous kernel restores
|
||||||
|
$nested1->activate();
|
||||||
|
$this->assertEquals($nested1->getConfigLoader(), ConfigLoader::inst());
|
||||||
|
$this->assertEquals($nested1->getInjectorLoader(), InjectorLoader::inst());
|
||||||
|
$this->assertEquals('/mysite/', Director::config()->get('alternate_base_url'));
|
||||||
|
$this->assertEquals(1, ConfigLoader::inst()->countManifests());
|
||||||
|
$this->assertEquals(1, InjectorLoader::inst()->countManifests());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvalidInjectorDetection()
|
||||||
|
{
|
||||||
|
$this->expectException(BadMethodCallException::class);
|
||||||
|
$this->expectExceptionMessage(
|
||||||
|
"Non-current injector manifest cannot be accessed. Please call ->activate() first"
|
||||||
|
);
|
||||||
|
|
||||||
|
/** @var Kernel $kernel */
|
||||||
|
$kernel = Injector::inst()->get(Kernel::class);
|
||||||
|
$kernel->nest(); // $kernel is no longer current kernel
|
||||||
|
|
||||||
|
$kernel->getInjectorLoader()->getManifest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvalidConfigDetection()
|
||||||
|
{
|
||||||
|
$this->expectException(BadMethodCallException::class);
|
||||||
|
$this->expectExceptionMessage(
|
||||||
|
"Non-current config manifest cannot be accessed. Please call ->activate() first"
|
||||||
|
);
|
||||||
|
|
||||||
|
/** @var Kernel $kernel */
|
||||||
|
$kernel = Injector::inst()->get(Kernel::class);
|
||||||
|
$kernel->nest(); // $kernel is no longer current kernel
|
||||||
|
|
||||||
|
$kernel->getConfigLoader()->getManifest();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user