Skip to content

Commit

Permalink
Merge pull request #198 from spatie/features/structure-discoverer
Browse files Browse the repository at this point in the history
Add support for structure discoverer
  • Loading branch information
rubenvanassche authored Feb 10, 2023
2 parents f4d282f + 6b47085 commit e594ac0
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 194 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
coverage: none

- name: Install composer dependencies
Expand Down
14 changes: 3 additions & 11 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,14 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 8.2, 8.1, 8.0]
laravel: [ 10.*, 9.*, 8.* ]
php: [ 8.2, 8.1]
laravel: [ 10.*, 9.*]
dependency-version: [ prefer-stable ]
include:
- laravel: 8.*
testbench: ^6.23
larastan: ^1.0
- laravel: 9.*
testbench: 7.*
larastan: ^1.0
- laravel: 10.*
testbench: 8.*
larastan: ^2.0
exclude:
- laravel: 10.*
php: 8.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

Expand All @@ -49,7 +41,7 @@ jobs:

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nunomaduro/larastan:${{ matrix.larastan }}" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute tests
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@
}
],
"require" : {
"php" : "^7.4|^8.0",
"php" : "^8.1",
"ext-json" : "*",
"illuminate/database" : "^8.73|^9.0|^10.0",
"illuminate/database" : "^9.0|^10.0",
"doctrine/dbal" : "^2.13|^3.2",
"phpdocumentor/type-resolver" : "^1.5",
"spatie/temporary-directory" : "^1.3|^2.0"
"spatie/temporary-directory" : "^1.3|^2.0",
"spatie/php-structure-discoverer" : "^1.0.1"
},
"require-dev" : {
"ext-redis": "*",
"mockery/mockery": "^1.4",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^6.23|^7.0|^8.0",
"orchestra/testbench": "^7.0|^8.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.2",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5",
"spatie/laravel-data": "^1.0.0|^2.0.0",
"spatie/laravel-data": "^1.0|^2.0|^3.0",
"spatie/pest-plugin-snapshots": "^1.1",
"spatie/phpunit-snapshot-assertions": "^4.2",
"spatie/ray": "^1.36"
Expand Down
17 changes: 3 additions & 14 deletions src/Console/CacheDiscoveredSettingsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Spatie\LaravelSettings\SettingsContainer;
use Spatie\LaravelSettings\Support\SettingsStructureScout;

class CacheDiscoveredSettingsCommand extends Command
{
Expand All @@ -17,19 +17,8 @@ public function handle(SettingsContainer $container, Filesystem $files): void
{
$this->info('Caching registered settings...');

$container
->clearCache()
->getSettingClasses()
->pipe(function (Collection $settingClasses) use ($files) {
$cachePath = config('settings.discovered_settings_cache_path');

$files->makeDirectory($cachePath, 0755, true, true);

$files->put(
$cachePath . '/settings.php',
'<?php return ' . var_export($settingClasses->toArray(), true) . ';'
);
});
SettingsStructureScout::create()->clear();
SettingsStructureScout::create()->cache();

$this->info('All done!');
}
Expand Down
3 changes: 2 additions & 1 deletion src/Console/ClearDiscoveredSettingsCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Spatie\LaravelSettings\Support\SettingsStructureScout;

class ClearDiscoveredSettingsCacheCommand extends Command
{
Expand All @@ -13,7 +14,7 @@ class ClearDiscoveredSettingsCacheCommand extends Command

public function handle(Filesystem $files): void
{
$files->delete(config('settings.discovered_settings_cache_path') . '/settings.php');
SettingsStructureScout::create()->clear();

$this->info('Cached discovered settings cleared!');
}
Expand Down
4 changes: 4 additions & 0 deletions src/LaravelSettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Spatie\LaravelSettings\Migrations\SettingsMigration;
use Spatie\LaravelSettings\SettingsRepositories\SettingsRepository;
use Spatie\LaravelSettings\Support\SettingsCacheFactory;
use Spatie\LaravelSettings\Support\SettingsStructureScout;
use Spatie\StructureDiscoverer\Support\StructureScoutManager;
use SplFileInfo;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -44,6 +46,8 @@ public function boot()
Event::subscribe(SettingsEventSubscriber::class);
Event::listen(SchemaLoaded::class, fn ($event) => $this->removeMigrationsWhenSchemaLoaded($event));

StructureScoutManager::add(SettingsStructureScout::class);

$this->loadMigrationsFrom($this->resolveMigrationPaths());
}

Expand Down
22 changes: 11 additions & 11 deletions src/Migrations/SettingsMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Spatie\LaravelSettings\Migrations;

use Closure;
use Illuminate\Support\Collection;
use Illuminate\Support\Arr;
use Spatie\LaravelSettings\Exceptions\InvalidSettingName;
use Spatie\LaravelSettings\Exceptions\SettingAlreadyExists;
use Spatie\LaravelSettings\Exceptions\SettingDoesNotExist;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function deleteIfExists(string $property): void
$this->deleteProperty($property);
}
}

