1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jellyfish\Transfer\Generator; |
6
|
|
|
|
7
|
|
|
use Jellyfish\Transfer\Definition\ClassDefinitionInterface; |
8
|
|
|
|
9
|
|
|
abstract class AbstractClassGenerator extends AbstractGenerator implements ClassGeneratorInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param \Jellyfish\Transfer\Definition\ClassDefinitionInterface $classDefinition |
13
|
|
|
* |
14
|
|
|
* @return \Jellyfish\Transfer\Generator\ClassGeneratorInterface |
15
|
|
|
* |
16
|
|
|
* @throws \Twig\Error\LoaderError |
17
|
|
|
* @throws \Twig\Error\RuntimeError |
18
|
|
|
* @throws \Twig\Error\SyntaxError |
19
|
|
|
*/ |
20
|
|
|
public function generate(ClassDefinitionInterface $classDefinition): ClassGeneratorInterface |
21
|
|
|
{ |
22
|
|
|
$file = $this->getFile($classDefinition); |
23
|
|
|
$pathToFile = $this->getPathToFile($classDefinition); |
24
|
|
|
$fileContent = $this->twig->render($this->getTemplateName(), [ |
25
|
|
|
'classDefinition' => $classDefinition |
26
|
|
|
]); |
27
|
|
|
|
28
|
|
|
$this->createDirectories($pathToFile); |
29
|
|
|
|
30
|
|
|
$this->filesystem->writeToFile($pathToFile . $file, $fileContent); |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param \Jellyfish\Transfer\Definition\ClassDefinitionInterface $classDefinition |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
abstract protected function getFile(ClassDefinitionInterface $classDefinition): string; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param \Jellyfish\Transfer\Definition\ClassDefinitionInterface $classDefinition |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
protected function getPathToFile(ClassDefinitionInterface $classDefinition): string |
48
|
|
|
{ |
49
|
|
|
$namespace = $classDefinition->getNamespace(); |
50
|
|
|
|
51
|
|
|
if ($namespace === null) { |
52
|
|
|
return $this->targetDirectory; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$namespaceParts = \explode('\\', $namespace); |
56
|
|
|
|
57
|
|
|
return $this->targetDirectory . \implode(DIRECTORY_SEPARATOR, $namespaceParts) . DIRECTORY_SEPARATOR; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|