diff --git a/src/Core/AppKernel.php b/src/Core/AppKernel.php index 6d39b67de..792ceafd6 100644 --- a/src/Core/AppKernel.php +++ b/src/Core/AppKernel.php @@ -58,7 +58,7 @@ class AppKernel extends CoreKernel // Config loader // @todo refactor CoreConfigFactory - $configFactory = new CoreConfigFactory($manifestCacheFactory, $this->getEnvironment()); + $configFactory = new CoreConfigFactory($manifestCacheFactory); $configManifest = $configFactory->createRoot(); $configLoader = ConfigLoader::inst(); $configLoader->pushManifest($configManifest); @@ -76,17 +76,17 @@ class AppKernel extends CoreKernel public function getEnvironment() { + // Check set + if ($this->enviroment) { + return $this->enviroment; + } + // Check saved session $env = $this->sessionEnvironment(); if ($env) { return $env; } - // Check set - if ($this->enviroment) { - return $this->enviroment; - } - // Check getenv if ($env = getenv('SS_ENVIRONMENT_TYPE')) { return $env; diff --git a/src/Core/Config/CoreConfigFactory.php b/src/Core/Config/CoreConfigFactory.php index 338d8193d..88e1343c3 100644 --- a/src/Core/Config/CoreConfigFactory.php +++ b/src/Core/Config/CoreConfigFactory.php @@ -10,6 +10,8 @@ use SilverStripe\Config\Transformer\YamlTransformer; use SilverStripe\Core\Cache\CacheFactory; use SilverStripe\Core\Config\Middleware\ExtensionMiddleware; use SilverStripe\Core\Config\Middleware\InheritanceMiddleware; +use SilverStripe\Core\Injector\Injector; +use SilverStripe\Core\Kernel; use SilverStripe\Core\Manifest\ClassLoader; use SilverStripe\Core\Manifest\ModuleLoader; use Symfony\Component\Finder\Finder; @@ -25,14 +27,13 @@ class CoreConfigFactory protected $cacheFactory = null; /** - * @var string + * Create factory + * + * @param CacheFactory $cacheFactory */ - protected $environment = null; - - public function __construct(CacheFactory $cacheFactory, $environment) + public function __construct(CacheFactory $cacheFactory = null) { $this->cacheFactory = $cacheFactory; - $this->environment = $environment; } /** @@ -47,10 +48,12 @@ class CoreConfigFactory $instance = new CachedConfigCollection(); // Create config cache - $cache = $this->cacheFactory->create(CacheInterface::class.'.configcache', [ - 'namespace' => 'configcache' - ]); - $instance->setCache($cache); + if ($this->cacheFactory) { + $cache = $this->cacheFactory->create(CacheInterface::class . '.configcache', [ + 'namespace' => 'configcache' + ]); + $instance->setCache($cache); + } // Set collection creator $instance->setCollectionCreator(function () { @@ -162,7 +165,11 @@ class CoreConfigFactory } ) ->addRule('environment', function ($env) { - return strtolower($this->environment) === strtolower($env); + // Note: The below relies on direct assignment of kernel to injector instance, + // and will fail if failing back to config service locator + /** @var Kernel $kernel */ + $kernel = Injector::inst()->get(Kernel::class); + return strtolower($kernel->getEnvironment()) === strtolower($env); }) ->addRule('moduleexists', function ($module) { return ModuleLoader::inst()->getManifest()->moduleExists($module); diff --git a/src/Core/TestKernel.php b/src/Core/TestKernel.php index 6ef7433b4..b19551463 100644 --- a/src/Core/TestKernel.php +++ b/src/Core/TestKernel.php @@ -9,8 +9,8 @@ class TestKernel extends AppKernel { public function __construct($flush = true) { - parent::__construct($flush); $this->setEnvironment(self::DEV); + parent::__construct($flush); } /** diff --git a/tests/php/Core/Manifest/ClassLoaderTest.php b/tests/php/Core/Manifest/ClassLoaderTest.php index cb82edbf9..1525fdc3b 100644 --- a/tests/php/Core/Manifest/ClassLoaderTest.php +++ b/tests/php/Core/Manifest/ClassLoaderTest.php @@ -36,10 +36,12 @@ class ClassLoaderTest extends SapphireTest { parent::setUp(); - $this->baseManifest1 = dirname(__FILE__) . '/fixtures/classmanifest'; - $this->baseManifest2 = dirname(__FILE__) . '/fixtures/classmanifest_other'; - $this->testManifest1 = new ClassManifest($this->baseManifest1, false); - $this->testManifest2 = new ClassManifest($this->baseManifest2, false); + $this->baseManifest1 = __DIR__ . '/fixtures/classmanifest'; + $this->baseManifest2 = __DIR__ . '/fixtures/classmanifest_other'; + $this->testManifest1 = new ClassManifest($this->baseManifest1); + $this->testManifest2 = new ClassManifest($this->baseManifest2); + $this->testManifest1->init(); + $this->testManifest2->init(); } public function testExclusive() diff --git a/tests/php/Core/Manifest/ClassManifestTest.php b/tests/php/Core/Manifest/ClassManifestTest.php index 7ed2f3b26..f1549ca5c 100644 --- a/tests/php/Core/Manifest/ClassManifestTest.php +++ b/tests/php/Core/Manifest/ClassManifestTest.php @@ -32,8 +32,10 @@ class ClassManifestTest extends SapphireTest parent::setUp(); $this->base = dirname(__FILE__) . '/fixtures/classmanifest'; - $this->manifest = new ClassManifest($this->base, false); - $this->manifestTests = new ClassManifest($this->base, true); + $this->manifest = new ClassManifest($this->base); + $this->manifest->init(false); + $this->manifestTests = new ClassManifest($this->base); + $this->manifestTests->init(true); } public function testGetItemPath() @@ -155,6 +157,7 @@ class ClassManifestTest extends SapphireTest public function testManifestWarnsAboutDuplicateClasses() { $this->expectException(Exception::class); - new ClassManifest(dirname(__FILE__) . '/fixtures/classmanifest_duplicates', false); + $manifest = new ClassManifest(dirname(__FILE__) . '/fixtures/classmanifest_duplicates'); + $manifest->init(); } } diff --git a/tests/php/Core/Manifest/ConfigManifestTest.php b/tests/php/Core/Manifest/ConfigManifestTest.php index 5c71cc3d8..5d619d183 100644 --- a/tests/php/Core/Manifest/ConfigManifestTest.php +++ b/tests/php/Core/Manifest/ConfigManifestTest.php @@ -4,8 +4,9 @@ namespace SilverStripe\Core\Tests\Manifest; use Dotenv\Loader; use SilverStripe\Config\Collections\MemoryConfigCollection; -use SilverStripe\Control\Director; use SilverStripe\Core\Config\CoreConfigFactory; +use SilverStripe\Core\Injector\Injector; +use SilverStripe\Core\Kernel; use SilverStripe\Core\Manifest\ModuleLoader; use SilverStripe\Core\Manifest\ModuleManifest; use SilverStripe\Dev\SapphireTest; @@ -17,6 +18,7 @@ class ConfigManifestTest extends SapphireTest parent::setUp(); $moduleManifest = new ModuleManifest(dirname(__FILE__) . '/fixtures/configmanifest'); + $moduleManifest->init(); ModuleLoader::inst()->pushManifest($moduleManifest); } @@ -45,7 +47,8 @@ class ConfigManifestTest extends SapphireTest public function getTestConfig() { $config = new MemoryConfigCollection(); - $transformer = CoreConfigFactory::inst()->buildYamlTransformerForPath(dirname(__FILE__) . '/fixtures/configmanifest'); + $factory = new CoreConfigFactory(); + $transformer = $factory->buildYamlTransformerForPath(dirname(__FILE__) . '/fixtures/configmanifest'); $config->transform([$transformer]); return $config; } @@ -173,8 +176,10 @@ class ConfigManifestTest extends SapphireTest public function testEnvironmentRules() { + /** @var Kernel $kernel */ + $kernel = Injector::inst()->get(Kernel::class); foreach (array('dev', 'test', 'live') as $env) { - Director::set_environment_type($env); + $kernel->setEnvironment($env); $config = $this->getConfigFixtureValue('Environment'); foreach (array('dev', 'test', 'live') as $check) { diff --git a/tests/php/Core/Manifest/ModuleManifestTest.php b/tests/php/Core/Manifest/ModuleManifestTest.php index 2bdefa5f5..01d36f062 100644 --- a/tests/php/Core/Manifest/ModuleManifestTest.php +++ b/tests/php/Core/Manifest/ModuleManifestTest.php @@ -1,6 +1,5 @@ base = dirname(__FILE__) . '/fixtures/classmanifest'; - $this->manifest = new ModuleManifest($this->base, false); + $this->manifest = new ModuleManifest($this->base); + $this->manifest->init(); } public function testGetModules() diff --git a/tests/php/Core/Manifest/NamespacedClassManifestTest.php b/tests/php/Core/Manifest/NamespacedClassManifestTest.php index 482474a7e..83c5d36eb 100644 --- a/tests/php/Core/Manifest/NamespacedClassManifestTest.php +++ b/tests/php/Core/Manifest/NamespacedClassManifestTest.php @@ -28,7 +28,8 @@ class NamespacedClassManifestTest extends SapphireTest parent::setUp(); $this->base = dirname(__FILE__) . '/fixtures/namespaced_classmanifest'; - $this->manifest = new ClassManifest($this->base, false); + $this->manifest = new ClassManifest($this->base); + $this->manifest->init(); ClassLoader::inst()->pushManifest($this->manifest, false); } diff --git a/tests/php/Core/Manifest/ThemeResourceLoaderTest.php b/tests/php/Core/Manifest/ThemeResourceLoaderTest.php index 41e974b81..41ddff23c 100644 --- a/tests/php/Core/Manifest/ThemeResourceLoaderTest.php +++ b/tests/php/Core/Manifest/ThemeResourceLoaderTest.php @@ -36,7 +36,8 @@ class ThemeResourceLoaderTest extends SapphireTest // Fake project root $this->base = dirname(__FILE__) . '/fixtures/templatemanifest'; // New ThemeManifest for that root - $this->manifest = new ThemeManifest($this->base, 'myproject', false); + $this->manifest = new ThemeManifest($this->base, 'myproject'); + $this->manifest->init(); // New Loader for that root $this->loader = new ThemeResourceLoader($this->base); $this->loader->addSet('$default', $this->manifest);