Skip to content

Commit

Permalink
Merge pull request #449 from hydephp/441-add-a-yaml-configuration-file
Browse files Browse the repository at this point in the history
Add a YAML configuration file hydephp/develop@74353f0
  • Loading branch information
github-actions committed Aug 28, 2022
1 parent 2b38bc4 commit ee14bd6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
| no configuration out of the box to get started. Though, you may want to
| change the options to personalize your site and make it your own!
|
| Tip: The settings here can also be overridden by creating a hyde.yml file
| in the root of your project directory. Note that these cannot reference
| environment variables, and their values override any made here.
|
*/

return [
Expand Down
10 changes: 10 additions & 0 deletions src/HydeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Services\AssetService;
use Hyde\Framework\Services\YamlConfigurationService;
use Hyde\Framework\Views\Components\LinkComponent;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
Expand All @@ -26,6 +27,8 @@ class HydeServiceProvider extends ServiceProvider
*/
public function register(): void
{
$this->initializeConfiguration();

$this->app->singleton(AssetService::class, AssetService::class);

$this->registerSourceDirectories([
Expand Down Expand Up @@ -86,6 +89,13 @@ public function boot(): void
HydeKernel::getInstance()->boot();
}

protected function initializeConfiguration()
{
if (YamlConfigurationService::hasFile()) {
YamlConfigurationService::boot();
}
}

/**
* Register the HydeCLI console commands.
*/
Expand Down
43 changes: 43 additions & 0 deletions src/Services/YamlConfigurationService.php
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 : [];
}
}
44 changes: 44 additions & 0 deletions tests/Feature/YamlConfigurationServiceTest.php
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'));
}
}
8 changes: 8 additions & 0 deletions tests/Unit/HydeServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public function test_provider_has_boot_method()
$this->assertTrue(method_exists($this->provider, 'boot'));
}

public function test_provider_applies_yaml_configuration_when_present()
{
$this->assertEquals('HydePHP', config('site.name'));
$this->file('hyde.yml', 'name: Foo');
$this->provider->register();
$this->assertEquals('Foo', config('site.name'));
}

public function test_provider_registers_asset_service_contract()
{
$this->assertTrue($this->app->bound(AssetService::class));
Expand Down

0 comments on commit ee14bd6

Please sign in to comment.