|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Livewire; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
|
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
7
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
|
8
|
|
|
|
|
9
|
|
|
class LivewireServiceProvider extends ServiceProvider |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Register the service provider. |
|
13
|
|
|
* |
|
14
|
|
|
* @return void |
|
15
|
|
|
*/ |
|
16
|
|
|
public function register() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->loadComponents(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get the services provided by the provider. |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public function provides() |
|
27
|
|
|
{ |
|
28
|
|
|
return []; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function loadComponents() |
|
32
|
|
|
{ |
|
33
|
|
|
if (! class_exists('Livewire')) return false; |
|
34
|
|
|
|
|
35
|
|
|
$modules = \Module::toCollection(); |
|
36
|
|
|
|
|
37
|
|
|
$defaultNamespace = GenerateConfigReader::read('livewire-namespace')->getPath(); |
|
38
|
|
|
|
|
39
|
|
|
$filesystem = new Filesystem(); |
|
40
|
|
|
|
|
41
|
|
|
$modules->map(function ($module) use ($filesystem, $defaultNamespace) { |
|
42
|
|
|
$modulePath = $module->getPath(); |
|
43
|
|
|
|
|
44
|
|
|
$path = $modulePath. '/'. strtr($defaultNamespace, ['\\' => '/']); |
|
45
|
|
|
|
|
46
|
|
|
$files = collect( $filesystem->isDirectory($path) ? $filesystem->allFiles($path) : [] ); |
|
47
|
|
|
|
|
48
|
|
|
$files->map(function ($file) use ($module, $path, $defaultNamespace) { |
|
49
|
|
|
$componentPath = \Str::after($file->getPathname(), $path.'/'); |
|
50
|
|
|
|
|
51
|
|
|
$componentClassPath = strtr( $componentPath , ['/' => '\\', '.php' => '']); |
|
52
|
|
|
|
|
53
|
|
|
$componentName = $this->getComponentName($componentClassPath, $module); |
|
54
|
|
|
|
|
55
|
|
|
$componentClassStr = '\\' . config('modules.namespace') . '\\' . $module->getName() . '\\' . $defaultNamespace . '\\' . $componentClassPath; |
|
56
|
|
|
|
|
57
|
|
|
$componentClass = get_class(new $componentClassStr); |
|
58
|
|
|
|
|
59
|
|
|
$loadComponent = \Livewire::component($componentName, $componentClass); |
|
|
|
|
|
|
60
|
|
|
}); |
|
61
|
|
|
}); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getComponentName($componentClassPath, $module = null) |
|
65
|
|
|
{ |
|
66
|
|
|
$dirs = explode('\\', $componentClassPath); |
|
67
|
|
|
|
|
68
|
|
|
$componentName = collect($dirs) |
|
69
|
|
|
->map([\Str::class, 'kebab']) |
|
70
|
|
|
->implode('.'); |
|
71
|
|
|
|
|
72
|
|
|
$moduleNamePrefix = ($module) ? $module->getLowerName().'::' : null; |
|
73
|
|
|
|
|
74
|
|
|
return $moduleNamePrefix . $componentName; |
|
75
|
|
|
} |
|
76
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.