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\web\twig\variables\CraftVariable; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
22
|
|
|
* @package autocomplete |
|
|
|
|
23
|
|
|
* @since 1.0.0 |
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
25
|
|
|
class AutocompleteVariableGenerator extends Generator |
26
|
|
|
{ |
27
|
|
|
// Public Static Methods |
28
|
|
|
// ========================================================================= |
29
|
|
|
|
30
|
|
|
/** |
|
|
|
|
31
|
|
|
* @inheritDoc |
32
|
|
|
*/ |
|
|
|
|
33
|
|
|
public static function getGeneratorName(): string |
34
|
|
|
{ |
35
|
|
|
return 'AutocompleteVariable'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
|
|
|
|
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
|
|
|
|
41
|
|
|
public static function generate() |
42
|
|
|
{ |
43
|
|
|
if (self::shouldRegenerateFile()) { |
44
|
|
|
static::generateInternal(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
|
|
|
|
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
|
|
|
|
51
|
|
|
public static function regenerate() |
52
|
|
|
{ |
53
|
|
|
static::generateInternal(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Private Static Methods |
57
|
|
|
// ========================================================================= |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Core function that generates the autocomplete class |
61
|
|
|
*/ |
|
|
|
|
62
|
|
|
private static function generateInternal() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$values = []; |
65
|
|
|
/* @noinspection PhpInternalEntityUsedInspection */ |
66
|
|
|
$globals = Craft::$app->view->getTwig()->getGlobals(); |
67
|
|
|
/* @var CraftVariable $craftVariable */ |
68
|
|
|
if (isset($globals['craft'])) { |
69
|
|
|
$craftVariable = $globals['craft']; |
70
|
|
|
foreach ($craftVariable->getComponents() as $key => $value) { |
71
|
|
|
$type = gettype($value); |
72
|
|
|
switch ($type) { |
73
|
|
|
case 'object': |
|
|
|
|
74
|
|
|
$className = get_class($value); |
75
|
|
|
$values[$key] = $className; |
76
|
|
|
break; |
77
|
|
|
|
78
|
|
|
case 'array': |
|
|
|
|
79
|
|
|
if (isset($value['class'])) { |
|
|
|
|
80
|
|
|
$values[$key] = $value['class']; |
81
|
|
|
} |
|
|
|
|
82
|
|
|
break; |
83
|
|
|
|
84
|
|
|
case 'string': |
|
|
|
|
85
|
|
|
$values[$key] = $value; |
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Format the line output for each value |
92
|
|
|
foreach ($values as $key => $value) { |
93
|
|
|
$values[$key] = ' * @property \\' . $value . ' $' . $key; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Save the template with variable substitution |
97
|
|
|
self::saveTemplate([ |
|
|
|
|
98
|
|
|
'{{ properties }}' => implode(PHP_EOL, $values), |
99
|
|
|
]); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|