|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Micro framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Micro\Library\DTO\Writer; |
|
15
|
|
|
|
|
16
|
|
|
class WriterFilesystem implements WriterInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $classFilePath |
|
20
|
|
|
* @param string $namespaceGeneral |
|
21
|
|
|
*/ |
|
22
|
6 |
|
public function __construct( |
|
23
|
|
|
private string $classFilePath, |
|
24
|
|
|
private string $namespaceGeneral, |
|
25
|
|
|
) { |
|
26
|
6 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $classnameFull |
|
30
|
|
|
* @param string $renderedClassData |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \Exception |
|
33
|
|
|
* |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function write(string $classnameFull, string $renderedClassData): void |
|
37
|
|
|
{ |
|
38
|
1 |
|
$meta = $this->getClassFileMeta($classnameFull); |
|
39
|
1 |
|
$file = $meta['file']; |
|
40
|
1 |
|
$path = $meta['path']; |
|
41
|
|
|
|
|
42
|
1 |
|
$this->createPath($path); |
|
43
|
|
|
|
|
44
|
1 |
|
file_put_contents($path.\DIRECTORY_SEPARATOR.$file, $renderedClassData); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $classnameFull |
|
49
|
|
|
* |
|
50
|
|
|
* @return array<string, string> |
|
51
|
|
|
*/ |
|
52
|
1 |
|
protected function getClassFileMeta(string $classnameFull): array |
|
53
|
|
|
{ |
|
54
|
1 |
|
$namespaceGeneralExploded = explode('\\', $this->namespaceGeneral); |
|
55
|
1 |
|
$classExploded = explode('\\', $classnameFull); |
|
56
|
1 |
|
$explodedFile = array_values(array_diff($classExploded, $namespaceGeneralExploded)); |
|
57
|
|
|
|
|
58
|
1 |
|
$file = $explodedFile[0]; |
|
59
|
1 |
|
$path = rtrim($this->classFilePath, '\\'); |
|
60
|
1 |
|
if (1 !== \count($classExploded)) { |
|
61
|
1 |
|
$file = array_pop($explodedFile); |
|
62
|
1 |
|
$path = $path.'\\'.implode(\DIRECTORY_SEPARATOR, $explodedFile); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
return [ |
|
66
|
1 |
|
'file' => $file.'.php', |
|
67
|
1 |
|
'path' => str_replace('\\', '/', $path), |
|
68
|
1 |
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $path |
|
73
|
|
|
* |
|
74
|
|
|
* @throws \Exception |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
1 |
|
protected function createPath(string $path): void |
|
79
|
|
|
{ |
|
80
|
1 |
|
$path = preg_replace('(\/+\/)', '/', $path); |
|
81
|
|
|
|
|
82
|
1 |
|
if (file_exists($path)) { |
|
83
|
1 |
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
if (!mkdir($path, 0755, true)) { |
|
87
|
|
|
throw new \Exception(sprintf('Can not create path %s', $path)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|