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