1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\BladeX; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
use Illuminate\Support\Facades\View; |
8
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
9
|
|
|
use Spatie\BladeX\Exceptions\CouldNotRegisterBladeXComponent; |
10
|
|
|
|
11
|
|
|
class BladeX |
12
|
|
|
{ |
13
|
|
|
/** @var array */ |
14
|
|
|
public $registeredComponents = []; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $prefix = ''; |
18
|
|
|
|
19
|
|
|
public function component(string $bladeViewName, string $bladeXComponentName = null): BladeXComponent |
20
|
|
|
{ |
21
|
|
|
$bladeViewName = str_replace('.', '/', $bladeViewName); |
22
|
|
|
|
23
|
|
|
if (is_null($bladeXComponentName)) { |
24
|
|
|
$baseComponentName = explode('/', $bladeViewName); |
25
|
|
|
|
26
|
|
|
$bladeXComponentName = kebab_case(end($baseComponentName)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (! view()->exists($bladeViewName)) { |
|
|
|
|
30
|
|
|
throw CouldNotRegisterBladeXComponent::viewNotFound($bladeViewName, $bladeXComponentName); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$newBladeXComponent = new BladeXComponent($bladeXComponentName, $bladeViewName); |
34
|
|
|
|
35
|
|
|
$this->registeredComponents[$newBladeXComponent->name] = $newBladeXComponent; |
36
|
|
|
|
37
|
|
|
return $newBladeXComponent; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getRegisteredComponents(): array |
41
|
|
|
{ |
42
|
|
|
return array_values($this->registeredComponents); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function components($directory) |
46
|
|
|
{ |
47
|
|
|
if (is_string($directory)) { |
48
|
|
|
$directory = [$directory]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (! is_array($directory)) { |
52
|
|
|
throw new InvalidArgumentException(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
collect($directory)->each(function ($directory) { |
56
|
|
|
$this->registerComponents($directory); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function registerComponents(string $directory) |
61
|
|
|
{ |
62
|
|
|
if (! File::isDirectory($directory)) { |
63
|
|
|
throw CouldNotRegisterBladeXComponent::componentDirectoryNotFound($directory); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
collect(File::allFiles($directory)) |
67
|
|
|
->filter(function (SplFileInfo $file) { |
68
|
|
|
return ends_with($file->getFilename(), '.blade.php'); |
69
|
|
|
}) |
70
|
|
|
->each(function (SplFileInfo $fileInfo) { |
71
|
|
|
$viewName = $this->getViewName($fileInfo->getPathname()); |
72
|
|
|
|
73
|
|
|
$componentName = str_replace_last('.blade.php', '', $fileInfo->getFilename()); |
74
|
|
|
|
75
|
|
|
$componentName = kebab_case($componentName); |
76
|
|
|
|
77
|
|
|
$this->component($viewName, $componentName); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function prefix(string $prefix = ''): self |
82
|
|
|
{ |
83
|
|
|
$this->prefix = $prefix; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getPrefix(): string |
89
|
|
|
{ |
90
|
|
|
return empty($this->prefix) ? '' : str_finish($this->prefix, '-'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function getViewName(string $pathName): string |
94
|
|
|
{ |
95
|
|
|
foreach (View::getFinder()->getPaths() as $registeredViewPath) { |
96
|
|
|
$pathName = str_replace(realpath($registeredViewPath).'/', '', $pathName); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$viewName = str_replace_last('.blade.php', '', $pathName); |
100
|
|
|
|
101
|
|
|
return $viewName; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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: