From e7c20c730ba6bb929cbe246cfca7aea0834742af Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Sat, 27 Jun 2020 17:52:30 +0200 Subject: [PATCH] Refactor component registration --- src/BladeIconsServiceProvider.php | 8 ++++++++ src/Factory.php | 22 +++++++++++----------- tests/ComponentsTest.php | 4 +++- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/BladeIconsServiceProvider.php b/src/BladeIconsServiceProvider.php index 92c4aa6..0a5809a 100644 --- a/src/BladeIconsServiceProvider.php +++ b/src/BladeIconsServiceProvider.php @@ -19,6 +19,7 @@ public function register(): void public function boot(): void { + $this->bootComponents(); $this->bootDirectives(); $this->bootPublishing(); } @@ -45,6 +46,13 @@ private function registerFactory(): void }); } + private function bootComponents(): void + { + $this->callAfterResolving('view', function ($view, Application $app) { + $app->make(Factory::class)->registerComponents(); + }); + } + private function bootDirectives(): void { Blade::directive('svg', function ($expression) { diff --git a/src/Factory.php b/src/Factory.php index d868f1e..7e354c0 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -60,23 +60,23 @@ public function add(string $set, array $options): self $this->sets[$set] = $options; - $this->registerComponents($options); - $this->cache = []; return $this; } - private function registerComponents(array $options): void + public function registerComponents(): void { - foreach ($this->filesystem->allFiles($options['path']) as $file) { - $path = array_filter(explode('/', Str::after($file->getPath(), $options['path']))); - - Blade::component( - SvgComponent::class, - implode('.', array_filter($path + [$file->getFilenameWithoutExtension()])), - $options['prefix'] - ); + foreach ($this->sets as $set) { + foreach ($this->filesystem->allFiles($set['path']) as $file) { + $path = array_filter(explode('/', Str::after($file->getPath(), $set['path']))); + + Blade::component( + SvgComponent::class, + implode('.', array_filter($path + [$file->getFilenameWithoutExtension()])), + $set['prefix'] + ); + } } } diff --git a/tests/ComponentsTest.php b/tests/ComponentsTest.php index 64e0653..8ff33d3 100644 --- a/tests/ComponentsTest.php +++ b/tests/ComponentsTest.php @@ -13,7 +13,9 @@ class ComponentsTest extends TestCase /** @test */ public function components_are_registered_with_their_subdirectories() { - $this->prepareSets(); + $factory = $this->prepareSets(); + + $factory->registerComponents(); $this->assertSame([ 'icon-camera' => Svg::class,