Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #61 from dinhquochan/feature/support-namespace
Browse files Browse the repository at this point in the history
[Feature] Add support subdirectories in a vendor namespace and global prefix (fixes #60)
  • Loading branch information
freekmurze authored Oct 29, 2018
2 parents 265cb16 + 2f63305 commit a36be68
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ protected function determineDefaultTag(string $view): string

$tag = kebab_case(end($baseComponentName));

if (str_contains($view, '::') && ! str_contains($tag, '::')) {
$namespace = array_first(explode('::', $view));
$tag = "{$namespace}::{$tag}";
}

return $tag;
}
}
2 changes: 1 addition & 1 deletion src/ComponentDirectory/NamespacedDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getAbsoluteDirectory(): string
throw CouldNotRegisterComponent::viewPathNotFound($viewPath);
}

return $absoluteDirectory;
return $viewPath ? "{$absoluteDirectory}/{$viewPath}" : $absoluteDirectory;
}

public function getViewName(SplFileInfo $viewFile): string
Expand Down
12 changes: 12 additions & 0 deletions tests/Features/ComponentCompilation/ComponentCompilationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ public function it_works_with_a_global_prefix()
$this->assertMatchesViewSnapshot('globalPrefix');
}

/** @test */
public function a_global_prefix_works_with_namespaced_component()
{
View::addNamespace('namespaced-components', __DIR__.'/stubs/namespacedComponents');

BladeX::component('namespaced-components::namespacedCard');

BladeX::prefix('x');

$this->assertMatchesViewSnapshot('namespacedGlobalPrefix');
}

/** @test */
public function it_compiles_components_that_use_a_global_function()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<div>
<div class="card">
<h1>Profile</h1>
<div>My content</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<div><?php $__env->startComponent(
'namespaced-components::namespacedCard',
array_merge(app(Spatie\BladeX\ContextStack::class)->read(),
['title' => 'Profile'])
); ?>
My content
<?php echo $__env->renderComponent(); ?>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="card">
<h1>{!! is_array($title) ? $title[0] : $title !!}</h1>
<div>{{ $slot }}</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<x-namespaced-components::namespaced-card title="Profile">
My content
</x-namespaced-components::namespaced-card>
19 changes: 19 additions & 0 deletions tests/Features/Registration/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ public function it_can_register_a_directory_containing_namespaced_view_component
], $registeredComponents);
}

/** @test */
public function it_can_register_a_subdirectory_containing_namespaced_view_components()
{
View::addNamespace('subdirectory-namespaced-test', __DIR__.'/stubs/components/namespacedComponents');

BladeX::component('subdirectory-namespaced-test::components.*');

$registeredComponents = collect(BladeX::registeredComponents())
->mapWithKeys(function (Component $bladeXComponent) {
return [$bladeXComponent->tag => $bladeXComponent->view];
})
->toArray();

$this->assertEquals([
'context' => 'bladex::context',
'subdirectory-namespaced-test::namespaced-view1' => 'subdirectory-namespaced-test::components.namespacedView1',
], $registeredComponents);
}

/** @test */
public function it_overwrites_the_previous_component_when_registering_one_with_the_same_name()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
namespaced with subdirectory view 1

0 comments on commit a36be68

Please sign in to comment.