1
|
|
|
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators; |
2
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
4
|
|
|
use Savannabits\JetstreamInertiaGenerator\Generators\Traits\Helpers; |
5
|
|
|
use Savannabits\JetstreamInertiaGenerator\Generators\Traits\Names; |
6
|
|
|
use Savannabits\JetstreamInertiaGenerator\Generators\Traits\Columns; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Illuminate\Filesystem\Filesystem; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
abstract class FileAppender extends Command { |
14
|
|
|
|
15
|
|
|
use Helpers, Columns, Names; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Filesystem |
19
|
|
|
*/ |
20
|
|
|
protected Filesystem $files; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Relations |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $relations = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Create a new controller creator command instance. |
31
|
|
|
* |
32
|
|
|
* @param Filesystem $files |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Filesystem $files) |
35
|
|
|
{ |
36
|
|
|
parent::__construct(); |
37
|
|
|
|
38
|
|
|
$this->files = $files; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function getArguments() { |
42
|
|
|
return [ |
43
|
|
|
['table_name', InputArgument::REQUIRED, 'Name of the existing table'], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Append content to file only if if the content is not present in the file |
49
|
|
|
* |
50
|
|
|
* @param $path |
51
|
|
|
* @param $content |
52
|
|
|
* @param string $defaultContent content that will be used to populated with newly created file (in case it does not already exists) |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
protected function appendIfNotAlreadyAppended($path, $content, string $defaultContent = "<?php".PHP_EOL.PHP_EOL): bool |
56
|
|
|
{ |
57
|
|
|
if (!$this->files->exists($path)) { |
58
|
|
|
$this->makeDirectory($path); |
59
|
|
|
$this->files->put($path, $defaultContent.$content); |
60
|
|
|
} else if (!$this->alreadyAppended($path, $content)) { |
61
|
|
|
$this->files->append($path, $content); |
62
|
|
|
} else { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Append content to file only if if the content is not present in the file |
71
|
|
|
* |
72
|
|
|
* @param $path |
73
|
|
|
* @param $search |
74
|
|
|
* @param $replace |
75
|
|
|
* @param string $defaultContent content that will be used to populated with newly created file (in case it does not already exists) |
76
|
|
|
* @return bool |
77
|
|
|
* @throws FileNotFoundException |
78
|
|
|
*/ |
79
|
|
|
protected function replaceIfNotPresent($path, $search, $replace, string $defaultContent = "<?php".PHP_EOL.PHP_EOL): bool |
80
|
|
|
{ |
81
|
|
|
if (!$this->files->exists($path)) { |
82
|
|
|
$this->makeDirectory($path); |
83
|
|
|
$this->files->put($path, $defaultContent); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!$this->alreadyAppended($path, $replace)) { |
87
|
|
|
$this->files->put($path, str_replace($search, $replace, $this->files->get($path))); |
88
|
|
|
return true; |
89
|
|
|
} else { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Execute the console command. |
96
|
|
|
* |
97
|
|
|
* @param InputInterface $input |
98
|
|
|
* @param OutputInterface $output |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
102
|
|
|
{ |
103
|
|
|
$this->initCommonNames($this->argument('table_name'), $this->option('model-name'), $this->option('controller-name'), $this->option('model-with-full-namespace')); |
104
|
|
|
|
105
|
|
|
return parent::execute($input, $output); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|