Skip to content

Commit

Permalink
feat: Configuration dir can be set in env (#98)
Browse files Browse the repository at this point in the history
Closes #93

Can be set in environment variables. An absolute path or relative from
package root
  • Loading branch information
Gashmob authored Dec 23, 2024
2 parents d45a5fa + a689b5d commit fbfec61
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
18 changes: 14 additions & 4 deletions include/Services/LoadServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
File renamed without changes.
16 changes: 13 additions & 3 deletions tests/unit/Services/LoadServicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,11 @@ protected function setUp(): void
);
}

protected function tearDown(): void
{
unset($_ENV['CONFIG_DIR']);
}

public function testItCanLoadNoServices(): void
{
self::expectNotToPerformAssertions();
Expand All @@ -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));
Expand All @@ -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));
Expand Down

0 comments on commit fbfec61

Please sign in to comment.