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::regenerate(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
|
|
|
|
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
|
|
|
|
51
|
|
|
public static function regenerate() |
52
|
|
|
{ |
53
|
|
|
$values = []; |
54
|
|
|
/** @noinspection PhpInternalEntityUsedInspection */ |
|
|
|
|
55
|
|
|
$globals = Craft::$app->view->getTwig()->getGlobals(); |
56
|
|
|
/** @var CraftVariable $craftVariable */ |
|
|
|
|
57
|
|
|
if (isset($globals['craft'])) { |
58
|
|
|
$craftVariable = $globals['craft']; |
59
|
|
|
foreach ($craftVariable->getComponents() as $key => $value) { |
60
|
|
|
$type = gettype($value); |
61
|
|
|
switch ($type) { |
62
|
|
|
case 'object': |
|
|
|
|
63
|
|
|
$className = get_class($value); |
64
|
|
|
$values[$key] = $className; |
65
|
|
|
break; |
66
|
|
|
|
67
|
|
|
case 'array': |
|
|
|
|
68
|
|
|
if (isset($value['class'])) { |
|
|
|
|
69
|
|
|
$values[$key] = $value['class']; |
70
|
|
|
} |
|
|
|
|
71
|
|
|
break; |
72
|
|
|
|
73
|
|
|
case 'string': |
|
|
|
|
74
|
|
|
$values[$key] = $value; |
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach ($values as $key => $value) { |
82
|
|
|
$values[$key] = " * @property \\$value $$key"; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Save the template with variable substitution |
86
|
|
|
self::saveTemplate([ |
|
|
|
|
87
|
|
|
'{{ properties }}' => implode(PHP_EOL, $values), |
88
|
|
|
]); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|