Skip to content

Commit 204fad6

Browse files
committed
Resolve view_path relative to Livewire’s config
1 parent 97a0c7b commit 204fad6

6 files changed

Lines changed: 60 additions & 20 deletions

File tree

config/livewire-forms.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232
| View Path
3333
|--------------------------------------------------------------------------
3434
|
35-
| The path under resources/views where the form views are published and loaded from.
35+
| The subdirectory within Livewire's configured view path where form views
36+
| are published and loaded from. This is relative to the 'view_path'
37+
| setting in your Livewire config (default: resources/views/livewire).
3638
|
3739
*/
3840

39-
'view_path' => 'livewire/forms',
41+
'view_path' => 'forms',
4042

4143
/*
4244
|--------------------------------------------------------------------------

src/Commands/MakeTheme.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Aerni\LivewireForms\Commands;
44

5+
use Aerni\LivewireForms\Facades\ViewManager;
56
use Illuminate\Console\Command;
67
use Illuminate\Support\Facades\File;
78
use Illuminate\Support\Str;
@@ -22,7 +23,7 @@ class MakeTheme extends Command
2223
public function handle(): void
2324
{
2425
$name = $this->argument('name') ?? text(label: 'What do you want to name the theme?', default: config('livewire-forms.theme'), required: true);
25-
$path = resource_path('views/'.config('livewire-forms.view_path').'/'.Str::snake($name));
26+
$path = ViewManager::absoluteViewPath(Str::snake($name));
2627

2728
if (! File::exists($path) || confirm(label: 'A theme with this name already exists. Do you want to overwrite it?', default: false)) {
2829
File::copyDirectory(__DIR__.'/../../resources/views/default/', $path);

src/Commands/MakeView.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Aerni\LivewireForms\Commands;
44

5+
use Aerni\LivewireForms\Facades\ViewManager;
56
use Illuminate\Console\Command;
67
use Illuminate\Support\Facades\File;
78
use Illuminate\Support\Str;
@@ -24,10 +25,10 @@ public function handle(): void
2425
$name = $this->argument('name') ?? text(label: 'What do you want to name the view?', default: config('livewire-forms.view'), required: true);
2526
$stub = File::get(__DIR__.'/../../resources/views/default.blade.php');
2627
$filename = Str::slug($name).'.blade.php';
27-
$path = resource_path('views/'.config('livewire-forms.view_path')."/{$filename}");
28+
$path = ViewManager::absoluteViewPath($filename);
2829

2930
if (! File::exists($path) || confirm(label: 'A view with this name already exists. Do you want to overwrite it?', default: false)) {
30-
File::ensureDirectoryExists(resource_path('views/'.config('livewire-forms.view_path')));
31+
File::ensureDirectoryExists(ViewManager::absoluteViewPath());
3132
File::put($path, $stub);
3233
info("The view was successfully created: <comment>{$this->getRelativePath($path)}</comment>");
3334
}

src/ViewManager.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@
44

55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Str;
7+
use Statamic\Facades\Path;
78

89
class ViewManager
910
{
10-
public function viewPath(string $view): string
11+
public function absoluteViewPath(string $path = ''): string
1112
{
12-
return config('livewire-forms.view_path', 'livewire/forms')."/{$view}";
13+
return rtrim(Path::assemble(
14+
config('livewire.view_path', resource_path('views/livewire')),
15+
config('livewire-forms.view_path', 'forms'),
16+
$path,
17+
), '/');
18+
}
19+
20+
public function viewPath(string $view = ''): string
21+
{
22+
return Str::after($this->absoluteViewPath($view), resource_path('views/'));
1323
}
1424

1525
public function themeViewPath(string $theme, string $view): string
@@ -40,7 +50,7 @@ public function themeExists(?string $theme): bool
4050
return false;
4151
}
4252

43-
return is_dir(resource_path("views/{$this->viewPath($theme)}"));
53+
return is_dir($this->absoluteViewPath($theme));
4454
}
4555

4656
public function defaultView(): string
@@ -55,7 +65,7 @@ public function defaultTheme(): string
5565

5666
public function views(): ?array
5767
{
58-
$path = resource_path('views/'.config('livewire-forms.view_path'));
68+
$path = $this->absoluteViewPath();
5969

6070
if (! File::isDirectory($path)) {
6171
return null;
@@ -69,7 +79,7 @@ public function views(): ?array
6979

7080
public function themes(): ?array
7181
{
72-
$path = resource_path('views/'.config('livewire-forms.view_path'));
82+
$path = $this->absoluteViewPath();
7383

7484
if (! File::isDirectory($path)) {
7585
return null;

tests/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ protected function defineEnvironment($app)
4343
protected function copyResources(): void
4444
{
4545
if (! ViewManager::themeExists('default')) {
46-
File::copyDirectory(__DIR__.'/../resources/views/default/', resource_path('views'.'/'.config('livewire-forms.view_path').'/default'));
46+
File::copyDirectory(__DIR__.'/../resources/views/default/', ViewManager::absoluteViewPath('default'));
4747
}
4848

4949
if (! ViewManager::viewExists('default')) {
50-
File::copy(__DIR__.'/../resources/views/default.blade.php', resource_path('views'.'/'.config('livewire-forms.view_path').'/default.blade.php'));
50+
File::copy(__DIR__.'/../resources/views/default.blade.php', ViewManager::absoluteViewPath('default.blade.php'));
5151
}
5252
}
5353
}

tests/ViewManagerTest.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,41 @@
22

33
use Aerni\LivewireForms\Facades\ViewManager;
44

5-
it('can get the path of a view', function () {
6-
config()->set('livewire-forms.view_path', 'my/custom/path');
5+
it('resolves view path relative to Livewire view path', function () {
6+
expect(ViewManager::viewPath('view'))->toBe('livewire/forms/view');
7+
});
8+
9+
it('resolves view path with custom Livewire view path', function () {
10+
config()->set('livewire.view_path', resource_path('views/wire'));
11+
12+
expect(ViewManager::viewPath('view'))->toBe('wire/forms/view');
13+
});
714

8-
expect(ViewManager::viewPath('view'))->toBe('my/custom/path/view');
15+
it('resolves view path with custom forms view path', function () {
16+
config()->set('livewire-forms.view_path', 'custom');
17+
18+
expect(ViewManager::viewPath('view'))->toBe('livewire/custom/view');
19+
});
20+
21+
it('resolves view path without a view', function () {
22+
expect(ViewManager::viewPath())->toBe('livewire/forms');
23+
});
24+
25+
it('resolves absolute view path', function () {
26+
expect(ViewManager::absoluteViewPath())
27+
->toBe(resource_path('views/livewire/forms'));
28+
});
29+
30+
it('resolves absolute view path with a subpath', function () {
31+
expect(ViewManager::absoluteViewPath('default'))
32+
->toBe(resource_path('views/livewire/forms/default'));
33+
});
34+
35+
it('resolves absolute view path with custom Livewire view path', function () {
36+
config()->set('livewire.view_path', resource_path('views/wire'));
37+
38+
expect(ViewManager::absoluteViewPath('default'))
39+
->toBe(resource_path('views/wire/forms/default'));
940
});
1041

1142
it('can get the default view', function () {
@@ -29,8 +60,3 @@
2960
expect(ViewManager::themeViewExists('default', 'fields.default'))->toBeTrue();
3061
expect(ViewManager::themeViewExists('default', 'fields.nope'))->toBeFalse();
3162
});
32-
33-
// it('can check if a theme exists', function () {
34-
// expect(ViewManager::themeExists('default'))->toBeTrue();
35-
// expect(ViewManager::themeExists('nope'))->toBeFalse();
36-
// });

0 commit comments

Comments
 (0)