1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Autocomplete plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Provides Twig template IDE autocomplete of Craft CMS & plugin variables |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @link https://putyourlightson.com |
9
|
|
|
* @copyright Copyright (c) 2021 nystudio107 |
|
|
|
|
10
|
|
|
* @copyright Copyright (c) 2021 PutYourLightsOn |
|
|
|
|
11
|
|
|
*/ |
|
|
|
|
12
|
|
|
|
13
|
|
|
namespace nystudio107\autocomplete\generators; |
14
|
|
|
|
15
|
|
|
use nystudio107\autocomplete\base\Generator; |
16
|
|
|
use nystudio107\autocomplete\events\DefineGeneratorValuesEvent; |
17
|
|
|
|
18
|
|
|
use Craft; |
19
|
|
|
use craft\base\Element; |
20
|
|
|
use craft\web\twig\GlobalsExtension; |
21
|
|
|
|
22
|
|
|
use yii\base\Event; |
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @author nystudio107 |
|
|
|
|
26
|
|
|
* @package autocomplete |
|
|
|
|
27
|
|
|
* @since 1.0.0 |
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
class AutocompleteTwigExtensionGenerator extends Generator |
30
|
|
|
{ |
31
|
|
|
// Constants |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
const ELEMENT_ROUTE_EXCLUDES = [ |
35
|
|
|
'matrixblock', |
36
|
|
|
'globalset' |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
// Public Static Methods |
40
|
|
|
// ========================================================================= |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
|
|
|
|
45
|
|
|
public static function getGeneratorName(): string |
46
|
|
|
{ |
47
|
|
|
return 'AutocompleteTwigExtension'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
|
|
|
|
53
|
|
|
public static function generate() |
54
|
|
|
{ |
55
|
|
|
if (self::shouldRegenerateFile()) { |
56
|
|
|
static::generateInternal(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
|
|
|
|
63
|
|
|
public static function regenerate() |
64
|
|
|
{ |
65
|
|
|
self::generateInternal(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Private Static Methods |
69
|
|
|
// ========================================================================= |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Core function that generates the autocomplete class |
73
|
|
|
*/ |
|
|
|
|
74
|
|
|
private static function generateInternal() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$values = []; |
77
|
|
|
// Iterate through the globals in the Twig context |
78
|
|
|
/* @noinspection PhpInternalEntityUsedInspection */ |
79
|
|
|
$globals = Craft::$app->view->getTwig()->getGlobals(); |
80
|
|
|
foreach ($globals as $key => $value) { |
81
|
|
|
$type = gettype($value); |
82
|
|
|
switch ($type) { |
83
|
|
|
case 'object': |
|
|
|
|
84
|
|
|
$values[$key] = 'new \\' . get_class($value) . '()'; |
85
|
|
|
break; |
86
|
|
|
|
87
|
|
|
case 'boolean': |
|
|
|
|
88
|
|
|
$values[$key] = $value ? 'true' : 'false'; |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
case 'integer': |
|
|
|
|
92
|
|
|
case 'double': |
|
|
|
|
93
|
|
|
$values[$key] = $value; |
94
|
|
|
break; |
95
|
|
|
|
96
|
|
|
case 'string': |
|
|
|
|
97
|
|
|
$values[$key] = "'" . addslashes($value) . "'"; |
98
|
|
|
break; |
99
|
|
|
|
100
|
|
|
case 'array': |
|
|
|
|
101
|
|
|
$values[$key] = '[]'; |
102
|
|
|
break; |
103
|
|
|
|
104
|
|
|
case 'NULL': |
|
|
|
|
105
|
|
|
$values[$key] = 'null'; |
106
|
|
|
break; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Mix in element route variables, and override values that should be used for autocompletion |
111
|
|
|
$values = array_merge( |
112
|
|
|
$values, |
113
|
|
|
static::elementRouteVariables(), |
114
|
|
|
static::globalVariables(), |
115
|
|
|
static::overrideValues() |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
// Allow plugins to modify the values |
119
|
|
|
$event = new DefineGeneratorValuesEvent([ |
|
|
|
|
120
|
|
|
'values' => $values, |
121
|
|
|
]); |
|
|
|
|
122
|
|
|
Event::trigger(self::class, self::EVENT_BEFORE_GENERATE, $event); |
123
|
|
|
$values = $event->values; |
124
|
|
|
|
125
|
|
|
// Format the line output for each value |
126
|
|
|
foreach ($values as $key => $value) { |
127
|
|
|
$values[$key] = " '" . $key . "' => " . $value . ","; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Save the template with variable substitution |
131
|
|
|
self::saveTemplate([ |
|
|
|
|
132
|
|
|
'{{ globals }}' => implode(PHP_EOL, $values), |
133
|
|
|
]); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Add in the element types that could be injected as route variables |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
private static function elementRouteVariables(): array |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$routeVariables = []; |
144
|
|
|
$elementTypes = Craft::$app->elements->getAllElementTypes(); |
|
|
|
|
145
|
|
|
foreach ($elementTypes as $elementType) { |
146
|
|
|
/* @var Element $elementType */ |
147
|
|
|
$key = $elementType::refHandle(); |
|
|
|
|
148
|
|
|
if (!empty($key) && !in_array($key, static::ELEMENT_ROUTE_EXCLUDES, true)) { |
149
|
|
|
$routeVariables[$key] = 'new \\' . $elementType . '()'; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $routeVariables; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Add in the global variables manually, because Craft conditionally loads the GlobalsExtension as of |
158
|
|
|
* Craft CMS 3.7.8 only for frontend routes |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
private static function globalVariables(): array |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$globalVariables = []; |
165
|
|
|
$globalsExtension = new GlobalsExtension(); |
166
|
|
|
foreach ($globalsExtension->getGlobals() as $key => $value) { |
167
|
|
|
$globalVariables[$key] = 'new \\' . get_class($value) . '()'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $globalVariables; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Override certain values that we always want hard-coded |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
private static function overrideValues(): array |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
return [ |
181
|
|
|
// Swap in our variable in place of the `craft` variable |
182
|
|
|
'craft' => 'new \nystudio107\autocomplete\variables\AutocompleteVariable()', |
183
|
|
|
// Set the current user to a new user, so it is never `null` |
184
|
|
|
'currentUser' => 'new \craft\elements\User()', |
185
|
|
|
// Set the nonce to a blank string, as it changes on every request |
186
|
|
|
'nonce' => "''", |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|