Skip to content

Commit b91925f

Browse files
Refactor: Simplify component path handling and improve code readability in Livewire components
1 parent 2110d7c commit b91925f

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

src/Providers/LivewireComponentServiceProvider.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
use Livewire\Livewire;
1111
use Mhmiton\LaravelModulesLivewire\Support\ModuleVoltComponentRegistry;
1212
use Mhmiton\LaravelModulesLivewire\View\ModuleVoltViewFactory;
13+
use Nwidart\Modules\Traits\PathNamespace;
1314
use ReflectionClass;
1415
use Symfony\Component\Finder\SplFileInfo;
1516

1617
class LivewireComponentServiceProvider extends ServiceProvider
1718
{
19+
use PathNamespace;
20+
1821
/**
1922
* Register the service provider.
2023
*/
@@ -42,15 +45,13 @@ protected function registerModuleComponents()
4245
$modulesLivewireNamespace = config('livewire.class_namespace', 'App\\Livewire');
4346

4447
$modules->each(function ($module) use ($modulesLivewireNamespace) {
45-
$directory = (string) Str::of($module->getAppPath())
46-
->append('/'.$modulesLivewireNamespace)
47-
->replace(['\\'], '/');
48+
$directory = Str::of($module->app_path($modulesLivewireNamespace))->toString();
4849

4950
$moduleNamespace = method_exists($module, 'getNamespace')
5051
? $module->getNamespace()
5152
: config('modules.namespace', 'Modules');
5253

53-
$namespace = $moduleNamespace.'\\'.$module->getName().'\\'.$modulesLivewireNamespace;
54+
$namespace = $this->namespace($moduleNamespace.'\\'.$module->getName().'\\'.$modulesLivewireNamespace);
5455

5556
$this->registerComponentDirectory($directory, $namespace, $module->getLowerName().'::');
5657

src/Traits/LivewireComponentParser.php

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Str;
7+
use Nwidart\Modules\Helpers\Path;
78

89
trait LivewireComponentParser
910
{
@@ -13,6 +14,8 @@ trait LivewireComponentParser
1314

1415
protected $directories;
1516

17+
protected $file;
18+
1619
protected function parser(): self|bool
1720
{
1821
if (! $module = $this->getModule()) {
@@ -21,9 +24,10 @@ protected function parser(): self|bool
2124

2225
$this->module = $module;
2326

24-
$this->directories = collect(
25-
preg_split('/[.\/(\\\\)]+/', $this->argument('component'))
26-
)->map([Str::class, 'studly']);
27+
$this->file = Path::studly($this->argument('component'));
28+
29+
$this->directories = collect(preg_split('/[.\/(\\\\)]+/', Path::directory($this->argument('component'))))
30+
->map([Str::class, 'studly']);
2731

2832
$this->component = $this->getComponent();
2933

@@ -41,50 +45,36 @@ protected function getComponent()
4145

4246
protected function class()
4347
{
44-
$modulePath = $this->getModulePath(true);
45-
46-
$moduleLivewireNamespace = $this->getModuleLivewireNamespace();
47-
48-
$classDir = (string) Str::of($modulePath)
49-
->append('/'.$moduleLivewireNamespace)
50-
->replace(['\\'], '/');
51-
52-
$classPath = $this->directories->implode('/');
53-
54-
$namespace = $this->getNamespace($classPath);
55-
56-
$className = $this->directories->last();
57-
58-
$componentTag = $this->getComponentTag();
48+
$dir = $this->path($this->getModulePath($this->getModuleLivewirePath())); // todo: examine app/ path handling.
49+
$path = $this->directories->implode('/');
50+
$filename = Path::join($dir, $this->file);
5951

6052
return (object) [
61-
'dir' => $classDir,
62-
'path' => $classPath,
63-
'file' => $classDir.'/'.$classPath.'.php',
64-
'namespace' => $namespace,
65-
'name' => $className,
66-
'tag' => $componentTag,
53+
'name' => Path::filename($this->file),
54+
'path' => $path,
55+
'namespace' => $this->getNamespace($path),
56+
'file' => "{$filename}.php",
57+
'dir' => $dir,
58+
'tag' => $this->getComponentTag(),
6759
];
6860
}
6961

7062
protected function view()
7163
{
72-
$moduleLivewireViewDir = $this->getModuleLivewireViewDir();
73-
74-
$path = $this->directories
75-
->map([Str::class, 'kebab'])
76-
->implode('/');
77-
64+
$dir = $this->getModuleLivewireViewDir();
65+
$path = $this->directories->map([Str::class, 'kebab'])->implode('/');
7866
if ($this->option('view')) {
7967
$path = strtr($this->option('view'), ['.' => '/']);
8068
}
69+
$file = Path::lower($this->file);
70+
$filename = Path::join($dir, $file);
8171

8272
return (object) [
83-
'dir' => $moduleLivewireViewDir,
73+
'name' => strtr($file, ['/' => '.']),
8474
'path' => $path,
85-
'folder' => Str::after($moduleLivewireViewDir, 'views/'),
86-
'file' => $moduleLivewireViewDir.'/'.$path.'.blade.php',
87-
'name' => strtr($path, ['/' => '.']),
75+
'file' => "{$filename}.blade.php",
76+
'folder' => Str::after($dir, 'views/'),
77+
'dir' => $dir,
8878
];
8979
}
9080

@@ -184,20 +174,17 @@ protected function getViewSourcePath()
184174

185175
protected function getComponentTag()
186176
{
187-
$directoryAsView = $this->directories
188-
->map([Str::class, 'kebab'])
189-
->implode('.');
190-
177+
$directoryAsView = Str::of($this->file)->explode('/')->map([Str::class, 'kebab'])->implode('.');
191178
$tag = "<livewire:{$this->getModuleLowerName()}::{$directoryAsView} />";
192179

193-
$tagWithOutIndex = Str::replaceLast('.index', '', $tag);
194-
195-
return $tagWithOutIndex;
180+
return Str::replaceLast('.index', '', $tag);
196181
}
197182

198183
protected function getComponentQuote()
199184
{
200-
return "The <code>{$this->getClassName()}</code> livewire component is loaded from the ".($this->isCustomModule() ? 'custom ' : '')."<code>{$this->getModuleName()}</code> module.";
185+
$file = Str::of($this->file)->explode('/')->implode(' / ');
186+
187+
return "<code>{$this->getModuleName()}".($this->isCustomModule() ? ' (custom)' : '').": {$file}</code>";
201188
}
202189

203190
/**

0 commit comments

Comments
 (0)