Skip to content

Commit 0cef567

Browse files
committed
Fix self-named components more and add tests
1 parent c5da1f5 commit 0cef567

File tree

3 files changed

+143
-17
lines changed

3 files changed

+143
-17
lines changed

src/BladeService.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,17 @@ public function componentNameToPath($name): string
140140
return $fullPath;
141141
}
142142

143-
// For root components, try index.blade.php (e.g., pages::auth -> auth/index.blade.php)...
144-
if (!str_contains($componentPath, '/')) {
145-
$indexPath = $basePath . '/' . $componentPath . '/index.blade.php';
146-
if (file_exists($indexPath)) {
147-
return $indexPath;
148-
}
149-
150-
// Try same-name file (e.g., pages::auth -> auth/auth.blade.php)...
151-
$sameNamePath = $basePath . '/' . $componentPath . '/' . $componentPath . '.blade.php';
152-
if (file_exists($sameNamePath)) {
153-
return $sameNamePath;
154-
}
143+
// Try index.blade.php (e.g., pages::auth -> auth/index.blade.php)...
144+
$indexPath = $basePath . '/' . $componentPath . '/index.blade.php';
145+
if (file_exists($indexPath)) {
146+
return $indexPath;
147+
}
148+
149+
// Try same-name file (e.g., pages::auth -> auth/auth.blade.php)...
150+
$lastSegment = basename($componentPath);
151+
$sameNamePath = $basePath . '/' . $componentPath . '/' . $lastSegment . '.blade.php';
152+
if (file_exists($sameNamePath)) {
153+
return $sameNamePath;
155154
}
156155
}
157156
}
@@ -178,19 +177,20 @@ public function componentNameToPath($name): string
178177

179178
// Try direct component file first (e.g., form.input -> form/input.blade.php)...
180179
$fullPath = $basePath . '/' . $componentPath . '.blade.php';
181-
if (file_exists($fullPath) && ! is_dir($fullPath)) {
180+
if (file_exists($fullPath)) {
182181
return $fullPath;
183182
}
184183

185-
// For root components, try index.blade.php (e.g., form -> form/index.blade.php)...
184+
// Try index.blade.php (e.g., form -> form/index.blade.php)...
186185
$indexPath = $basePath . '/' . $componentPath . '/index.blade.php';
187-
if (file_exists($indexPath) && ! is_dir($indexPath)) {
186+
if (file_exists($indexPath)) {
188187
return $indexPath;
189188
}
190189

191190
// Try same-name file (e.g., card -> card/card.blade.php)...
192-
$sameNamePath = $basePath . '/' . $componentPath . '/' . $componentPath . '.blade.php';
193-
if (file_exists($sameNamePath) && ! is_dir($sameNamePath)) {
191+
$lastSegment = basename($componentPath);
192+
$sameNamePath = $basePath . '/' . $componentPath . '/' . $lastSegment . '.blade.php';
193+
if (file_exists($sameNamePath)) {
194194
return $sameNamePath;
195195
}
196196
}

tests/BladeServiceTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
use Livewire\Blaze\BladeService;
4+
5+
describe('componentNameToPath', function () {
6+
beforeEach(function () {
7+
$basePath = __DIR__ . '/fixtures/components';
8+
app('blade.compiler')->anonymousComponentPath($basePath);
9+
app('blade.compiler')->anonymousComponentPath($basePath, 'fixtures');
10+
});
11+
12+
describe('namespaced', function () {
13+
it('gets the correct path for namespaced direct component files', function () {
14+
$input = 'fixtures::button';
15+
$expected = __DIR__ . '/fixtures/components/button.blade.php';
16+
17+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
18+
});
19+
20+
it('prefers namespaced direct component file over index.blade.php for root components', function () {
21+
$input = 'fixtures::card';
22+
$expected = __DIR__ . '/fixtures/components/card.blade.php';
23+
24+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
25+
});
26+
27+
it('gets the correct path for namespaced nested components', function () {
28+
$input = 'fixtures::form.input';
29+
$expected = __DIR__ . '/fixtures/components/form/input.blade.php';
30+
31+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
32+
});
33+
34+
it('gets the correct path for namespaced deeply nested components', function () {
35+
$input = 'fixtures::form.fields.text';
36+
$expected = __DIR__ . '/fixtures/components/form/fields/text.blade.php';
37+
38+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
39+
});
40+
41+
it('gets the correct path for namespaced root components using index.blade.php', function () {
42+
$input = 'fixtures::form';
43+
$expected = __DIR__ . '/fixtures/components/form/index.blade.php';
44+
45+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
46+
});
47+
48+
it('gets the correct path for namespaced root components using same-name.blade.php', function () {
49+
$input = 'fixtures::panel';
50+
$expected = __DIR__ . '/fixtures/components/panel/panel.blade.php';
51+
52+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
53+
});
54+
55+
it('gets the correct path for namespaced nested root components using same-name.blade.php', function () {
56+
$input = 'fixtures::kanban.comments';
57+
$expected = __DIR__ . '/fixtures/components/kanban/comments/comments.blade.php';
58+
59+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
60+
});
61+
62+
it('handles non-existent namespaced components gracefully', function () {
63+
$input = 'fixtures::nonexistent.component';
64+
65+
expect((new BladeService)->componentNameToPath($input))->toBe('');
66+
});
67+
});
68+
69+
describe('non-namespaced', function () {
70+
it('gets the correct path for direct component files', function () {
71+
$input = 'button';
72+
$expected = __DIR__ . '/fixtures/components/button.blade.php';
73+
74+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
75+
});
76+
77+
it('prefers direct component file over index.blade.php for root components', function () {
78+
$input = 'card';
79+
$expected = __DIR__ . '/fixtures/components/card.blade.php';
80+
81+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
82+
});
83+
84+
it('gets the correct path for nested components', function () {
85+
$input = 'form.input';
86+
$expected = __DIR__ . '/fixtures/components/form/input.blade.php';
87+
88+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
89+
});
90+
91+
it('gets the correct path for deeply nested components', function () {
92+
$input = 'form.fields.text';
93+
$expected = __DIR__ . '/fixtures/components/form/fields/text.blade.php';
94+
95+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
96+
});
97+
98+
it('gets the correct path for root components using index.blade.php', function () {
99+
$input = 'form';
100+
$expected = __DIR__ . '/fixtures/components/form/index.blade.php';
101+
102+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
103+
});
104+
105+
it('gets the correct path for root components using same-name.blade.php', function () {
106+
$input = 'panel';
107+
$expected = __DIR__ . '/fixtures/components/panel/panel.blade.php';
108+
109+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
110+
});
111+
112+
it('gets the correct path for nested root components using same-name.blade.php', function () {
113+
$input = 'kanban.comments';
114+
$expected = __DIR__ . '/fixtures/components/kanban/comments/comments.blade.php';
115+
116+
expect((new BladeService)->componentNameToPath($input))->toBe($expected);
117+
});
118+
119+
it('handles non-existent components gracefully', function () {
120+
$input = 'nonexistent.component';
121+
122+
expect((new BladeService)->componentNameToPath($input))->toBe('');
123+
});
124+
});
125+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Comments

0 commit comments

Comments
 (0)