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
|
|
|
|
17
|
|
|
use Craft; |
18
|
|
|
use craft\base\Element; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
22
|
|
|
* @package autocomplete |
|
|
|
|
23
|
|
|
* @since 1.0.0 |
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
25
|
|
|
class AutocompleteTwigExtensionGenerator extends Generator |
26
|
|
|
{ |
27
|
|
|
// const |
28
|
|
|
// ========================================================================= |
29
|
|
|
|
30
|
|
|
const ELEMENT_ROUTE_EXCLUDES = [ |
31
|
|
|
'matrixblock', |
32
|
|
|
'globalset' |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
// Public Static Methods |
36
|
|
|
// ========================================================================= |
37
|
|
|
|
38
|
|
|
/** |
|
|
|
|
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
|
|
|
|
41
|
|
|
public static function getGeneratorName(): string |
42
|
|
|
{ |
43
|
|
|
return 'AutocompleteTwigExtension'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
|
|
|
|
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
|
|
|
|
49
|
|
|
public static function generate() |
50
|
|
|
{ |
51
|
|
|
// We always regenerate, to be context-sensitive based on the last page that was loaded/rendered |
52
|
|
|
self::generateInternal(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @inheritDoc |
57
|
|
|
*/ |
|
|
|
|
58
|
|
|
public static function regenerate() |
59
|
|
|
{ |
60
|
|
|
self::generateInternal(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Private Static Methods |
64
|
|
|
// ========================================================================= |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Core function that generates the autocomplete class |
68
|
|
|
*/ |
|
|
|
|
69
|
|
|
private static function generateInternal() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$values = []; |
72
|
|
|
// Iterate through the globals in the Twig context |
73
|
|
|
/* @noinspection PhpInternalEntityUsedInspection */ |
74
|
|
|
$globals = Craft::$app->view->getTwig()->getGlobals(); |
75
|
|
|
foreach ($globals as $key => $value) { |
76
|
|
|
$type = gettype($value); |
77
|
|
|
switch ($type) { |
78
|
|
|
case 'object': |
|
|
|
|
79
|
|
|
$values[$key] = 'new \\' . get_class($value) . '()'; |
80
|
|
|
break; |
81
|
|
|
|
82
|
|
|
case 'boolean': |
|
|
|
|
83
|
|
|
$values[$key] = $value ? 'true' : 'false'; |
84
|
|
|
break; |
85
|
|
|
|
86
|
|
|
case 'integer': |
|
|
|
|
87
|
|
|
case 'double': |
|
|
|
|
88
|
|
|
$values[$key] = $value; |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
case 'string': |
|
|
|
|
92
|
|
|
$values[$key] = "'" . addslashes($value) . "'"; |
93
|
|
|
break; |
94
|
|
|
|
95
|
|
|
case 'array': |
|
|
|
|
96
|
|
|
$values[$key] = '[]'; |
97
|
|
|
break; |
98
|
|
|
|
99
|
|
|
case 'NULL': |
|
|
|
|
100
|
|
|
$values[$key] = 'null'; |
101
|
|
|
break; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
// Mix in element route variables, and override values that should be used for autocompletion |
105
|
|
|
$values = array_merge( |
106
|
|
|
$values, |
107
|
|
|
static::elementRouteVariables(), |
108
|
|
|
static::overrideValues() |
109
|
|
|
); |
110
|
|
|
// Format the line output for each value |
111
|
|
|
foreach ($values as $key => $value) { |
112
|
|
|
$values[$key] = " '" . $key . "' => " . $value . ","; |
113
|
|
|
} |
114
|
|
|
// Save the template with variable substitution |
115
|
|
|
self::saveTemplate([ |
|
|
|
|
116
|
|
|
'{{ globals }}' => implode(PHP_EOL, $values), |
117
|
|
|
]); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Add in the element types that could be injected as route variables |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
private static function elementRouteVariables(): array |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$routeVariables = []; |
128
|
|
|
$elementTypes = Craft::$app->elements->getAllElementTypes(); |
|
|
|
|
129
|
|
|
foreach ($elementTypes as $elementType) { |
130
|
|
|
/* @var Element $elementType */ |
131
|
|
|
$key = $elementType::refHandle(); |
|
|
|
|
132
|
|
|
if (!empty($key) && !in_array($key, static::ELEMENT_ROUTE_EXCLUDES)) { |
133
|
|
|
$routeVariables[$key] = 'new \\' . $elementType . '()'; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $routeVariables; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Override certain values that we always want hard-coded |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
private static function overrideValues(): array |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
return [ |
148
|
|
|
// Swap in our variable in place of the `craft` variable |
149
|
|
|
'craft' => 'new \nystudio107\autocomplete\variables\AutocompleteVariable()', |
150
|
|
|
// Set the current user to a new user, so it is never `null` |
151
|
|
|
'currentUser' => 'new \craft\elements\User()', |
152
|
|
|
// Set the nonce to a blank string, as it changes on every request |
153
|
|
|
'nonce' => "''", |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|