From a689b5dbc72797bbc206284b7abf0a599c81202d Mon Sep 17 00:00:00 2001 From: Kevin Traini Date: Mon, 23 Dec 2024 21:10:13 +0100 Subject: [PATCH] feat: Configuration dir can be set in env Closes #93 Can be set in environment variables. An absolute path or relative from package root --- include/Services/LoadServices.php | 18 ++++++++++++++---- .../{config => custom_config_dir}/bar.yml | 0 tests/unit/Services/LoadServicesTest.php | 16 +++++++++++++--- 3 files changed, 27 insertions(+), 7 deletions(-) rename tests/unit/Fixtures/brick1/{config => custom_config_dir}/bar.yml (100%) diff --git a/include/Services/LoadServices.php b/include/Services/LoadServices.php index 3179ef3..e439ed0 100644 --- a/include/Services/LoadServices.php +++ b/include/Services/LoadServices.php @@ -27,6 +27,7 @@ namespace Archict\Core\Services; +use Archict\Core\Env\EnvironmentService; use Composer\InstalledVersions; use CuyZ\Valinor\Mapper\MappingError; use CuyZ\Valinor\Mapper\TreeMapper; @@ -137,7 +138,7 @@ private function getParameter(ReflectionParameter $parameter, ServiceManager $ma try { return $this->mapper->map( $type_name, - Yaml::parse(read($this->getConfigurationFilenameForService($service))) + Yaml::parse(read($this->getConfigurationFilenameForService($service, $manager->get(EnvironmentService::class)))) ); } catch (MappingError $error) { throw new ServiceConfigurationFileFormatInvalidException($service->reflection->getName(), $error); @@ -151,14 +152,23 @@ private function getParameter(ReflectionParameter $parameter, ServiceManager $ma * @return non-empty-string * @throws ServiceConfigurationFileNotFoundException */ - private function getConfigurationFilenameForService(ServiceRepresentation $representation): string + private function getConfigurationFilenameForService(ServiceRepresentation $representation, ?EnvironmentService $env): string { $base_filename = $representation->service_attribute->configuration_filename ?? (strtolower($representation->reflection->getShortName()) . '.yml'); $package_config = $representation->package_path . '/config/' . $base_filename; - $root_config = InstalledVersions::getRootPackage()['install_path'] . '/config/' . $base_filename; + if ($env !== null) { + $config_dir = (string) $env->get('CONFIG_DIR', InstalledVersions::getRootPackage()['install_path'] . '/config/'); + if ($config_dir !== '' && $config_dir[0] !== '/') { + $config_dir = InstalledVersions::getRootPackage()['install_path'] . '/' . $config_dir; + } + } else { + $config_dir = InstalledVersions::getRootPackage()['install_path'] . '/config/'; + } + + $root_config = $config_dir . $base_filename; - if (exists($root_config)) { + if ($root_config !== '' && exists($root_config)) { return $root_config; } diff --git a/tests/unit/Fixtures/brick1/config/bar.yml b/tests/unit/Fixtures/brick1/custom_config_dir/bar.yml similarity index 100% rename from tests/unit/Fixtures/brick1/config/bar.yml rename to tests/unit/Fixtures/brick1/custom_config_dir/bar.yml diff --git a/tests/unit/Services/LoadServicesTest.php b/tests/unit/Services/LoadServicesTest.php index 3085164..c9bb038 100644 --- a/tests/unit/Services/LoadServicesTest.php +++ b/tests/unit/Services/LoadServicesTest.php @@ -28,6 +28,7 @@ namespace Archict\Core\Services; use Archict\Brick\Service; +use Archict\Core\Env\Environment; use Archict\Core\Fixtures\brick1\src\Service1; use Archict\Core\Fixtures\brick1\src\Service1Configuration; use Archict\Core\Fixtures\ServiceWithDependency; @@ -58,6 +59,11 @@ protected function setUp(): void ); } + protected function tearDown(): void + { + unset($_ENV['CONFIG_DIR']); + } + public function testItCanLoadNoServices(): void { self::expectNotToPerformAssertions(); @@ -67,9 +73,11 @@ public function testItCanLoadNoServices(): void public function testItCanLoadServices(): void { $manager = new ServiceManager(); + $manager->add(new Environment()); + $_ENV['CONFIG_DIR'] = __DIR__ . '/../Fixtures/brick1/custom_config_dir/'; (new LoadServices($this->mapper))->loadServicesIntoManager( $manager, - [$this->service1] + [$this->service1], ); self::assertTrue($manager->has(Service1::class)); @@ -86,16 +94,18 @@ public function testItCannotLoadServicesWithNonExistentDependencies(): void self::expectException(ServicesCannotBeLoadedException::class); (new LoadServices($this->mapper))->loadServicesIntoManager( $manager, - [$this->service_with_dependency] + [$this->service_with_dependency], ); } public function testItCanLoadServicesWithDependencies(): void { $manager = new ServiceManager(); + $manager->add(new Environment()); + $_ENV['CONFIG_DIR'] = 'tests/unit/Fixtures/brick1/custom_config_dir/'; (new LoadServices($this->mapper))->loadServicesIntoManager( $manager, - [$this->service1, $this->service_with_dependency] + [$this->service1, $this->service_with_dependency], ); self::assertTrue($manager->has(Service1::class));