|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Inflector\InflectorFactory; |
|
6
|
|
|
use GraphQL\Type\Definition\Type; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Modelarium\Exception\Exception; |
|
9
|
|
|
use Modelarium\GeneratedCollection; |
|
10
|
|
|
use Modelarium\Parser; |
|
11
|
|
|
|
|
12
|
|
|
abstract class BaseGenerator implements GeneratorInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use GeneratorNameTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $stubDir = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Parser |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $parser = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Type |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $type = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Parser $parser |
|
33
|
|
|
* @param string $name The target type name. |
|
34
|
|
|
* @param Type|string $type |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Parser $parser, string $name, $type = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->parser = $parser; |
|
39
|
|
|
$this->setName($name); |
|
40
|
|
|
|
|
41
|
|
|
if ($type instanceof Type) { |
|
42
|
|
|
$this->type = $type; |
|
43
|
|
|
} elseif (!$type) { |
|
44
|
|
|
$this->type = $parser->getSchema()->getType($name); |
|
45
|
|
|
} else { |
|
46
|
|
|
throw new Exception('Invalid model'); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Stubs from a stub file. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $stubName The name for the stub file. |
|
54
|
|
|
* @param Callable $f |
|
55
|
|
|
* @return string |
|
56
|
|
|
* @see BaseGenerator::stubFile() |
|
57
|
|
|
* @throws \Safe\Exceptions\FilesystemException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function stubToString(string $stubName, callable $f = null): string |
|
60
|
|
|
{ |
|
61
|
|
|
$stub = \Safe\file_get_contents($this->stubDir . "/$stubName.stub.php"); |
|
62
|
|
|
return $this->replaceStub($stub, $f); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Stubs a string. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $stub |
|
69
|
|
|
* @param callable $f |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function replaceStub(string $stub, callable $f = null): string |
|
73
|
|
|
{ |
|
74
|
|
|
$data = $this->replaceDummy($stub); |
|
75
|
|
|
if ($f) { |
|
76
|
|
|
$data = $f($data); |
|
77
|
|
|
} |
|
78
|
|
|
return $data; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Replaces common strings from the stubs |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $str The string data to apply replaces |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function replaceDummy(string $str) |
|
88
|
|
|
{ |
|
89
|
|
|
$str = str_replace( |
|
90
|
|
|
'DummyClass', |
|
91
|
|
|
$this->studlyName, |
|
92
|
|
|
$str |
|
93
|
|
|
); |
|
94
|
|
|
$str = str_replace( |
|
95
|
|
|
'DummyName', |
|
96
|
|
|
$this->name, |
|
97
|
|
|
$str |
|
98
|
|
|
); |
|
99
|
|
|
$str = str_replace( |
|
100
|
|
|
'dummynameplural', |
|
101
|
|
|
$this->lowerNamePlural, |
|
102
|
|
|
$str |
|
103
|
|
|
); |
|
104
|
|
|
$str = str_replace( |
|
105
|
|
|
'dummyname', |
|
106
|
|
|
$this->lowerName, |
|
107
|
|
|
$str |
|
108
|
|
|
); |
|
109
|
|
|
return $str; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|