Skip to content

Commit c51d53b

Browse files
Refactor: Simplify component path handling and improve code readability in Livewire components
1 parent a708a8a commit c51d53b

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
{
@@ -15,6 +16,8 @@ trait LivewireComponentParser
1516

1617
protected $directories;
1718

19+
protected $file;
20+
1821
protected function parser(): self|bool
1922
{
2023
if (! $module = $this->getModule()) {
@@ -23,9 +26,10 @@ protected function parser(): self|bool
2326

2427
$this->module = $module;
2528

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

3034
$this->component = $this->getComponent();
3135

@@ -43,50 +47,36 @@ protected function getComponent()
4347

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

6254
return (object) [
63-
'dir' => $classDir,
64-
'path' => $classPath,
65-
'file' => $classDir.'/'.$classPath.'.php',
66-
'namespace' => $namespace,
67-
'name' => $className,
68-
'tag' => $componentTag,
55+
'name' => Path::filename($this->file),
56+
'path' => $path,
57+
'namespace' => $this->getNamespace($path),
58+
'file' => "{$filename}.php",
59+
'dir' => $dir,
60+
'tag' => $this->getComponentTag(),
6961
];
7062
}
7163

7264
protected function view()
7365
{
74-
$moduleLivewireViewDir = $this->getModuleLivewireViewDir();
75-
76-
$path = $this->directories
77-
->map([Str::class, 'kebab'])
78-
->implode('/');
79-
66+
$dir = $this->getModuleLivewireViewDir();
67+
$path = $this->directories->map([Str::class, 'kebab'])->implode('/');
8068
if ($this->option('view')) {
8169
$path = strtr($this->option('view'), ['.' => '/']);
8270
}
71+
$file = Path::lower($this->file);
72+
$filename = Path::join($dir, $file);
8373

8474
return (object) [
85-
'dir' => $moduleLivewireViewDir,
75+
'name' => strtr($file, ['/' => '.']),
8676
'path' => $path,
87-
'folder' => Str::after($moduleLivewireViewDir, 'views/'),
88-
'file' => $moduleLivewireViewDir.'/'.$path.'.blade.php',
89-
'name' => strtr($path, ['/' => '.']),
77+
'file' => "{$filename}.blade.php",
78+
'folder' => Str::after($dir, 'views/'),
79+
'dir' => $dir,
9080
];
9181
}
9282

@@ -186,20 +176,17 @@ protected function getViewSourcePath()
186176

187177
protected function getComponentTag()
188178
{
189-
$directoryAsView = $this->directories
190-
->map([Str::class, 'kebab'])
191-
->implode('.');
192-
179+
$directoryAsView = Str::of($this->file)->explode('/')->map([Str::class, 'kebab'])->implode('.');
193180
$tag = "<livewire:{$this->getModuleLowerName()}::{$directoryAsView} />";
194181

195-
$tagWithOutIndex = Str::replaceLast('.index', '', $tag);
196-
197-
return $tagWithOutIndex;
182+
return Str::replaceLast('.index', '', $tag);
198183
}
199184

200185
protected function getComponentQuote()
201186
{
202-
return "The <code>{$this->getClassName()}</code> livewire component is loaded from the ".($this->isCustomModule() ? 'custom ' : '')."<code>{$this->getModuleName()}</code> module.";
187+
$file = Str::of($this->file)->explode('/')->implode(' / ');
188+
189+
return "<code>{$this->getModuleName()}".($this->isCustomModule() ? ' (custom)' : '').": {$file}</code>";
203190
}
204191

205192
/**

0 commit comments

Comments
 (0)