1 | <?php |
||
2 | |||
3 | namespace Luke\Migration\Config; |
||
4 | |||
5 | class FileCreate |
||
6 | { |
||
7 | private $arrayFilterKeys = ['create_', '_table', '_table_']; |
||
8 | |||
9 | public function createFile(string $fileName) |
||
10 | { |
||
11 | if (!file_exists(CONFIG_PATH)) { |
||
12 | mkdir(CONFIG_PATH, 0777, true); |
||
13 | } |
||
14 | |||
15 | if (file_exists(CONFIG_PATH . "/" . $fileName . ".php")) { |
||
16 | throw new \Exception("Arquivo ja existe"); |
||
17 | } |
||
18 | $file = fopen(CONFIG_PATH . "/" . $fileName . ".php", 'w+'); |
||
19 | if ($file) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
20 | $text = $this->copyFile(); |
||
21 | $lines = $this->modifyClassName($text, $fileName); |
||
22 | fwrite($file, $lines); |
||
23 | fclose($file); |
||
24 | } |
||
25 | } |
||
26 | |||
27 | private function copyFile(): string |
||
28 | { |
||
29 | return file_get_contents(__DIR__ . "/../stub/migration.stub"); |
||
30 | } |
||
31 | |||
32 | private function modifyClassName(string $text, string $table): string |
||
33 | { |
||
34 | foreach ($this->arrayFilterKeys as $key) { |
||
35 | $table = str_replace($key, '', $table); |
||
36 | } |
||
37 | |||
38 | return str_replace('{{ $table }}', $table, $text); |
||
39 | } |
||
40 | } |
||
41 |