Skip to content

Commit

Permalink
Refactor component registration
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Jun 27, 2020
1 parent 8721ea6 commit e7c20c7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/BladeIconsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function register(): void

public function boot(): void
{
$this->bootComponents();
$this->bootDirectives();
$this->bootPublishing();
}
Expand All @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/ComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e7c20c7

Please sign in to comment.