|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\BladeX; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Spatie\BladeX\ComponentDirectory\NamespacedDirectory; |
|
7
|
|
|
use Spatie\BladeX\ComponentDirectory\RegularDirectory; |
|
8
|
|
|
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent; |
|
9
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
10
|
|
|
|
|
11
|
|
|
class BladeX |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var array */ |
|
14
|
|
|
public $registeredComponents = []; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $prefix = ''; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string|string[] $view |
|
21
|
|
|
* @param string $tag |
|
22
|
|
|
* |
|
23
|
|
|
* @return null|\Spatie\BladeX\Component |
|
24
|
|
|
*/ |
|
25
|
|
|
public function component($view, string $tag = ''): ?Component |
|
26
|
|
|
{ |
|
27
|
|
|
if (is_iterable($view)) { |
|
28
|
|
|
$this->registerViews($view); |
|
29
|
|
|
|
|
30
|
|
|
return null; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ($view instanceof Component) { |
|
34
|
|
|
$this->registeredComponents[] = $view; |
|
35
|
|
|
|
|
36
|
|
|
return $view; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (! is_string($view)) { |
|
40
|
|
|
throw CouldNotRegisterComponent::invalidArgument(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (Str::endsWith($view, '*')) { |
|
44
|
|
|
$this->registerComponents($view); |
|
45
|
|
|
|
|
46
|
|
|
return null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (! view()->exists($view)) { |
|
|
|
|
|
|
50
|
|
|
throw CouldNotRegisterComponent::viewNotFound($view, $tag); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$component = new Component($view, $tag); |
|
54
|
|
|
|
|
55
|
|
|
$this->registeredComponents[] = $component; |
|
56
|
|
|
|
|
57
|
|
|
return $component; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string|string[] $viewDirectory |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[] |
|
64
|
|
|
*/ |
|
65
|
|
|
public function components($viewDirectory): ComponentCollection |
|
66
|
|
|
{ |
|
67
|
|
|
if (is_iterable($viewDirectory)) { |
|
68
|
|
|
$components = new ComponentCollection(); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($viewDirectory as $singleViewDirectory) { |
|
71
|
|
|
if (Str::endsWith($singleViewDirectory, '*')) { |
|
72
|
|
|
$components = $components->merge($this->registerComponents($singleViewDirectory)); |
|
73
|
|
|
} else { |
|
74
|
|
|
$components->push($this->component($singleViewDirectory)); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $components; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->registerComponents($viewDirectory); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return \Spatie\BladeX\Component[] |
|
86
|
|
|
*/ |
|
87
|
|
|
public function registeredComponents(): array |
|
88
|
|
|
{ |
|
89
|
|
|
return collect($this->registeredComponents)->reverse()->unique(function (Component $component) { |
|
90
|
|
|
return $component->getTag(); |
|
91
|
|
|
})->reverse()->values()->all(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function prefix(string $prefix = ''): self |
|
95
|
|
|
{ |
|
96
|
|
|
$this->prefix = $prefix; |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getPrefix(): string |
|
102
|
|
|
{ |
|
103
|
|
|
return empty($this->prefix) ? '' : Str::finish($this->prefix, '-'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @internal |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $viewDirectory |
|
110
|
|
|
* |
|
111
|
|
|
* @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[] |
|
112
|
|
|
*/ |
|
113
|
|
|
public function registerComponents(string $viewDirectory) |
|
114
|
|
|
{ |
|
115
|
|
|
if (! Str::endsWith($viewDirectory, '*')) { |
|
116
|
|
|
throw CouldNotRegisterComponent::viewDirectoryWithoutWildcard($viewDirectory); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$includeSubdirectories = Str::endsWith($viewDirectory, '**.*'); |
|
120
|
|
|
|
|
121
|
|
|
$componentDirectory = Str::contains($viewDirectory, '::') |
|
122
|
|
|
? new NamespacedDirectory($viewDirectory, $includeSubdirectories) |
|
123
|
|
|
: new RegularDirectory($viewDirectory, $includeSubdirectories); |
|
124
|
|
|
|
|
125
|
|
|
return $this->registerViews( |
|
126
|
|
|
ComponentCollection::make($componentDirectory->getFiles()) |
|
127
|
|
|
|
|
128
|
|
|
->filter(function (SplFileInfo $file) { |
|
129
|
|
|
return Str::endsWith($file->getFilename(), '.blade.php'); |
|
130
|
|
|
}) |
|
131
|
|
|
->map(function (SplFileInfo $file) use ($componentDirectory) { |
|
132
|
|
|
return $componentDirectory->getViewName($file); |
|
133
|
|
|
}) |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param iterable|string[] $views |
|
139
|
|
|
* |
|
140
|
|
|
* @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[] |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function registerViews(iterable $views): ComponentCollection |
|
143
|
|
|
{ |
|
144
|
|
|
return ComponentCollection::make($views)->map(function (string $viewName) { |
|
145
|
|
|
return $this->component($viewName); |
|
146
|
|
|
}); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: