Skip to content

Commit 035e963

Browse files
authored
Fix exception caused by non-existing directories (#80)
1 parent 07ecb88 commit 035e963

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/ServiceProvider.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,15 @@ protected function bootFormConfigFields(): self
9595
'type' => 'select',
9696
'display' => __('View'),
9797
'instructions' => __('Choose the view for this form.'),
98-
'options' => collect(File::files(resource_path('views/'.config('livewire-forms.view_path'))))
99-
->map(fn ($file) => Str::before($file->getBasename(), '.'))
100-
->mapWithKeys(fn ($view) => [$view => str($view)->replace(['_', '-'], ' ')->title()->toString()]),
98+
'options' => $this->viewOptions(),
10199
'clearable' => true,
102100
'width' => 50,
103101
],
104102
'theme' => [
105103
'type' => 'select',
106104
'display' => __('Theme'),
107105
'instructions' => __('Choose the theme for this form.'),
108-
'options' => collect(File::directories(resource_path('views/'.config('livewire-forms.view_path'))))
109-
->map(fn ($directory) => basename($directory))
110-
->mapWithKeys(fn ($theme) => [$theme => str($theme)->replace(['_', '-'], ' ')->title()->toString()]),
106+
'options' => $this->themeOptions(),
111107
'clearable' => true,
112108
'width' => 50,
113109
],
@@ -120,4 +116,32 @@ protected function bootFormConfigFields(): self
120116

121117
return $this;
122118
}
119+
120+
protected function viewOptions(): ?array
121+
{
122+
$path = resource_path('views/'.config('livewire-forms.view_path'));
123+
124+
if (! File::isDirectory($path)) {
125+
return null;
126+
}
127+
128+
return collect(File::files($path))
129+
->map(fn ($file) => Str::before($file->getBasename(), '.'))
130+
->mapWithKeys(fn ($view) => [$view => str($view)->replace(['_', '-'], ' ')->title()->toString()])
131+
->all();
132+
}
133+
134+
protected function themeOptions(): ?array
135+
{
136+
$path = resource_path('views/'.config('livewire-forms.view_path'));
137+
138+
if (! File::isDirectory($path)) {
139+
return null;
140+
}
141+
142+
return collect(File::directories($path))
143+
->map(fn ($directory) => basename($directory))
144+
->mapWithKeys(fn ($theme) => [$theme => str($theme)->replace(['_', '-'], ' ')->title()->toString()])
145+
->all();
146+
}
123147
}

0 commit comments

Comments
 (0)