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