1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chocofamily\LaravelEventSauce\Console; |
4
|
|
|
|
5
|
|
|
use Chocofamily\LaravelEventSauce\Exceptions\MakeFileFailed; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
abstract class MakeCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** @var Filesystem */ |
13
|
|
|
protected $filesystem; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* MakeCommand constructor. |
17
|
|
|
* @param Filesystem $files |
18
|
|
|
*/ |
19
|
|
|
public function __construct(Filesystem $files) |
20
|
|
|
{ |
21
|
|
|
parent::__construct(); |
22
|
|
|
$this->filesystem = $files; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $namespace |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
protected function formatClassName(string $namespace): string |
30
|
|
|
{ |
31
|
|
|
$name = ltrim($namespace, '\\/'); |
32
|
|
|
$name = str_replace('/', '\\', $name); |
33
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
34
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
35
|
|
|
return $name; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->formatClassName(trim($rootNamespace, '\\').'\\'.$name); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $name |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
protected function getPath(string $name): string |
46
|
|
|
{ |
47
|
|
|
$name = Str::replaceFirst($this->laravel->getNamespace(), '', $name); |
48
|
|
|
|
49
|
|
|
return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $paths |
54
|
|
|
* @throws MakeFileFailed |
55
|
|
|
*/ |
56
|
|
|
protected function ensureValidPaths(array $paths): void |
57
|
|
|
{ |
58
|
|
|
foreach ($paths as $path) { |
59
|
|
|
if (file_exists($path)) { |
60
|
|
|
$this->error("The file at path `{$path}` already exists."); |
61
|
|
|
throw MakeFileFailed::fileExists($path); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $path |
68
|
|
|
*/ |
69
|
|
|
protected function makeDirectory(string $path): void |
70
|
|
|
{ |
71
|
|
|
if (! $this->filesystem->isDirectory(dirname($path))) { |
72
|
|
|
$this->filesystem->makeDirectory(dirname($path), 0755, true, true); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $stubName |
78
|
|
|
* @param array $replacements |
79
|
|
|
* @return string |
80
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
81
|
|
|
*/ |
82
|
|
|
protected function getStubContent(string $stubName, array $replacements): string |
83
|
|
|
{ |
84
|
|
|
$content = $this->filesystem->get(__DIR__."/stubs/{$stubName}.php.stub"); |
85
|
|
|
foreach ($replacements as $search => $replace) { |
86
|
|
|
$content = str_replace("{{ {$search} }}", $replace, $content); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $content; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $paths |
94
|
|
|
* @param array $replacements |
95
|
|
|
*/ |
96
|
|
|
protected function makeFiles(array $paths, array $replacements): void |
97
|
|
|
{ |
98
|
|
|
collect($paths)->map(function (string $path, string $stubName) use ($replacements) { |
99
|
|
|
$this->filesystem->put($path, $this->getStubContent($stubName, $replacements)); |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|