Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions config/livewire-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
| View Path
|--------------------------------------------------------------------------
|
| The path under resources/views where the form views are published and loaded from.
| The subdirectory within Livewire's configured view path where form views
| are published and loaded from. This is relative to the 'view_path'
| setting in your Livewire config (default: resources/views/livewire).
|
*/

'view_path' => 'livewire/forms',
'view_path' => 'forms',

/*
|--------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/MakeTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Aerni\LivewireForms\Commands;

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

if (! File::exists($path) || confirm(label: 'A theme with this name already exists. Do you want to overwrite it?', default: false)) {
File::copyDirectory(__DIR__.'/../../resources/views/default/', $path);
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/MakeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Aerni\LivewireForms\Commands;

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

if (! File::exists($path) || confirm(label: 'A view with this name already exists. Do you want to overwrite it?', default: false)) {
File::ensureDirectoryExists(resource_path('views/'.config('livewire-forms.view_path')));
File::ensureDirectoryExists(ViewManager::absoluteViewPath());
File::put($path, $stub);
info("The view was successfully created: <comment>{$this->getRelativePath($path)}</comment>");
}
Expand Down
26 changes: 0 additions & 26 deletions src/UpdateScripts/AddDictionaryFieldView.php

This file was deleted.

52 changes: 52 additions & 0 deletions src/UpdateScripts/MigrateViewPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Aerni\LivewireForms\UpdateScripts;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Statamic\UpdateScripts\UpdateScript;

class MigrateViewPath extends UpdateScript
{
public function shouldUpdate($newVersion, $oldVersion)
{
return $this->isUpdatingTo('10.3.0');
}

public function update()
{
$configPath = config_path('livewire-forms.php');

if (! File::exists($configPath)) {
return;
}

$oldViewPath = config('livewire-forms.view_path');
$oldAbsolutePath = resource_path("views/{$oldViewPath}");
$livewireViewPath = config('livewire.view_path', resource_path('views/livewire'));

if (! str_starts_with($oldAbsolutePath, "{$livewireViewPath}/")) {
$this->console()->warn("The 'view_path' config is now relative to Livewire's configured view path. Your custom value '{$oldViewPath}' could not be auto-migrated. Please update config/livewire-forms.php manually.");

return;
}

$newViewPath = Str::after($oldAbsolutePath, "{$livewireViewPath}/");

if ($newViewPath === $oldViewPath) {
return;
}

$contents = File::get($configPath);

$contents = str_replace(
"'view_path' => '{$oldViewPath}'",
"'view_path' => '{$newViewPath}'",
$contents,
);

File::put($configPath, $contents);

$this->console()->info("Updated 'view_path' config from '{$oldViewPath}' to '{$newViewPath}'. The path is now relative to Livewire's configured view path.");
}
}
20 changes: 15 additions & 5 deletions src/ViewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@

use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Statamic\Facades\Path;

class ViewManager
{
public function viewPath(string $view): string
public function absoluteViewPath(string $path = ''): string
{
return config('livewire-forms.view_path', 'livewire/forms')."/{$view}";
return rtrim(Path::assemble(
config('livewire.view_path', resource_path('views/livewire')),
config('livewire-forms.view_path', 'forms'),
$path,
), '/');
}

public function viewPath(string $view = ''): string
{
return Str::after($this->absoluteViewPath($view), resource_path('views/'));
}

public function themeViewPath(string $theme, string $view): string
Expand Down Expand Up @@ -40,7 +50,7 @@ public function themeExists(?string $theme): bool
return false;
}

return is_dir(resource_path("views/{$this->viewPath($theme)}"));
return is_dir($this->absoluteViewPath($theme));
}

public function defaultView(): string
Expand All @@ -55,7 +65,7 @@ public function defaultTheme(): string

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

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

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

if (! File::isDirectory($path)) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ protected function defineEnvironment($app)
protected function copyResources(): void
{
if (! ViewManager::themeExists('default')) {
File::copyDirectory(__DIR__.'/../resources/views/default/', resource_path('views'.'/'.config('livewire-forms.view_path').'/default'));
File::copyDirectory(__DIR__.'/../resources/views/default/', ViewManager::absoluteViewPath('default'));
}

if (! ViewManager::viewExists('default')) {
File::copy(__DIR__.'/../resources/views/default.blade.php', resource_path('views'.'/'.config('livewire-forms.view_path').'/default.blade.php'));
File::copy(__DIR__.'/../resources/views/default.blade.php', ViewManager::absoluteViewPath('default.blade.php'));
}
}
}
42 changes: 34 additions & 8 deletions tests/ViewManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,41 @@

use Aerni\LivewireForms\Facades\ViewManager;

it('can get the path of a view', function () {
config()->set('livewire-forms.view_path', 'my/custom/path');
it('resolves view path relative to Livewire view path', function () {
expect(ViewManager::viewPath('view'))->toBe('livewire/forms/view');
});

it('resolves view path with custom Livewire view path', function () {
config()->set('livewire.view_path', resource_path('views/wire'));

expect(ViewManager::viewPath('view'))->toBe('wire/forms/view');
});

expect(ViewManager::viewPath('view'))->toBe('my/custom/path/view');
it('resolves view path with custom forms view path', function () {
config()->set('livewire-forms.view_path', 'custom');

expect(ViewManager::viewPath('view'))->toBe('livewire/custom/view');
});

it('resolves view path without a view', function () {
expect(ViewManager::viewPath())->toBe('livewire/forms');
});

it('resolves absolute view path', function () {
expect(ViewManager::absoluteViewPath())
->toBe(resource_path('views/livewire/forms'));
});

it('resolves absolute view path with a subpath', function () {
expect(ViewManager::absoluteViewPath('default'))
->toBe(resource_path('views/livewire/forms/default'));
});

it('resolves absolute view path with custom Livewire view path', function () {
config()->set('livewire.view_path', resource_path('views/wire'));

expect(ViewManager::absoluteViewPath('default'))
->toBe(resource_path('views/wire/forms/default'));
});

it('can get the default view', function () {
Expand All @@ -29,8 +60,3 @@
expect(ViewManager::themeViewExists('default', 'fields.default'))->toBeTrue();
expect(ViewManager::themeViewExists('default', 'fields.nope'))->toBeFalse();
});

// it('can check if a theme exists', function () {
// expect(ViewManager::themeExists('default'))->toBeTrue();
// expect(ViewManager::themeExists('nope'))->toBeFalse();
// });
Loading