From 112ad46f216c339a4a049aaf9e3248e4043ef3bd Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Wed, 4 Sep 2019 12:58:24 +0200 Subject: [PATCH] prefix single components and collections - allow to remove namespace (#84) * unify/improve component directories * allow to register multiple components and prefix them separately allow to prefix single components * fix and add unittests * improve bladex facade * replace registerComponents by components method flag registerComponents as internal * allow to register components by list of paths * fix php cs --- src/BladeX.php | 87 ++++-- src/Compiler.php | 17 +- src/Component.php | 67 ++++- src/ComponentCollection.php | 25 ++ src/ComponentDirectory/ComponentDirectory.php | 5 +- .../NamespacedDirectory.php | 15 +- src/ComponentDirectory/RegularDirectory.php | 11 - src/Exceptions/CouldNotRegisterComponent.php | 5 + src/Facades/BladeX.php | 10 +- .../Registration/RegistrationTest.php | 269 +++++++++++++++++- 10 files changed, 431 insertions(+), 80 deletions(-) create mode 100644 src/ComponentCollection.php diff --git a/src/BladeX.php b/src/BladeX.php index a0a4013..fe76a0b 100755 --- a/src/BladeX.php +++ b/src/BladeX.php @@ -18,23 +18,21 @@ class BladeX protected $prefix = ''; /** - * @param string|array $view + * @param string|string[] $view * @param string $tag * * @return null|\Spatie\BladeX\Component */ public function component($view, string $tag = ''): ?Component { - if (is_array($view)) { - foreach ($view as $singleView) { - $this->component($singleView); - } + if (is_iterable($view)) { + $this->registerViews($view); return null; } if ($view instanceof Component) { - $this->registeredComponents[$view->tag] = $view; + $this->registeredComponents[] = $view; return $view; } @@ -55,14 +53,43 @@ public function component($view, string $tag = ''): ?Component $component = new Component($view, $tag); - $this->registeredComponents[$component->tag] = $component; + $this->registeredComponents[] = $component; return $component; } + /** + * @param string|string[] $viewDirectory + * + * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[] + */ + public function components($viewDirectory): ComponentCollection + { + if (is_iterable($viewDirectory)) { + $components = new ComponentCollection(); + + foreach ($viewDirectory as $singleViewDirectory) { + if (Str::endsWith($singleViewDirectory, '*')) { + $components = $components->merge($this->registerComponents($singleViewDirectory)); + } else { + $components->push($this->component($singleViewDirectory)); + } + } + + return $components; + } + + return $this->registerComponents($viewDirectory); + } + + /** + * @return \Spatie\BladeX\Component[] + */ public function registeredComponents(): array { - return array_values($this->registeredComponents); + return collect($this->registeredComponents)->reverse()->unique(function (Component $component) { + return $component->getTag(); + })->reverse()->values()->all(); } public function prefix(string $prefix = ''): self @@ -77,21 +104,43 @@ public function getPrefix(): string return empty($this->prefix) ? '' : Str::finish($this->prefix, '-'); } - public function registerComponents(string $viewDirectory) + /** + * @internal + * + * @param string $viewDirectory + * + * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[] + */ + public function registerComponents(string $viewDirectory): ComponentCollection { + if (! Str::endsWith($viewDirectory, '*')) { + throw CouldNotRegisterComponent::viewDirectoryWithoutWildcard($viewDirectory); + } + $componentDirectory = Str::contains($viewDirectory, '::') ? new NamespacedDirectory($viewDirectory) : new RegularDirectory($viewDirectory); - collect(File::files($componentDirectory->getAbsoluteDirectory())) - ->filter(function (SplFileInfo $file) { - return Str::endsWith($file->getFilename(), '.blade.php'); - }) - ->map(function (SplFileInfo $file) use ($componentDirectory) { - return $componentDirectory->getViewName($file); - }) - ->each(function (string $viewName) { - $this->component($viewName); - }); + return $this->registerViews( + ComponentCollection::make(File::files($componentDirectory->getAbsoluteDirectory())) + ->filter(function (SplFileInfo $file) { + return Str::endsWith($file->getFilename(), '.blade.php'); + }) + ->map(function (SplFileInfo $file) use ($componentDirectory) { + return $componentDirectory->getViewName($file); + }) + ); + } + + /** + * @param iterable|string[] $views + * + * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[] + */ + protected function registerViews(iterable $views): ComponentCollection + { + return ComponentCollection::make($views)->map(function (string $viewName) { + return $this->component($viewName); + }); } } diff --git a/src/Compiler.php b/src/Compiler.php index 288b4d6..2ad7017 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -38,9 +38,7 @@ protected function parseComponentHtml(string $viewContents, Component $component protected function parseSelfClosingTags(string $viewContents, Component $component): string { - $prefix = $this->bladeX->getPrefix(); - - $pattern = "/<\s*{$prefix}{$component->tag}\s*(?(?:\s+[\w\-:]+(=(?:\\\"[^\\\"]+\\\"|\'[^\']+\'|[^\'\\\"=<>]+))?)*\s*)\/>/"; + $pattern = "/<\s*{$component->getTag()}\s*(?(?:\s+[\w\-:]+(=(?:\\\"[^\\\"]+\\\"|\'[^\']+\'|[^\'\\\"=<>]+))?)*\s*)\/>/"; return preg_replace_callback($pattern, function (array $matches) use ($component) { $attributes = $this->getAttributesFromAttributeString($matches['attributes']); @@ -51,9 +49,7 @@ protected function parseSelfClosingTags(string $viewContents, Component $compone protected function parseOpeningTags(string $viewContents, Component $component): string { - $prefix = $this->bladeX->getPrefix(); - - $pattern = "/<\s*{$prefix}{$component->tag}(?(?:\s+[\w\-:]+(=(?:\\\"[^\\\"]*\\\"|\'[^\']*\'|[^\'\\\"=<>]+))?)*\s*)(?/"; + $pattern = "/<\s*{$component->getTag()}(?(?:\s+[\w\-:]+(=(?:\\\"[^\\\"]*\\\"|\'[^\']*\'|[^\'\\\"=<>]+))?)*\s*)(?/"; return preg_replace_callback($pattern, function (array $matches) use ($component) { $attributes = $this->getAttributesFromAttributeString($matches['attributes']); @@ -64,9 +60,7 @@ protected function parseOpeningTags(string $viewContents, Component $component): protected function parseClosingTags(string $viewContents, Component $component): string { - $prefix = $this->bladeX->getPrefix(); - - $pattern = "/<\/\s*{$prefix}{$component->tag}\s*>/"; + $pattern = "/<\/\s*{$component->getTag()}\s*>/"; return preg_replace($pattern, $this->componentEndString($component), $viewContents); } @@ -164,11 +158,6 @@ protected function parseSlots(string $viewContents): string return $viewContents; } - protected function isOpeningHtmlTag(string $tagName, string $html): bool - { - return ! Str::endsWith($html, ["", '/>']); - } - protected function parseBindAttributes(string $attributeString): string { return preg_replace("/\s*:([\w-]+)=/m", ' bind:$1=', $attributeString); diff --git a/src/Component.php b/src/Component.php index 17edf5a..8ae3d41 100644 --- a/src/Component.php +++ b/src/Component.php @@ -2,36 +2,50 @@ namespace Spatie\BladeX; -use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Contracts\Support\Arrayable; use Spatie\BladeX\Exceptions\CouldNotRegisterComponent; class Component { + /** @var BladeX */ + protected $bladeX; + /** @var string */ public $view; - /** @var string */ + /** + * @var string + * @internal + * @see Component::getTag() + */ public $tag; /** @var string */ public $viewModel; - public static function make(string $view, string $tag = '') + /** @var string */ + protected $prefix; + + /** @var bool */ + protected $withNamespace; + + public static function make(string $view, string $tag = '', string $prefix = '', bool $withNamespace = true) { - return new static($view, $tag); + return new static($view, $tag, $prefix, $withNamespace); } - public function __construct(string $view, string $tag = '') + public function __construct(string $view, string $tag = '', string $prefix = '', bool $withNamespace = true) { - if ($tag === '') { - $tag = $this->determineDefaultTag($view); - } - $this->view = $view; $this->tag = $tag; + + $this->prefix = $prefix; + + $this->withNamespace = $withNamespace; + + $this->bladeX = app(BladeX::class); } public function tag(string $tag) @@ -41,6 +55,20 @@ public function tag(string $tag) return $this; } + public function prefix(string $prefix) + { + $this->prefix = $prefix; + + return $this; + } + + public function withoutNamespace() + { + $this->withNamespace = false; + + return $this; + } + public function viewModel(string $viewModel) { if (! class_exists($viewModel)) { @@ -56,17 +84,30 @@ public function viewModel(string $viewModel) return $this; } - protected function determineDefaultTag(string $view): string + public function getTag(): string + { + $tag = empty($this->prefix) ? $this->bladeX->getPrefix() : Str::finish($this->prefix, '-'); + + $tag .= empty($this->tag) ? $this->determineDefaultTag() : $this->tag; + + return $tag; + } + + protected function determineDefaultTag(): string { - $baseComponentName = explode('.', $view); + $baseComponentName = explode('.', $this->view); $tag = Str::kebab(end($baseComponentName)); - if (Str::contains($view, '::') && ! Str::contains($tag, '::')) { - $namespace = Arr::first(explode('::', $view)); + if (Str::contains($this->view, '::') && ! Str::contains($tag, '::')) { + $namespace = Str::before($this->view, '::'); $tag = "{$namespace}::{$tag}"; } + if (! $this->withNamespace && Str::contains($tag, '::')) { + $tag = Str::after($tag, '::'); + } + return $tag; } } diff --git a/src/ComponentCollection.php b/src/ComponentCollection.php new file mode 100644 index 0000000..9c3df48 --- /dev/null +++ b/src/ComponentCollection.php @@ -0,0 +1,25 @@ +each->prefix($prefix); + + return $this; + } + + public function withoutNamespace() + { + $this->each->withoutNamespace(); + + return $this; + } +} diff --git a/src/ComponentDirectory/ComponentDirectory.php b/src/ComponentDirectory/ComponentDirectory.php index deb3c19..75d0910 100644 --- a/src/ComponentDirectory/ComponentDirectory.php +++ b/src/ComponentDirectory/ComponentDirectory.php @@ -7,12 +7,15 @@ abstract class ComponentDirectory { + /** @var string */ + protected $viewDirectory; + abstract public function getAbsoluteDirectory(): string; public function getViewName(SplFileInfo $viewFile): string { $view = Str::replaceLast('.blade.php', '', $viewFile->getFilename()); - return "{$this->viewDirectory}.{$view}"; + return empty($this->viewDirectory) ? $view : "{$this->viewDirectory}.{$view}"; } } diff --git a/src/ComponentDirectory/NamespacedDirectory.php b/src/ComponentDirectory/NamespacedDirectory.php index 0c23b05..c600623 100644 --- a/src/ComponentDirectory/NamespacedDirectory.php +++ b/src/ComponentDirectory/NamespacedDirectory.php @@ -12,13 +12,10 @@ class NamespacedDirectory extends ComponentDirectory /** @var string */ protected $namespace; - /** @var string */ - protected $viewDirectory; - public function __construct(string $viewDirectory) { [$this->namespace, $viewDirectory] = explode('::', $viewDirectory); - $this->viewDirectory = Str::before($viewDirectory, '*'); + $this->viewDirectory = trim(Str::before($viewDirectory, '*'), '.'); } public function getAbsoluteDirectory(): string @@ -36,14 +33,6 @@ public function getAbsoluteDirectory(): string public function getViewName(SplFileInfo $viewFile): string { - $view = Str::replaceLast('.blade.php', '', $viewFile->getFilename()); - - $viewDirectory = ''; - - if ($this->viewDirectory !== '') { - $viewDirectory = $this->viewDirectory; - } - - return "{$this->namespace}::{$viewDirectory}{$view}"; + return "{$this->namespace}::".parent::getViewName($viewFile); } } diff --git a/src/ComponentDirectory/RegularDirectory.php b/src/ComponentDirectory/RegularDirectory.php index 8284f79..0c562aa 100644 --- a/src/ComponentDirectory/RegularDirectory.php +++ b/src/ComponentDirectory/RegularDirectory.php @@ -4,14 +4,10 @@ use Illuminate\Support\Str; use Illuminate\Support\Facades\View; -use Symfony\Component\Finder\SplFileInfo; use Spatie\BladeX\Exceptions\CouldNotRegisterComponent; class RegularDirectory extends ComponentDirectory { - /** @var string */ - protected $viewDirectory; - public function __construct(string $viewDirectory) { $this->viewDirectory = Str::before($viewDirectory, '.*'); @@ -34,11 +30,4 @@ public function getAbsoluteDirectory(): string return $absoluteDirectory; } - - public function getViewName(SplFileInfo $viewFile): string - { - $view = Str::replaceLast('.blade.php', '', $viewFile->getFilename()); - - return "{$this->viewDirectory}.{$view}"; - } } diff --git a/src/Exceptions/CouldNotRegisterComponent.php b/src/Exceptions/CouldNotRegisterComponent.php index a7e1aa1..231372c 100644 --- a/src/Exceptions/CouldNotRegisterComponent.php +++ b/src/Exceptions/CouldNotRegisterComponent.php @@ -31,4 +31,9 @@ public static function viewModelNotArrayable(string $componentName, string $view { return new static("Could not register component `{$componentName}` because the view model class `{$viewModelClass}` does not implement `".Arrayable::class.'`.'); } + + public static function viewDirectoryWithoutWildcard(string $viewDirectory) + { + return new static("Could not register components because the view directory `{$viewDirectory}` does not end with a wildcard."); + } } diff --git a/src/Facades/BladeX.php b/src/Facades/BladeX.php index d294a8f..680b4c8 100644 --- a/src/Facades/BladeX.php +++ b/src/Facades/BladeX.php @@ -4,7 +4,15 @@ use Illuminate\Support\Facades\Facade; -/** @see \Spatie\BladeX\BladeX */ +/** + * @see \Spatie\BladeX\BladeX + * + * @method static null|\Spatie\BladeX\Component component($view, string $tag = '') + * @method static \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[] components(string|string[] $viewDirectory) + * @method static \Spatie\BladeX\Component[] registeredComponents() + * @method static \Spatie\BladeX\BladeX prefix(string $prefix = '') + * @method static string getPrefix() + */ class BladeX extends Facade { protected static function getFacadeAccessor() diff --git a/tests/Features/Registration/RegistrationTest.php b/tests/Features/Registration/RegistrationTest.php index ea16d1c..2b36340 100644 --- a/tests/Features/Registration/RegistrationTest.php +++ b/tests/Features/Registration/RegistrationTest.php @@ -27,7 +27,7 @@ public function it_can_register_a_single_component_by_only_providing_a_view() $registeredComponents = BladeX::registeredComponents(); $this->assertEquals('components.directoryWithComponents.myView1', $registeredComponents[1]->view); - $this->assertEquals('my-view1', $registeredComponents[1]->tag); + $this->assertEquals('my-view1', $registeredComponents[1]->getTag()); } /** @test */ @@ -39,7 +39,7 @@ public function it_can_register_a_single_component_with_a_custom_tag() $this->assertCount(2, $registeredComponents); $this->assertEquals('components.directoryWithComponents.myView1', $registeredComponents[1]->view); - $this->assertEquals('my-custom-tag', $registeredComponents[1]->tag); + $this->assertEquals('my-custom-tag', $registeredComponents[1]->getTag()); } /** @test */ @@ -53,7 +53,7 @@ public function it_accepts_a_component_instance() $this->assertCount(2, $registeredComponents); $this->assertEquals('components.selectField', $registeredComponents[1]->view); - $this->assertEquals('my-custom-tag', $registeredComponents[1]->tag); + $this->assertEquals('my-custom-tag', $registeredComponents[1]->getTag()); } /** @test */ @@ -84,7 +84,7 @@ public function it_can_register_a_directory_containing_view_components() $registeredComponents = collect(BladeX::registeredComponents()) ->mapWithKeys(function (Component $bladeXComponent) { - return [$bladeXComponent->tag => $bladeXComponent->view]; + return [$bladeXComponent->getTag() => $bladeXComponent->view]; }) ->toArray(); @@ -106,7 +106,7 @@ public function it_can_register_multiple_directories_containing_view_components( $registeredComponents = collect(BladeX::registeredComponents()) ->mapWithKeys(function (Component $bladeXComponent) { - return [$bladeXComponent->tag => $bladeXComponent->view]; + return [$bladeXComponent->getTag() => $bladeXComponent->view]; }) ->toArray(); @@ -145,6 +145,22 @@ public function it_will_throw_an_exception_when_registering_a_directory_that_doe BladeX::component('nonExistingDirectory.*'); } + /** @test */ + public function it_will_throw_an_exception_when_registering_a_namespace_that_does_not_exist() + { + $this->expectException(CouldNotRegisterComponent::class); + + BladeX::component('non-existing-namespace::*'); + } + + /** @test */ + public function it_will_throw_an_exception_when_registering_components_without_wildcard() + { + $this->expectException(CouldNotRegisterComponent::class); + + BladeX::components('components.directoryWithComponents'); + } + /** @test */ public function it_can_register_a_directory_containing_namespaced_view_components() { @@ -154,7 +170,7 @@ public function it_can_register_a_directory_containing_namespaced_view_component $registeredComponents = collect(BladeX::registeredComponents()) ->mapWithKeys(function (Component $bladeXComponent) { - return [$bladeXComponent->tag => $bladeXComponent->view]; + return [$bladeXComponent->getTag() => $bladeXComponent->view]; }) ->toArray(); @@ -175,7 +191,7 @@ public function it_can_register_a_subdirectory_containing_namespaced_view_compon $registeredComponents = collect(BladeX::registeredComponents()) ->mapWithKeys(function (Component $bladeXComponent) { - return [$bladeXComponent->tag => $bladeXComponent->view]; + return [$bladeXComponent->getTag() => $bladeXComponent->view]; }) ->toArray(); @@ -194,6 +210,243 @@ public function it_overwrites_the_previous_component_when_registering_one_with_t $registeredComponents = BladeX::registeredComponents(); $this->assertEquals('components.directoryWithComponents.myView2', $registeredComponents[1]->view); - $this->assertEquals('foo', $registeredComponents[1]->tag); + $this->assertEquals('foo', $registeredComponents[1]->getTag()); + } + + /** @test */ + public function it_can_register_a_namespaced_view_component_without_namespace() + { + View::addNamespace('namespaced-test', __DIR__.'/stubs/components/namespacedComponents'); + + BladeX::component('namespaced-test::namespacedView1')->withoutNamespace(); + + $registeredComponents = collect(BladeX::registeredComponents()) + ->mapWithKeys(function (Component $bladeXComponent) { + return [$bladeXComponent->getTag() => $bladeXComponent->view]; + }) + ->toArray(); + + $this->assertEquals([ + 'context' => 'bladex::context', + 'namespaced-view1' => 'namespaced-test::namespacedView1', + ], $registeredComponents); + } + + /** @test */ + public function it_can_register_a_namespaced_view_component_without_namespace_but_global_prefix() + { + View::addNamespace('namespaced-test', __DIR__.'/stubs/components/namespacedComponents'); + + BladeX::prefix('x'); + + BladeX::component('namespaced-test::namespacedView1')->withoutNamespace(); + + $registeredComponents = collect(BladeX::registeredComponents()) + ->mapWithKeys(function (Component $bladeXComponent) { + return [$bladeXComponent->getTag() => $bladeXComponent->view]; + }) + ->toArray(); + + $this->assertEquals([ + 'x-context' => 'bladex::context', + 'x-namespaced-view1' => 'namespaced-test::namespacedView1', + ], $registeredComponents); + } + + /** @test */ + public function it_can_register_a_namespaced_view_component_without_namespace_but_self_prefix() + { + View::addNamespace('namespaced-test', __DIR__.'/stubs/components/namespacedComponents'); + + BladeX::component('namespaced-test::namespacedView1')->withoutNamespace()->prefix('ns'); + + $registeredComponents = collect(BladeX::registeredComponents()) + ->mapWithKeys(function (Component $bladeXComponent) { + return [$bladeXComponent->getTag() => $bladeXComponent->view]; + }) + ->toArray(); + + $this->assertEquals([ + 'context' => 'bladex::context', + 'ns-namespaced-view1' => 'namespaced-test::namespacedView1', + ], $registeredComponents); + } + + /** @test */ + public function it_can_register_a_namespaced_view_component_without_namespace_but_self_prefix_overrides_global_prefix() + { + View::addNamespace('namespaced-test', __DIR__.'/stubs/components/namespacedComponents'); + + BladeX::prefix('x'); + + BladeX::component('namespaced-test::namespacedView1')->withoutNamespace()->prefix('ns'); + + $registeredComponents = collect(BladeX::registeredComponents()) + ->mapWithKeys(function (Component $bladeXComponent) { + return [$bladeXComponent->getTag() => $bladeXComponent->view]; + }) + ->toArray(); + + $this->assertEquals([ + 'x-context' => 'bladex::context', + 'ns-namespaced-view1' => 'namespaced-test::namespacedView1', + ], $registeredComponents); + } + + /** @test */ + public function it_can_register_a_directory_containing_namespaced_view_components_without_namespace() + { + View::addNamespace('namespaced-test', __DIR__.'/stubs/components/namespacedComponents'); + + BladeX::components('namespaced-test::*')->withoutNamespace(); + + BladeX::components('namespaced-test::components.*'); + + $registeredComponents = collect(BladeX::registeredComponents()) + ->mapWithKeys(function (Component $bladeXComponent) { + return [$bladeXComponent->getTag() => $bladeXComponent->view]; + }) + ->toArray(); + + $this->assertEquals([ + 'context' => 'bladex::context', + 'namespaced-view1' => 'namespaced-test::namespacedView1', + 'namespaced-view2' => 'namespaced-test::namespacedView2', + 'namespaced-view3' => 'namespaced-test::namespacedView3', + 'namespaced-test::namespaced-view1' => 'namespaced-test::components.namespacedView1', + ], $registeredComponents); + } + + /** @test */ + public function it_can_register_a_directory_containing_namespaced_view_components_without_namespace_but_global_prefix() + { + View::addNamespace('namespaced-test', __DIR__.'/stubs/components/namespacedComponents'); + + BladeX::prefix('x'); + + BladeX::components('namespaced-test::*')->withoutNamespace(); + + BladeX::components('namespaced-test::components.*'); + + $registeredComponents = collect(BladeX::registeredComponents()) + ->mapWithKeys(function (Component $bladeXComponent) { + return [$bladeXComponent->getTag() => $bladeXComponent->view]; + }) + ->toArray(); + + $this->assertEquals([ + 'x-context' => 'bladex::context', + 'x-namespaced-view1' => 'namespaced-test::namespacedView1', + 'x-namespaced-view2' => 'namespaced-test::namespacedView2', + 'x-namespaced-view3' => 'namespaced-test::namespacedView3', + 'x-namespaced-test::namespaced-view1' => 'namespaced-test::components.namespacedView1', + ], $registeredComponents); + } + + /** @test */ + public function it_can_register_a_directory_containing_namespaced_view_components_without_namespace_but_self_prefix() + { + View::addNamespace('namespaced-test', __DIR__.'/stubs/components/namespacedComponents'); + + BladeX::components('namespaced-test::*')->withoutNamespace()->prefix('ns'); + + BladeX::components('namespaced-test::components.*'); + + $registeredComponents = collect(BladeX::registeredComponents()) + ->mapWithKeys(function (Component $bladeXComponent) { + return [$bladeXComponent->getTag() => $bladeXComponent->view]; + }) + ->toArray(); + + $this->assertEquals([ + 'context' => 'bladex::context', + 'ns-namespaced-view1' => 'namespaced-test::namespacedView1', + 'ns-namespaced-view2' => 'namespaced-test::namespacedView2', + 'ns-namespaced-view3' => 'namespaced-test::namespacedView3', + 'namespaced-test::namespaced-view1' => 'namespaced-test::components.namespacedView1', + ], $registeredComponents); + } + + /** @test */ + public function it_can_register_a_directory_containing_namespaced_view_components_without_namespace_but_self_prefix_overrides_global_prefix() + { + View::addNamespace('namespaced-test', __DIR__.'/stubs/components/namespacedComponents'); + + BladeX::prefix('x'); + + BladeX::components('namespaced-test::*')->withoutNamespace()->prefix('ns'); + + BladeX::components('namespaced-test::components.*')->withoutNamespace()->prefix('nsc'); + + $registeredComponents = collect(BladeX::registeredComponents()) + ->mapWithKeys(function (Component $bladeXComponent) { + return [$bladeXComponent->getTag() => $bladeXComponent->view]; + }) + ->toArray(); + + $this->assertEquals([ + 'x-context' => 'bladex::context', + 'ns-namespaced-view1' => 'namespaced-test::namespacedView1', + 'ns-namespaced-view2' => 'namespaced-test::namespacedView2', + 'ns-namespaced-view3' => 'namespaced-test::namespacedView3', + 'nsc-namespaced-view1' => 'namespaced-test::components.namespacedView1', + ], $registeredComponents); + } + + /** @test */ + public function it_can_register_multiple_directories_containing_view_components_with_prefix() + { + BladeX::components([ + 'components.directoryWithComponents.*', + 'components.directoryWithComponents2.*', + ])->prefix('c'); + + $registeredComponents = collect(BladeX::registeredComponents()) + ->mapWithKeys(function (Component $bladeXComponent) { + return [$bladeXComponent->getTag() => $bladeXComponent->view]; + }) + ->toArray(); + + $this->assertEquals([ + 'c-my-view1' => 'components.directoryWithComponents.myView1', + 'c-my-view2' => 'components.directoryWithComponents.myView2', + 'c-my-view3' => 'components.directoryWithComponents.myView3', + 'c-my-view4' => 'components.directoryWithComponents2.myView4', + 'c-my-view5' => 'components.directoryWithComponents2.myView5', + 'c-my-view6' => 'components.directoryWithComponents2.myView6', + 'context' => 'bladex::context', + ], $registeredComponents); + } + + /** @test */ + public function it_can_register_multiple_views_with_prefix() + { + BladeX::components([ + 'components.directoryWithComponents.myView1', + 'components.directoryWithComponents.myView2', + 'components.directoryWithComponents.myView3', + ])->prefix('c'); + + BladeX::components([ + 'components.directoryWithComponents2.myView4', + 'components.directoryWithComponents2.myView5', + 'components.directoryWithComponents2.myView6', + ]); + + $registeredComponents = collect(BladeX::registeredComponents()) + ->mapWithKeys(function (Component $bladeXComponent) { + return [$bladeXComponent->getTag() => $bladeXComponent->view]; + }) + ->toArray(); + + $this->assertEquals([ + 'c-my-view1' => 'components.directoryWithComponents.myView1', + 'c-my-view2' => 'components.directoryWithComponents.myView2', + 'c-my-view3' => 'components.directoryWithComponents.myView3', + 'my-view4' => 'components.directoryWithComponents2.myView4', + 'my-view5' => 'components.directoryWithComponents2.myView5', + 'my-view6' => 'components.directoryWithComponents2.myView6', + 'context' => 'bladex::context', + ], $registeredComponents); } }