-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathBundleGenerator.php
250 lines (219 loc) · 6.88 KB
/
BundleGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* @package s9e\TextFormatter
* @copyright Copyright (c) The s9e authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Configurator\RendererGenerators\PHP;
class BundleGenerator
{
/**
* @var Configurator Configurator this instance belongs to
*/
protected $configurator;
/**
* @var callable Callback used to serialize the objects
*/
public $serializer = 'serialize';
/**
* @var string Callback used to unserialize the serialized objects (must be a string)
*/
public $unserializer = 'unserialize';
/**
* Constructor
*
* @param Configurator $configurator Configurator
*/
public function __construct(Configurator $configurator)
{
$this->configurator = $configurator;
}
/**
* Create and return the source of a bundle based on given Configurator instance
*
* Options:
*
* - autoInclude: automatically load the source of the PHP renderer (default: true)
*
* @param string $className Name of the bundle class
* @param array $options Associative array of optional settings
* @return string PHP source for the bundle
*/
public function generate($className, array $options = [])
{
// Add default options
$options += ['autoInclude' => true];
// Copy the PHP files header if applicable
if ($this->configurator->rendering->engine instanceof PHP)
{
$this->configurator->rendering->engine->phpHeader = $this->configurator->phpHeader;
}
// Get the parser and renderer
$objects = $this->configurator->finalize();
$parser = $objects['parser'];
$renderer = $objects['renderer'];
// Split the bundle's class name and its namespace
$namespace = '';
if (preg_match('#(.*)\\\\([^\\\\]+)$#', $className, $m))
{
$namespace = $m[1];
$className = $m[2];
}
// Start with the standard header
$php = [];
$php[] = $this->configurator->phpHeader;
if ($namespace)
{
$php[] = 'namespace ' . $namespace . ';';
$php[] = '';
}
// Generate and append the bundle class
$php[] = 'abstract class ' . $className . ' extends \\s9e\\TextFormatter\\Bundle';
$php[] = '{';
$php[] = ' /**';
$php[] = ' * @var ?\\s9e\\TextFormatter\\Parser Singleton instance used by parse()';
$php[] = ' */';
$php[] = ' protected static ?\\s9e\\TextFormatter\\Parser $parser;';
$php[] = '';
$php[] = ' /**';
$php[] = ' * @var ?\\s9e\\TextFormatter\\Renderer Singleton instance used by render()';
$php[] = ' */';
$php[] = ' protected static ?\\s9e\\TextFormatter\\Renderer $renderer;';
$php[] = '';
// Add the event callbacks if applicable
$events = [
'beforeParse'
=> 'Callback executed before parse(), receives the original text as argument',
'afterParse'
=> 'Callback executed after parse(), receives the parsed text as argument',
'beforeRender'
=> 'Callback executed before render(), receives the parsed text as argument',
'afterRender'
=> 'Callback executed after render(), receives the output as argument',
'beforeUnparse'
=> 'Callback executed before unparse(), receives the parsed text as argument',
'afterUnparse'
=> 'Callback executed after unparse(), receives the original text as argument'
];
foreach ($events as $eventName => $eventDesc)
{
if (isset($options[$eventName]))
{
$php[] = ' /**';
$php[] = ' * @var ' . $eventDesc;
$php[] = ' */';
$php[] = ' public static $' . $eventName . ' = ' . var_export($options[$eventName], true) . ';';
$php[] = '';
}
}
if (isset($objects['js']))
{
$php[] = ' /**';
$php[] = ' * {@inheritdoc}';
$php[] = ' */';
$php[] = ' public static function getJS(): string';
$php[] = ' {';
$php[] = ' return ' . var_export($objects['js'], true) . ';';
$php[] = ' }';
$php[] = '';
}
$php[] = ' /**';
$php[] = ' * {@inheritdoc}';
$php[] = ' */';
$php[] = ' public static function getParser(): \\s9e\\TextFormatter\\Parser';
$php[] = ' {';
if (isset($options['parserSetup']))
{
$php[] = ' $parser = ' . $this->exportObject($parser) . ';';
$php[] = ' ' . $this->exportCallback($namespace, $options['parserSetup'], '$parser') . ';';
$php[] = '';
$php[] = ' return $parser;';
}
else
{
$php[] = ' return ' . $this->exportObject($parser) . ';';
}
$php[] = ' }';
$php[] = '';
$php[] = ' /**';
$php[] = ' * {@inheritdoc}';
$php[] = ' */';
$php[] = ' public static function getRenderer(): \\s9e\\TextFormatter\\Renderer';
$php[] = ' {';
// If this is a PHP renderer and we know where it's saved, automatically load it as needed
if (!empty($options['autoInclude'])
&& $this->configurator->rendering->engine instanceof PHP
&& isset($this->configurator->rendering->engine->lastFilepath))
{
$className = get_class($renderer);
$filepath = realpath($this->configurator->rendering->engine->lastFilepath);
$php[] = ' if (!class_exists(' . var_export($className, true) . ', false)';
$php[] = ' && file_exists(' . var_export($filepath, true) . '))';
$php[] = ' {';
$php[] = ' include ' . var_export($filepath, true) . ';';
$php[] = ' }';
$php[] = '';
}
if (isset($options['rendererSetup']))
{
$php[] = ' $renderer = ' . $this->exportObject($renderer) . ';';
$php[] = ' ' . $this->exportCallback($namespace, $options['rendererSetup'], '$renderer') . ';';
$php[] = '';
$php[] = ' return $renderer;';
}
else
{
$php[] = ' return ' . $this->exportObject($renderer) . ';';
}
$php[] = ' }';
$php[] = '}';
return implode("\n", $php);
}
/**
* Export a given callback as PHP code
*
* @param string $namespace Namespace in which the callback is execute
* @param callable $callback Original callback
* @param string $argument Callback's argument (as PHP code)
* @return string PHP code
*/
protected function exportCallback($namespace, callable $callback, $argument)
{
if (is_array($callback) && is_string($callback[0]))
{
// Replace ['foo', 'bar'] with 'foo::bar'
$callback = $callback[0] . '::' . $callback[1];
}
if (!is_string($callback))
{
return 'call_user_func(' . var_export($callback, true) . ', ' . $argument . ')';
}
// Ensure that the callback starts with a \
if ($callback[0] !== '\\')
{
$callback = '\\' . $callback;
}
// Replace \foo\bar::baz() with bar::baz() if we're in namespace foo
if (substr($callback, 0, 2 + strlen($namespace)) === '\\' . $namespace . '\\')
{
$callback = substr($callback, 2 + strlen($namespace));
}
return $callback . '(' . $argument . ')';
}
/**
* Serialize and export a given object as PHP code
*
* @param object $obj Original object
* @return string PHP code
*/
protected function exportObject($obj)
{
// Serialize the object
$str = call_user_func($this->serializer, $obj);
// Export the object's source
$str = var_export($str, true);
return $this->unserializer . '(' . $str . ')';
}
}