Skip to content

Commit

Permalink
Allow for remote config files (#201)
Browse files Browse the repository at this point in the history
* Allow for remote config files

Implemented a driver for fetching remote configs and also added reworked the current logic to also work as a driver

Fixes #200

* Revert "Allow for remote config files"

This reverts commit ae01069.

* Simplified version of fetching remote configs

* fix codestyle

* Make fileExits method protected

* Use the path param instead of class path property

* formatting

* remove type hint

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
kevinpijning and taylorotwell authored Jul 27, 2023
1 parent 8254c5d commit 2fb8cd1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/Repositories/ConfigurationJsonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function preset()
*/
protected function get()
{
if (file_exists((string) $this->path)) {
if ($this->fileExists((string) $this->path)) {
return tap(json_decode(file_get_contents($this->path), true), function ($configuration) {
if (! is_array($configuration)) {
abort(1, sprintf('The configuration file [%s] is not valid JSON.', $this->path));
Expand All @@ -86,4 +86,17 @@ protected function get()

return [];
}

/**
* Determine if a local or remote file exists.
*
* @return bool
*/
protected function fileExists(string $path)
{
return match (true) {
str_starts_with($path, 'http://') || str_starts_with($path, 'https://') => str_contains(get_headers($path)[0], '200 OK'),
default => file_exists($path)
};
}
}
8 changes: 8 additions & 0 deletions tests/Unit/Repositories/ConfigurationJsonRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
->and($repository->rules())->toBeEmpty();
});

it('works with a remote json file', function () {
$repository = new ConfigurationJsonRepository('https://raw.githubusercontent.com/laravel/pint/main/tests/Fixtures/rules/pint.json', 'psr12');

expect($repository->rules())->toBe([
'no_unused_imports' => false,
]);
});

it('may have rules options', function () {
$repository = new ConfigurationJsonRepository(dirname(__DIR__, 2).'/Fixtures/rules/pint.json', 'psr12');

Expand Down

0 comments on commit 2fb8cd1

Please sign in to comment.