|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\BladeX; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
|
6
|
|
|
use Illuminate\Support\Facades\View; |
|
7
|
|
|
use Spatie\BladeX\ComponentDirectory\NamespacedDirectory; |
|
8
|
|
|
use Spatie\BladeX\ComponentDirectory\RegularDirectory; |
|
9
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
10
|
|
|
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent; |
|
11
|
|
|
|
|
12
|
|
|
class BladeX |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var array */ |
|
15
|
|
|
public $registeredComponents = []; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $prefix = ''; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string|array $view |
|
22
|
|
|
* @param string $tag |
|
23
|
|
|
* |
|
24
|
|
|
* @return null|\Spatie\BladeX\Component |
|
25
|
|
|
*/ |
|
26
|
|
|
public function component($view, string $tag = ''): ?Component |
|
27
|
|
|
{ |
|
28
|
|
|
if (is_array($view)) { |
|
29
|
|
|
foreach ($view as $singleView) { |
|
30
|
|
|
$this->component($singleView); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return null; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if ($view instanceof Component) { |
|
37
|
|
|
$this->registeredComponents[$view->tag] = $view; |
|
38
|
|
|
|
|
39
|
|
|
return $view; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (!is_string($view)) { |
|
43
|
|
|
throw CouldNotRegisterComponent::invalidArgument(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (ends_with($view, '*')) { |
|
47
|
|
|
$this->registerComponents($view); |
|
48
|
|
|
|
|
49
|
|
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (! view()->exists($view)) { |
|
|
|
|
|
|
53
|
|
|
throw CouldNotRegisterComponent::viewNotFound($view, $tag); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$component = new Component($view, $tag); |
|
57
|
|
|
|
|
58
|
|
|
$this->registeredComponents[$component->tag] = $component; |
|
59
|
|
|
|
|
60
|
|
|
return $component; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function registeredComponents(): array |
|
64
|
|
|
{ |
|
65
|
|
|
return array_values($this->registeredComponents); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function prefix(string $prefix = ''): self |
|
69
|
|
|
{ |
|
70
|
|
|
$this->prefix = $prefix; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getPrefix(): string |
|
76
|
|
|
{ |
|
77
|
|
|
return empty($this->prefix) ? '' : str_finish($this->prefix, '-'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function registerComponents(string $viewDirectory) |
|
81
|
|
|
{ |
|
82
|
|
|
$componentDirectory = str_contains($viewDirectory, '::') |
|
83
|
|
|
? new NamespacedDirectory($viewDirectory) |
|
84
|
|
|
: new RegularDirectory($viewDirectory); |
|
85
|
|
|
|
|
86
|
|
|
collect(File::files($componentDirectory->getAbsoluteDirectory())) |
|
87
|
|
|
->filter(function (SplFileInfo $file) { |
|
88
|
|
|
return ends_with($file->getFilename(), '.blade.php'); |
|
89
|
|
|
}) |
|
90
|
|
|
->map(function (SplFileInfo $file) use ($componentDirectory) { |
|
91
|
|
|
return $componentDirectory->getViewName($file); |
|
92
|
|
|
}) |
|
93
|
|
|
->each(function (string $viewName) { |
|
94
|
|
|
$this->component($viewName); |
|
95
|
|
|
}); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
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: