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
|
|
|
self::generateInternal(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
|
|
|
|
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
|
|
|
|
49
|
|
|
public static function regenerate() |
50
|
|
|
{ |
51
|
|
|
self::generateInternal(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Private Static Methods |
55
|
|
|
// ========================================================================= |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Core function that generates the autocomplete class |
59
|
|
|
*/ |
|
|
|
|
60
|
|
|
private static function generateInternal() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$values = []; |
63
|
|
|
// Iterate through the globals in the Twig context |
64
|
|
|
/* @noinspection PhpInternalEntityUsedInspection */ |
65
|
|
|
$globals = Craft::$app->view->getTwig()->getGlobals(); |
66
|
|
|
foreach ($globals as $key => $value) { |
67
|
|
|
$type = gettype($value); |
68
|
|
|
switch ($type) { |
69
|
|
|
case 'object': |
|
|
|
|
70
|
|
|
$values[$key] = 'new \\' . get_class($value) . '()'; |
71
|
|
|
break; |
72
|
|
|
|
73
|
|
|
case 'boolean': |
|
|
|
|
74
|
|
|
$values[$key] = $value ? 'true' : 'false'; |
75
|
|
|
break; |
76
|
|
|
|
77
|
|
|
case 'integer': |
|
|
|
|
78
|
|
|
case 'double': |
|
|
|
|
79
|
|
|
$values[$key] = $value; |
80
|
|
|
break; |
81
|
|
|
|
82
|
|
|
case 'string': |
|
|
|
|
83
|
|
|
$values[$key] = "'" . addslashes($value) . "'"; |
84
|
|
|
break; |
85
|
|
|
|
86
|
|
|
case 'array': |
|
|
|
|
87
|
|
|
$values[$key] = '[]'; |
88
|
|
|
break; |
89
|
|
|
|
90
|
|
|
case 'NULL': |
|
|
|
|
91
|
|
|
$values[$key] = 'null'; |
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
// Override values that should be used for autocompletion |
96
|
|
|
$values = array_merge( |
97
|
|
|
$values, |
98
|
|
|
static::overrideValues() |
99
|
|
|
); |
100
|
|
|
// Format the line output for each value |
101
|
|
|
foreach ($values as $key => $value) { |
102
|
|
|
$values[$key] = " '" . $key . "' => " . $value . ","; |
103
|
|
|
} |
104
|
|
|
// Save the template with variable substitution |
105
|
|
|
self::saveTemplate([ |
|
|
|
|
106
|
|
|
'{{ globals }}' => implode(PHP_EOL, $values), |
107
|
|
|
]); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Override certain values that we always want hard-coded |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
private static function overrideValues(): array |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
// Swap in our variable in place of the `craft` variable |
119
|
|
|
'craft' => 'new \nystudio107\autocomplete\variables\AutocompleteVariable()', |
120
|
|
|
// Set the current user to a new user, so it is never `null` |
121
|
|
|
'currentUser' => 'new \craft\elements\User()', |
122
|
|
|
// Set the nonce to a blank string, as it changes on every request |
123
|
|
|
'nonce' => "''", |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|