|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace InnoFlash\LaraStart\Console\Commands\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
|
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use InnoFlash\LaraStart\Helper; |
|
10
|
|
|
|
|
11
|
|
|
abstract class MakeFile extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
abstract public function getStub(); |
|
14
|
|
|
|
|
15
|
|
|
abstract public function getFilename(); |
|
16
|
|
|
|
|
17
|
|
|
abstract public function getPath(); |
|
18
|
|
|
|
|
19
|
|
|
protected $filesystem; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(Filesystem $filesystem) |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct(); |
|
24
|
|
|
$this->filesystem = $filesystem; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Execute the console command. |
|
29
|
|
|
* |
|
30
|
|
|
* @return mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
public function handle() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->makeFile(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function makeFile() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->makeDir(); |
|
40
|
|
|
if (! $this->filesystem->isFile($this->getPath().'/'.$this->getFilename())) { |
|
41
|
|
|
$this->warn(Helper::getFileName($this->argument('name')).' created'); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
return $this->filesystem->put($this->getPath().'/'.$this->getFilename(), $this->getReplaceContent()); |
|
44
|
|
|
} else { |
|
45
|
|
|
$this->warn(Helper::getFileName($this->argument('name')).' already exist'); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function makeDir() |
|
53
|
|
|
{ |
|
54
|
|
|
if (! $this->filesystem->isDirectory($this->getPath())) { |
|
55
|
|
|
return $this->filesystem->makeDirectory($this->getPath(), 0755, true); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function getContent() |
|
60
|
|
|
{ |
|
61
|
|
|
try { |
|
62
|
|
|
return $this->filesystem->get($this->getStub()); |
|
63
|
|
|
} catch (FileNotFoundException $e) { |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function getReplaceContent() |
|
68
|
|
|
{ |
|
69
|
|
|
$content = $this->getContent(); |
|
70
|
|
|
$content = str_replace( |
|
71
|
|
|
$this->stringsToReplace(), |
|
72
|
|
|
$this->replaceContent(), |
|
73
|
|
|
$content |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
return $content; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function stringsToReplace() |
|
80
|
|
|
{ |
|
81
|
|
|
return [ |
|
82
|
|
|
'$servicePackage', |
|
83
|
|
|
'$namespaceModelName', |
|
84
|
|
|
'$filename', |
|
85
|
|
|
'modelObject', |
|
86
|
|
|
'model_object', |
|
87
|
|
|
'ModelName', |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function replaceContent() |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
|
|
Helper::getDirName($this->argument('name'), true), |
|
|
|
|
|
|
95
|
|
|
Helper::getModelNamespace($this->option('model')), |
|
96
|
|
|
Helper::getFileName($this->argument('name')), |
|
|
|
|
|
|
97
|
|
|
Str::camel(Helper::getFileName($this->option('model'))), |
|
98
|
|
|
\str_replace('-', '_', Str::kebab(Helper::getFileName($this->option('model')))), |
|
99
|
|
|
Helper::getFileName($this->option('model')), |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|