Skip to content

Commit

Permalink
Config file version (#69)
Browse files Browse the repository at this point in the history
* Add config_version to config file;

* Throw exception if published config version in outdated;

* Add tests;

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
hans-thomas and StyleCIBot authored Jan 9, 2025
1 parent 451784a commit a5a8d49
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
11 changes: 11 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,15 @@
'migrations' => [
database_path('migrations'),
],

/*
|--------------------------------------------------------------------------
| Configuration version
|--------------------------------------------------------------------------
|
| Every update in config file will increase the config version. It will be
| used to inform devs to update their published config file.
|
*/
'config_version' => '1.0.0',
];
15 changes: 15 additions & 0 deletions src/Exceptions/Package/PublishedVersionOutDatedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Hans\Valravn\Exceptions\Package;

use Hans\Valravn\Exceptions\VException;

class PublishedVersionOutDatedException extends VException
{
protected string $errorCodePrefix = 'ValravnECx';

public function __construct()
{
parent::__construct('Published config file is outdated! please republish it.', 3);
}
}
15 changes: 14 additions & 1 deletion src/ValravnServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Hans\Valravn\Commands\Requests;
use Hans\Valravn\Commands\Resources;
use Hans\Valravn\Commands\Service;
use Hans\Valravn\Exceptions\Package\PublishedVersionOutDatedException;
use Hans\Valravn\Services\Caching\CachingService;
use Hans\Valravn\Services\Filtering\FilteringService;
use Hans\Valravn\Services\Routing\RoutingService;
Expand All @@ -25,6 +26,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Throwable;

class ValravnServiceProvider extends ServiceProvider
{
Expand All @@ -42,11 +44,22 @@ public function register()
/**
* Bootstrap any application services.
*
* @throws Throwable
*
* @return void
*/
public function boot()
{
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'valravn');
$configFile = __DIR__.'/../config/config.php';
$config = require $configFile;
if ($publishedConfigVersion = config('valravn.config_version', false)) {
throw_if(
version_compare($config['config_version'], $publishedConfigVersion, '>'),
new PublishedVersionOutDatedException()
);
}

$this->mergeConfigFrom($configFile, 'valravn');

$this->registerMacros();
if ($this->app->runningInConsole()) {
Expand Down
60 changes: 60 additions & 0 deletions tests/Feature/ValravnServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Hans\Valravn\Tests\Feature;

use Hans\Valravn\Exceptions\Package\PublishedVersionOutDatedException;
use Hans\Valravn\Tests\TestCase;
use Hans\Valravn\ValravnServiceProvider;
use Illuminate\Support\Facades\File;
use PHPUnit\Framework\Attributes\Test;

class ValravnServiceProviderTest extends TestCase
{
protected function tearDown(): void
{
$configFile = __DIR__.'/../../config/config.php';
$publishedVersion = config('valravn.config_version');
$newVersion = str_split($publishedVersion, strrpos($publishedVersion, '.'))[0].'.999';

// revert 'config_version' key
$configContent = file_get_contents($configFile);
$configContent = str_replace($newVersion, $publishedVersion, $configContent);
file_put_contents($configFile, $configContent);

File::delete(base_path('config/valravn.php'));
self::assertFileDoesNotExist(base_path('config/valravn.php'));

parent::tearDown();
}

#[Test]
public function publishedConfigFileVersion()
{
$publishedVersion = config('valravn.config_version');
$configVersion = require __DIR__.'/../../config/config.php';
$configVersion = $configVersion['config_version'];

self::assertGreaterThanOrEqual($configVersion, $publishedVersion);
}

#[Test]
public function publishedConfigFileVersionIsOutdated()
{
$this->artisan('vendor:publish', ['--tag' => 'valravn-config']);
self::assertFileExists(base_path('config/valravn.php'));

$configFile = __DIR__.'/../../config/config.php';
$publishedVersion = config('valravn.config_version');

$newVersion = str_split($publishedVersion, strrpos($publishedVersion, '.'))[0].'.999';
// change 'config_version' key
$newConfigContent = file_get_contents($configFile);
$newConfigContent = str_replace($publishedVersion, $newVersion, $newConfigContent);
file_put_contents($configFile, $newConfigContent);

$this->expectException(PublishedVersionOutDatedException::class);

$provider = new ValravnServiceProvider($this->app);
$provider->boot();
}
}

0 comments on commit a5a8d49

Please sign in to comment.