/
ChangeDefaultNamespace.php
115 lines (90 loc) · 4.28 KB
/
ChangeDefaultNamespace.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Livewire\Features\SupportConsoleCommands\Commands\Upgrade;
use Livewire\Features\SupportConsoleCommands\Commands\ComponentParser;
use Livewire\Features\SupportConsoleCommands\Commands\ComponentParserFromExistingComponent;
use Livewire\Features\SupportConsoleCommands\Commands\UpgradeCommand;
class ChangeDefaultNamespace extends UpgradeStep
{
public function handle(UpgradeCommand $console, \Closure $next)
{
if($this->hasOldNamespace())
{
$console->line("<fg=#FB70A9;bg=black;options=bold,reverse> The Livewire namespace has changed. </>");
$console->newLine();
$console->line('The <options=underscore>App\\Http\\Livewire</> namespace was detected and is no longer the default in Livewire v3. Livewire v3 now uses the <options=underscore>App\\Livewire</> namespace.');
$choice = $console->choice('Would you like to migrate or keep the old namespace?', [
'migrate',
'keep',
], 'migrate');
if($choice === 'keep') {
$console->line('Keeping the old namespace...');
$this->publishConfigIfMissing($console);
$console->line('Setting the default namespace to "App\\Http\\Livewire"...');
$config = $this->filesystem()->get('config/livewire.php');
$config = str_replace('App\\\\Livewire', 'App\\\\Http\\\\Livewire', $config);
$this->filesystem()->put('config/livewire.php', $config);
return $next($console);
}
$componentNames = [];
$results = collect($this->filesystem()->allFiles('app/Http/Livewire'))
->filter(function($file) {
return str($file)->endsWith('.php');
})
->map(function($file) {
return str($file)->after('app/Http/Livewire/')->before('.php')->__toString();
})->map(function($component) use (&$componentNames) {
// Track component names to update namespace references later on.
$componentNames[] = $component;
$parser = new ComponentParser(
'App\\Http\\Livewire',
config('livewire.view_path'),
$component,
);
$newParser = new ComponentParserFromExistingComponent(
'App\\Livewire',
config('livewire.view_path'),
$component,
$parser
);
if ($this->filesystem()->exists($newParser->relativeClassPath())) {
return ['Skipped', $component, 'Already exists'];
}
if($this->filesystem()->directoryMissing(dirname($newParser->relativeClassPath()))) {
$this->filesystem()->createDirectory(dirname($newParser->relativeClassPath()));
}
$this->filesystem()->put($newParser->relativeClassPath(), $newParser->classContents());
$this->filesystem()->delete($parser->relativeClassPath());
return ['Migrated', $component];
});
foreach($componentNames as $name) {
$name = str($name)->replace('/', '\\\\')->toString();
// Update any namespace references
$this->patternReplacement(
pattern: "/App\\\Http\\\Livewire\\\({$name})/",
replacement: 'App\Livewire\\\$1',
directories: [
'app',
'resources/views',
'routes',
'tests',
]
);
}
// Update vite config
$this->patternReplacement(
pattern: '/App\/Http\/Livewire/',
replacement: 'App/Livewire',
mode: 'manual',
files: 'vite.config.js'
);
$console->table(
['Status', 'Component', 'Remark'], $results
);
}
return $next($console);
}
protected function hasOldNamespace()
{
return $this->filesystem()->exists('app/Http/Livewire') || config('livewire.class_namespace') === 'App\\Http\\Livewire';
}
}