-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #449 from hydephp/441-add-a-yaml-configuration-file
Add a YAML configuration file hydephp/develop@74353f0
- Loading branch information
github-actions
committed
Aug 28, 2022
1 parent
2b38bc4
commit ee14bd6
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Services; | ||
|
||
use Hyde\Framework\Hyde; | ||
use Illuminate\Support\Facades\Config; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
/** | ||
* @see \Hyde\Framework\Testing\Feature\YamlConfigurationServiceTest | ||
*/ | ||
class YamlConfigurationService | ||
{ | ||
public static function boot(): void | ||
{ | ||
if (static::hasFile()) { | ||
Config::set('site', array_merge( | ||
Config::get('site', []), | ||
static::getYaml() | ||
)); | ||
} | ||
} | ||
|
||
public static function hasFile(): bool | ||
{ | ||
return file_exists(Hyde::path('hyde.yml')) | ||
|| file_exists(Hyde::path('hyde.yaml')); | ||
} | ||
|
||
protected static function getFile(): string | ||
{ | ||
return file_exists(Hyde::path('hyde.yml')) | ||
? Hyde::path('hyde.yml') | ||
: Hyde::path('hyde.yaml'); | ||
} | ||
|
||
protected static function getYaml(): array | ||
{ | ||
$yaml = Yaml::parse(file_get_contents(static::getFile())); | ||
|
||
return is_array($yaml) ? $yaml : []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Testing\Feature; | ||
|
||
use Hyde\Framework\Services\YamlConfigurationService; | ||
use Hyde\Testing\TestCase; | ||
use Illuminate\Support\Facades\Config; | ||
|
||
/** | ||
* @covers \Hyde\Framework\Services\YamlConfigurationService | ||
* | ||
* @see \Hyde\Framework\Testing\Unit\HydeServiceProviderTest as it determines if this service should be booted. | ||
*/ | ||
class YamlConfigurationServiceTest extends TestCase | ||
{ | ||
public function test_changes_in_yaml_file_override_changes_in_site_config() | ||
{ | ||
$this->assertEquals('HydePHP', Config::get('site.name')); | ||
$this->file('hyde.yml', 'name: Foo'); | ||
YamlConfigurationService::boot(); | ||
$this->assertEquals('Foo', Config::get('site.name')); | ||
} | ||
|
||
public function test_changes_in_yaml_file_override_changes_in_site_config_when_using_yaml_extension() | ||
{ | ||
$this->assertEquals('HydePHP', Config::get('site.name')); | ||
$this->file('hyde.yaml', 'name: Foo'); | ||
YamlConfigurationService::boot(); | ||
$this->assertEquals('Foo', Config::get('site.name')); | ||
} | ||
|
||
public function test_service_gracefully_handles_missing_file() | ||
{ | ||
YamlConfigurationService::boot(); | ||
$this->assertEquals('HydePHP', Config::get('site.name')); | ||
} | ||
|
||
public function test_service_gracefully_handles_empty_file() | ||
{ | ||
$this->file('hyde.yml', ''); | ||
YamlConfigurationService::boot(); | ||
$this->assertEquals('HydePHP', Config::get('site.name')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters