Skip to content

Commit

Permalink
Merge pull request #270 from hydephp/218-add-config-version-property-…
Browse files Browse the repository at this point in the history
…so-that-a-warning-can-be-sent-if-a-config-file-is-outdated

Send a non-intrusive warning when the config file is out of date
  • Loading branch information
caendesilva authored May 3, 2022
2 parents 66d1782 + 44d78a8 commit b3ee7b0
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,18 @@
'maxHeadingLevel' => 4,
'smoothPageScrolling' => true,
],

/*
|--------------------------------------------------------------------------
| Hyde Config Version @HydeConfigVersion 1.0.0
|--------------------------------------------------------------------------
|
| Hyde can use the value above to determine if this configuration file
| contains the latest config options. If your config needs updating,
| a message will be shown in the HydeCLI, unless disabled below.
|
*/

'warnAboutOutdatedConfig' => true,

];
34 changes: 34 additions & 0 deletions src/Actions/ChecksIfConfigIsUpToDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Hyde\Framework\Actions;

use Hyde\Framework\Contracts\ActionContract;
use Hyde\Framework\Hyde;

/**
* Checks if the installed config is up-to-date with the Framework's config.
* Works by comparing the number of title blocks, which is a crude but fast way to check.
*
* @see \Tests\Feature\Actions\ChecksIfConfigIsUpToDateTest
*/
class ChecksIfConfigIsUpToDate implements ActionContract
{
public string $hydeConfig;
public string $frameworkConfig;

public function __construct()
{
$this->hydeConfig = file_get_contents(Hyde::path('config/hyde.php'));
$this->frameworkConfig = file_get_contents(Hyde::vendorPath('config/hyde.php'));
}

public function execute(): bool
{
return $this->findOptions($this->hydeConfig) === $this->findOptions($this->frameworkConfig);
}

public function findOptions(string $config): int
{
return substr_count($config, '--------------------------------------------------------------------------');
}
}
18 changes: 18 additions & 0 deletions src/Commands/HydeUpdateConfigsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hyde\Framework\Commands;

use Hyde\Framework\Actions\ChecksIfConfigIsUpToDate;
use Hyde\Framework\Hyde;
use Illuminate\Support\Facades\File;
use LaravelZero\Framework\Commands\Command;
Expand All @@ -16,6 +17,18 @@ class HydeUpdateConfigsCommand extends Command
protected $signature = 'update:configs';
protected $description = 'Publish the default configuration files';

public function __construct()
{
parent::__construct();

if ($this->checkIfConfigIsOutOfDate() && config('hyde.warnAboutOutdatedConfig', true)) {
$this->setDescription(
'<comment>⚠ Your configuration may be out of date. </comment>'.
'Run this command to update them.'
);
}
}

public function handle(): int
{
File::copyDirectory(Hyde::vendorPath('config'), Hyde::path('config'));
Expand All @@ -24,4 +37,9 @@ public function handle(): int

return 0;
}

protected function checkIfConfigIsOutOfDate(): bool
{
return ! (new ChecksIfConfigIsUpToDate)->execute();
}
}
53 changes: 53 additions & 0 deletions tests/Feature/Actions/ChecksIfConfigIsUpToDateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Tests\Feature\Actions;

use Hyde\Framework\Actions\ChecksIfConfigIsUpToDate;
use Tests\TestCase;

/**
* @covers \Hyde\Framework\Actions\ChecksIfConfigIsUpToDate
*/
class ChecksIfConfigIsUpToDateTest extends TestCase
{
public function test_it_returns_true_if_config_is_up_to_date()
{
$action = new ChecksIfConfigIsUpToDate();

$action->hydeConfig = $this->makeConfig();
$action->frameworkConfig = $this->makeConfig();

$this->assertTrue($action->execute());
}

public function test_it_returns_false_if_config_is_not_up_to_date()
{
$action = new ChecksIfConfigIsUpToDate();

$action->hydeConfig = $this->makeConfig();
$action->frameworkConfig = 'foo';

$this->assertFalse($action->execute());
}

protected function makeConfig(): string
{
return <<<'EOF'
<?php return [
/*
|--------------------------------------------------------------------------
| Foo Bar
|--------------------------------------------------------------------------
|
*/
/*
|--------------------------------------------------------------------------
| Second Option
|--------------------------------------------------------------------------
|
*/
];
EOF;
}
}

0 comments on commit b3ee7b0

Please sign in to comment.