|
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
|
|
|
|
|
19
|
|
|
/** |
|
|
|
|
|
|
20
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
21
|
|
|
* @package autocomplete |
|
|
|
|
|
|
22
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
23
|
|
|
*/ |
|
|
|
|
|
|
24
|
|
|
class AutocompleteTwigExtensionGenerator extends Generator |
|
25
|
|
|
{ |
|
26
|
|
|
// Public Static Methods |
|
27
|
|
|
// ========================================================================= |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
|
|
|
|
|
30
|
|
|
* @inheritDoc |
|
31
|
|
|
*/ |
|
|
|
|
|
|
32
|
|
|
public static function getGeneratorName(): string |
|
33
|
|
|
{ |
|
34
|
|
|
return 'AutocompleteTwigExtension'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
|
|
|
|
|
38
|
|
|
* @inheritDoc |
|
39
|
|
|
*/ |
|
|
|
|
|
|
40
|
|
|
public static function generate() |
|
41
|
|
|
{ |
|
42
|
|
|
// We always regenerate, to be context-sensitive based on the last page that was loaded/rendered |
|
43
|
|
|
static::regenerate(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
|
|
|
|
|
47
|
|
|
* @inheritDoc |
|
48
|
|
|
*/ |
|
|
|
|
|
|
49
|
|
|
public static function regenerate() |
|
50
|
|
|
{ |
|
51
|
|
|
$values = []; |
|
52
|
|
|
// Route variables are not merged in until the template is rendered, so do it here manually |
|
53
|
|
|
/* @noinspection PhpInternalEntityUsedInspection */ |
|
54
|
|
|
$globals = array_merge( |
|
55
|
|
|
Craft::$app->view->getTwig()->getGlobals(), |
|
56
|
|
|
Craft::$app->controller->actionParams['variables'] ?? [] |
|
57
|
|
|
); |
|
58
|
|
|
foreach ($globals as $key => $value) { |
|
59
|
|
|
$type = gettype($value); |
|
60
|
|
|
switch ($type) { |
|
61
|
|
|
case 'object': |
|
|
|
|
|
|
62
|
|
|
$values[$key] = 'new \\' . get_class($value) . '()'; |
|
63
|
|
|
break; |
|
64
|
|
|
|
|
65
|
|
|
case 'boolean': |
|
|
|
|
|
|
66
|
|
|
$values[$key] = $value ? 'true' : 'false'; |
|
67
|
|
|
break; |
|
68
|
|
|
|
|
69
|
|
|
case 'integer': |
|
|
|
|
|
|
70
|
|
|
case 'double': |
|
|
|
|
|
|
71
|
|
|
$values[$key] = $value; |
|
72
|
|
|
break; |
|
73
|
|
|
|
|
74
|
|
|
case 'string': |
|
|
|
|
|
|
75
|
|
|
$values[$key] = "'" . addslashes($value) . "'"; |
|
76
|
|
|
break; |
|
77
|
|
|
|
|
78
|
|
|
case 'array': |
|
|
|
|
|
|
79
|
|
|
$values[$key] = '[]'; |
|
80
|
|
|
break; |
|
81
|
|
|
|
|
82
|
|
|
case 'NULL': |
|
|
|
|
|
|
83
|
|
|
$values[$key] = 'null'; |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Override values that should be used for autocompletion |
|
89
|
|
|
static::overrideValues($values); |
|
90
|
|
|
|
|
91
|
|
|
// Format the line output for each value |
|
92
|
|
|
foreach ($values as $key => $value) { |
|
93
|
|
|
$values[$key] = " '" . $key . "' => " . $value . ","; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Save the template with variable substitution |
|
97
|
|
|
self::saveTemplate([ |
|
|
|
|
|
|
98
|
|
|
'{{ globals }}' => implode(PHP_EOL, $values), |
|
99
|
|
|
]); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Public Static Methods |
|
103
|
|
|
// ========================================================================= |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Override certain values that we always want hard-coded |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $values |
|
|
|
|
|
|
109
|
|
|
*/ |
|
|
|
|
|
|
110
|
|
|
private static function overrideValues(array &$values) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
// Swap in our variable in place of the `craft` variable |
|
113
|
|
|
$values['craft'] = 'new \nystudio107\autocomplete\variables\AutocompleteVariable()'; |
|
114
|
|
|
|
|
115
|
|
|
// Set the current user to a new user, so it is never `null` |
|
116
|
|
|
$values['currentUser'] = 'new \craft\elements\User()'; |
|
117
|
|
|
|
|
118
|
|
|
// Set the nonce to a blank string, as it changes on every request |
|
119
|
|
|
$values['nonce'] = "''"; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|