Skip to content

Commit 0728eff

Browse files
committed
add autoload views with namespace
1 parent 5da8740 commit 0728eff

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

src/PaginationViewServiceProvider.php

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,55 @@ class PaginationViewServiceProvider extends ServiceProvider
99
{
1010
public function boot(): void
1111
{
12-
if ($this->app->runningInConsole()) {
13-
// Set the target path
14-
$target = $this->app->resourcePath('views/vendor/pagination');
15-
16-
// Get views path
17-
$directory = __DIR__ . '/../resources/views';
12+
// Get views path
13+
$directory = __DIR__ . '/../resources/views';
1814

19-
// Get all folders inside views
20-
$folders = File::directories($directory);
15+
// Get all folders inside views
16+
$folders = File::directories($directory);
2117

22-
$views = [];
18+
$views = [];
2319

24-
// Create associative array for all vendors, vendor_name => folder_path
25-
foreach ($folders as $folder) {
26-
$vendorName = basename($folder);
27-
$source = $directory . DIRECTORY_SEPARATOR . $vendorName;
20+
// Create associative array for all vendors, vendor_name => folder_path
21+
foreach ($folders as $folder) {
22+
$vendorName = basename($folder);
23+
$source = $directory . DIRECTORY_SEPARATOR . $vendorName;
2824

29-
if (is_dir($source)) {
30-
$views[$vendorName] = $source;
31-
}
25+
if (is_dir($source)) {
26+
$views[$vendorName] = $source;
3227
}
28+
}
3329

34-
// Create an array where the original view paths are keys and the target path is the value
35-
$publishPaths = array_fill_keys(
36-
array_values($views),
37-
$target
38-
);
30+
// Load all views with namespace
31+
$this->loadViewsFrom(
32+
array_values($views),
33+
'pagination'
34+
);
3935

40-
// Publish all views under a single tag
41-
$this->publishes($publishPaths, 'pagination-view-all');
36+
if ($this->app->runningInConsole()) {
37+
$this->bootInConsole($views);
38+
}
39+
}
4240

43-
// Publish individual views under specific tags
44-
foreach ($views as $vendor => $path) {
45-
$this->publishes(
46-
[$path => $target],
47-
"pagination-view-$vendor"
48-
);
49-
}
41+
protected function bootInConsole($views): void
42+
{
43+
// Set the target path
44+
$target = $this->app->resourcePath('views/vendor/pagination');
45+
46+
// Create an array where the original view paths are keys and the target path is the value
47+
$publishPaths = array_fill_keys(
48+
array_values($views),
49+
$target
50+
);
51+
52+
// Publish all views under a single tag
53+
$this->publishes($publishPaths, 'pagination-view-all');
54+
55+
// Publish individual views under specific tags
56+
foreach ($views as $vendor => $path) {
57+
$this->publishes(
58+
[$path => $target],
59+
"pagination-view-$vendor"
60+
);
5061
}
5162
}
5263
}

0 commit comments

Comments
 (0)