Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function getSection(string $sectionName):?ConfigSection {
return $this->sectionList[$sectionName] ?? null;
}

public function isProduction():bool {
return $this->getBool("app.production") ?? false;
}

protected function getSectionValue(string $name):?string {
$parts = explode(".", $name, 2);
$section = $this->getSection($parts[0]);
Expand Down
17 changes: 17 additions & 0 deletions src/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public static function createForProject(
$config = $config->withMerge($previousConfig);
}

if($file === "production") {
$config = self::withProductionFlag($config);
}

$previousConfig = $config;
}

Expand All @@ -65,4 +69,17 @@ public static function createFromPathName(string $pathName):?Config {
$parser = new IniParser($pathName);
return $parser->parse();
}

protected static function withProductionFlag(Config $config):Config {
$appSection = $config->getSection("app");
if($appSection && $appSection->contains("production")) {
return $config;
}

return $config->withMerge(new Config(
new ConfigSection("app", [
"production" => "true",
])
));
}
}
22 changes: 22 additions & 0 deletions test/phpunit/ConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testCreateForProject():void {
self::assertEquals("this appears by default", $config->get("block1.value.existsByDefault"));
self::assertEquals("my.production.database", $config->get("database.host"));
self::assertEquals("example", $config->get("database.schema"));
self::assertTrue($config->isProduction());
}

public function testCreateFromPathName():void {
Expand Down Expand Up @@ -83,4 +84,25 @@ public function testCreateForProjectPreservesFalseyOverrides():void {
self::assertSame(0, $config->getInt("session.max_age"));
self::assertSame("", $config->getString("session.label"));
}

public function testCreateForProjectProductionFileDoesNotOverrideExplicitFlag():void {
$filePath = implode(DIRECTORY_SEPARATOR, [
$this->tmp,
"config.ini",
]);
$filePathProduction = implode(DIRECTORY_SEPARATOR, [
$this->tmp,
"config.production.ini",
]);

file_put_contents($filePath, implode(PHP_EOL, [
"[app]",
"production=false",
"",
]));
file_put_contents($filePathProduction, Helper::INI_OVERRIDE_PROD);

$config = ConfigFactory::createForProject($this->tmp);
self::assertFalse($config->isProduction());
}
}
20 changes: 20 additions & 0 deletions test/phpunit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ public function testTypeSafeGetter() {
self::assertNull($sut->getFloat("nothing-here"));
}

public function testIsProductionDefaultsToFalse():void {
putenv("app_production");
$config = new Config();
self::assertFalse($config->isProduction());
}

public function testIsProductionUsesAppProductionValue():void {
putenv("app_production");
$config = new Config(
new ConfigSection("app", [
"production" => "0",
])
);
self::assertFalse($config->isProduction());

putenv("app_production=true");
self::assertTrue($config->isProduction());
putenv("app_production");
}

public function testWithMergeReturnsNewAndDoesNotMutateOriginal():void {
$original = new Config(
new ConfigSection("app", [
Expand Down
Loading