1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium; |
4
|
|
|
|
5
|
|
|
use Doctrine\Inflector\InflectorFactory; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use LightnCandy\LightnCandy; |
8
|
|
|
use Modelarium\Exception\Exception; |
9
|
|
|
use function Safe\date; |
10
|
|
|
|
11
|
|
|
trait GeneratorNameTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $baseName = ''; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $studlyName = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $lowerName = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $lowerNamePlural = ''; |
32
|
27 |
|
|
33
|
|
|
public function getInflector(): \Doctrine\Inflector\Inflector |
34
|
27 |
|
{ |
35
|
27 |
|
static $inflector = null; |
36
|
6 |
|
if (!$inflector) { |
37
|
|
|
$inflector = InflectorFactory::create()->build(); |
38
|
27 |
|
} |
39
|
|
|
return $inflector; |
40
|
|
|
} |
41
|
27 |
|
|
42
|
|
|
protected function setBaseName(string $name): void |
43
|
27 |
|
{ |
44
|
27 |
|
$this->baseName = $name; |
45
|
27 |
|
$this->studlyName = Str::studly($this->baseName); |
46
|
27 |
|
$this->lowerName = mb_strtolower($this->baseName); |
47
|
27 |
|
$this->lowerNamePlural = $this->getInflector()->pluralize($this->lowerName); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Splits a fully qualified class name into its namespace, class name and relative path |
52
|
|
|
* |
53
|
|
|
* @param string $fullclass |
54
|
|
|
* @return array |
55
|
2 |
|
*/ |
56
|
|
|
protected static function splitClassName(string $fullclass): array |
57
|
2 |
|
{ |
58
|
2 |
|
$classTokens = explode('\\', $fullclass); |
59
|
2 |
|
$className = array_pop($classTokens); |
60
|
2 |
|
$classNamespace = implode('\\', $classTokens); |
61
|
2 |
|
$relativePath = implode('/', $classTokens); |
62
|
|
|
return [$classNamespace, $className, $relativePath]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the base path (where composer.json is located) |
67
|
|
|
* |
68
|
|
|
* @param string $file The filename |
69
|
|
|
* @return string |
70
|
12 |
|
*/ |
71
|
|
|
public static function getBasePath(string $file = null): string |
72
|
12 |
|
{ |
73
|
12 |
|
$basepath = dirname(\Composer\Factory::getComposerFile()); |
74
|
12 |
|
if ($file) { |
75
|
|
|
$basepath .= '/' . $file; |
76
|
12 |
|
} |
77
|
|
|
return $basepath; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the value of name |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getBaseName() |
86
|
|
|
{ |
87
|
|
|
return $this->baseName; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the value of studlyName |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
8 |
|
*/ |
95
|
|
|
public function getStudlyName() |
96
|
8 |
|
{ |
97
|
|
|
return $this->studlyName; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
* @param string $path |
103
|
|
|
* @return callable |
104
|
25 |
|
*/ |
105
|
|
|
protected function compileMustacheFromFile(string $path) |
106
|
25 |
|
{ |
107
|
25 |
|
$template = \Safe\file_get_contents($path); |
108
|
25 |
|
$phpStr = LightnCandy::compile( |
109
|
|
|
$template, |
110
|
25 |
|
[ |
|
|
|
|
111
|
|
|
'flags' => LightnCandy::FLAG_ERROR_EXCEPTION, |
112
|
|
|
'delimiters' => array('{|', '|}') |
113
|
|
|
] |
114
|
25 |
|
); |
115
|
|
|
if (!$phpStr) { |
116
|
|
|
throw new Exception('Invalid template'); |
117
|
|
|
} |
118
|
25 |
|
/** @var callable $renderer */ |
119
|
25 |
|
$renderer = LightnCandy::prepare($phpStr); |
|
|
|
|
120
|
|
|
return $renderer; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Replaces common strings from the stubs |
125
|
|
|
* |
126
|
|
|
* @param string $path The string data to apply replaces |
127
|
|
|
* @param array $context |
128
|
|
|
* @return string |
129
|
25 |
|
*/ |
130
|
|
|
public function templateFile(string $path, array $context = []) |
131
|
25 |
|
{ |
132
|
25 |
|
$renderer = $this->compileMustacheFromFile($path); |
133
|
25 |
|
$context['StudlyName'] = $context['studlyName'] = $this->studlyName; |
134
|
25 |
|
$context['lowerName'] = $this->lowerName; |
135
|
25 |
|
$context['lowerNamePlural'] = $this->lowerNamePlural; |
136
|
|
|
$context['date'] = date("c"); |
137
|
25 |
|
|
138
|
|
|
return $renderer($context); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Stubs from a mustache file. Convenience wrapper for templateFile(). |
143
|
|
|
* |
144
|
|
|
* @param string $stubName |
145
|
|
|
* @param array $context |
146
|
|
|
* @return string |
147
|
25 |
|
*/ |
148
|
|
|
public function templateStub(string $stubName, array $context = []): string |
149
|
25 |
|
{ |
150
|
25 |
|
$stub = $this->stubDir . "/$stubName.mustache.php"; |
151
|
|
|
return $this->templateFile($stub, $context); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get the value of lowerName |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getLowerName() |
160
|
|
|
{ |
161
|
|
|
return $this->lowerName; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|