public function update(string $property, Closure $closure, bool $encrypted = false): void
{
if (! $this->checkIfPropertyExists($property)) {
Expand Down Expand Up @@ -177,15 +177,15 @@ protected function deleteProperty(string $property): void

protected function getCast(string $group, string $name): ?SettingsCast
{
return optional($this->settingsGroups()->get($group))->getCast($name);
}
$settingsClass = Arr::first(
app(SettingsContainer::class)->getSettingClasses(),
fn (string $settingsClass) => $settingsClass::group() === $group
);

protected function settingsGroups(): Collection
{
return app(SettingsContainer::class)
->getSettingClasses()
->mapWithKeys(fn (string $settingsClass) => [
$settingsClass::group() => new SettingsConfig($settingsClass),
]);
if ($settingsClass === null) {
return null;
}

return (new SettingsConfig($settingsClass))->getCast($name);
}
}
23 changes: 3 additions & 20 deletions src/SettingsContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Spatie\LaravelSettings\Exceptions\CouldNotUnserializeSettings;
use Spatie\LaravelSettings\Support\Composer;
use Spatie\LaravelSettings\Support\DiscoverSettings;
use Spatie\LaravelSettings\Support\SettingsCacheFactory;
use Spatie\LaravelSettings\Support\SettingsStructureScout;

class SettingsContainer
{
Expand Down Expand Up @@ -48,17 +47,10 @@ public function getSettingClasses(): Collection
return self::$settingsClasses;
}

$cachedDiscoveredSettings = config('settings.discovered_settings_cache_path') . '/settings.php';
$structureScout = SettingsStructureScout::create();

if (file_exists($cachedDiscoveredSettings)) {
$classes = require $cachedDiscoveredSettings;

return self::$settingsClasses = collect($classes);
}

/** @var \Spatie\LaravelSettings\Settings[] $settings */
$settings = array_merge(
$this->discoverSettings(),
$structureScout->get(),
config('settings.settings', [])
);

Expand All @@ -71,13 +63,4 @@ public function clearCache(): self

return $this;
}

protected function discoverSettings(): array
{
return (new DiscoverSettings())
->within(config('settings.auto_discover_settings', []))
->useBasePath(base_path())
->ignoringFiles(Composer::getAutoloadedFiles(base_path('composer.json')))
->discover();
}
}
26 changes: 0 additions & 26 deletions src/Support/Composer.php

This file was deleted.

90 changes: 0 additions & 90 deletions src/Support/DiscoverSettings.php

This file was deleted.

33 changes: 33 additions & 0 deletions src/Support/SettingsStructureScout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Spatie\LaravelSettings\Support;

use Spatie\LaravelSettings\Settings;
use Spatie\StructureDiscoverer\Cache\DiscoverCacheDriver;
use Spatie\StructureDiscoverer\Cache\FileDiscoverCacheDriver;
use Spatie\StructureDiscoverer\Discover;
use Spatie\StructureDiscoverer\StructureScout;

class SettingsStructureScout extends StructureScout
{
public function identifier(): string
{
return "laravel-settings";
}

protected function definition(): Discover
{
return Discover::in(...config('settings.auto_discover_settings', []))
->classes()
->extending(Settings::class);
}

public function cacheDriver(): DiscoverCacheDriver
{
return new FileDiscoverCacheDriver(
config('settings.discovered_settings_cache_path'),
serialize: false,
filename: 'settings.php'
);
}
}
Loading

0 comments on commit e594ac0

Please sign in to comment.