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; |
14
|
|
|
|
15
|
|
|
use nystudio107\autocomplete\base\Generator; |
16
|
|
|
use nystudio107\autocomplete\console\controllers\AutocompleteController; |
17
|
|
|
use nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator; |
18
|
|
|
use nystudio107\autocomplete\generators\AutocompleteVariableGenerator; |
19
|
|
|
|
20
|
|
|
use Craft; |
21
|
|
|
use craft\console\Application as CraftConsoleApp; |
22
|
|
|
use craft\events\RegisterComponentTypesEvent; |
23
|
|
|
use craft\services\Plugins; |
24
|
|
|
use craft\web\Application as CraftWebApp; |
25
|
|
|
|
26
|
|
|
use yii\base\Application as YiiApp; |
27
|
|
|
use yii\base\BootstrapInterface; |
28
|
|
|
use yii\base\Event; |
29
|
|
|
use yii\base\Module; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class Autocomplete |
33
|
|
|
* |
34
|
|
|
* @author nystudio107 |
|
|
|
|
35
|
|
|
* @package Autocomplete |
|
|
|
|
36
|
|
|
* @since 1.0.0 |
|
|
|
|
37
|
|
|
* |
38
|
|
|
* @property-read string[] $allAutocompleteGenerators |
39
|
|
|
*/ |
|
|
|
|
40
|
|
|
class Autocomplete extends Module implements BootstrapInterface |
41
|
|
|
{ |
42
|
|
|
// Constants |
43
|
|
|
// ========================================================================= |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @event RegisterComponentTypesEvent The event that is triggered when registering |
47
|
|
|
* Autocomplete Generator types |
48
|
|
|
* |
49
|
|
|
* Autocomplete Generator types must implement [[GeneratorInterface]]. [[Generator]] |
50
|
|
|
* provides a base implementation. |
51
|
|
|
* |
52
|
|
|
* ```php |
53
|
|
|
* use nystudio107\autocomplete\Autocomplete; |
54
|
|
|
* use craft\events\RegisterComponentTypesEvent; |
55
|
|
|
* use yii\base\Event; |
56
|
|
|
* |
57
|
|
|
* Event::on(Autocomplete::class, |
58
|
|
|
* Autocomplete::EVENT_REGISTER_AUTOCOMPLETE_GENERATORS, |
59
|
|
|
* function(RegisterComponentTypesEvent $event) { |
60
|
|
|
* $event->types[] = MyAutocompleteGenerator::class; |
61
|
|
|
* } |
62
|
|
|
* ); |
63
|
|
|
* ``` |
64
|
|
|
*/ |
65
|
|
|
const EVENT_REGISTER_AUTOCOMPLETE_GENERATORS = 'registerAutocompleteGenerators'; |
66
|
|
|
|
67
|
|
|
const DEFAULT_AUTOCOMPLETE_GENERATORS = [ |
68
|
|
|
AutocompleteVariableGenerator::class, |
69
|
|
|
AutocompleteTwigExtensionGenerator::class, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
// Private Properties |
73
|
|
|
// ========================================================================= |
74
|
|
|
|
75
|
|
|
private $allAutocompleteGenerators; |
|
|
|
|
76
|
|
|
|
77
|
|
|
// Public Methods |
78
|
|
|
// ========================================================================= |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Bootstraps the extension |
82
|
|
|
* |
83
|
|
|
* @param YiiApp $app |
|
|
|
|
84
|
|
|
*/ |
|
|
|
|
85
|
|
|
public function bootstrap($app) |
86
|
|
|
{ |
87
|
|
|
// Set the currently requested instance of this module class, |
88
|
|
|
// so we can later access it with `Autocomplete::getInstance()` |
89
|
|
|
static::setInstance($this); |
90
|
|
|
|
91
|
|
|
// Make sure it's Craft |
92
|
|
|
if (!($app instanceof CraftWebApp || $app instanceof CraftConsoleApp)) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
// Make sure we're in devMode |
96
|
|
|
if (!Craft::$app->config->general->devMode) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Register our event handlers |
101
|
|
|
$this->registerEventHandlers(); |
102
|
|
|
|
103
|
|
|
// Add our console controller |
104
|
|
|
if (Craft::$app->request->isConsoleRequest) { |
105
|
|
|
Craft::$app->controllerMap['autocomplete'] = AutocompleteController::class; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Registers our event handlers |
111
|
|
|
*/ |
|
|
|
|
112
|
|
|
public function registerEventHandlers() |
113
|
|
|
{ |
114
|
|
|
Event::on(Plugins::class,Plugins::EVENT_AFTER_INSTALL_PLUGIN, [$this, 'regenerateAutocompleteClasses']); |
115
|
|
|
Event::on(Plugins::class,Plugins::EVENT_AFTER_UNINSTALL_PLUGIN, [$this, 'deleteAutocompleteClasses']); |
116
|
|
|
Event::on(Plugins::class,Plugins::EVENT_AFTER_LOAD_PLUGINS, [$this, 'generateAutocompleteClasses']); |
117
|
|
|
Craft::info('Event Handlers installed',__METHOD__); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Call each of the autocomplete generator classes to tell them to generate their classes if they don't exist already |
122
|
|
|
*/ |
|
|
|
|
123
|
|
|
public function generateAutocompleteClasses() |
124
|
|
|
{ |
125
|
|
|
$autocompleteGenerators = $this->getAllAutocompleteGenerators(); |
126
|
|
|
foreach($autocompleteGenerators as $generatorClass) { |
|
|
|
|
127
|
|
|
/* @var Generator $generatorClass */ |
128
|
|
|
$generatorClass::generate(); |
129
|
|
|
} |
130
|
|
|
Craft::info('Autocomplete classes generated',__METHOD__); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Call each of the autocomplete generator classes to tell them to regenerate their classes from scratch |
135
|
|
|
*/ |
|
|
|
|
136
|
|
|
public function regenerateAutocompleteClasses() |
137
|
|
|
{ |
138
|
|
|
$autocompleteGenerators = $this->getAllAutocompleteGenerators(); |
139
|
|
|
foreach($autocompleteGenerators as $generatorClass) { |
|
|
|
|
140
|
|
|
/* @var Generator $generatorClass */ |
141
|
|
|
$generatorClass::regenerate(); |
142
|
|
|
} |
143
|
|
|
Craft::info('Autocomplete classes regenerated',__METHOD__); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Call each of the autocomplete generator classes to tell them to delete their classes |
148
|
|
|
*/ |
|
|
|
|
149
|
|
|
public function deleteAutocompleteClasses() |
150
|
|
|
{ |
151
|
|
|
$autocompleteGenerators = $this->getAllAutocompleteGenerators(); |
152
|
|
|
foreach($autocompleteGenerators as $generatorClass) { |
|
|
|
|
153
|
|
|
/* @var Generator $generatorClass */ |
154
|
|
|
$generatorClass::delete(); |
155
|
|
|
} |
156
|
|
|
Craft::info('Autocomplete classes deleted',__METHOD__); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// Protected Methods |
160
|
|
|
// ========================================================================= |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns all available autocomplete generator classes. |
164
|
|
|
* |
165
|
|
|
* @return string[] The available autocomplete generator classes |
166
|
|
|
*/ |
167
|
|
|
public function getAllAutocompleteGenerators(): array |
168
|
|
|
{ |
169
|
|
|
if ($this->allAutocompleteGenerators) { |
|
|
|
|
170
|
|
|
return $this->allAutocompleteGenerators; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$event = new RegisterComponentTypesEvent([ |
|
|
|
|
174
|
|
|
'types' => self::DEFAULT_AUTOCOMPLETE_GENERATORS |
175
|
|
|
]); |
|
|
|
|
176
|
|
|
$this->trigger(self::EVENT_REGISTER_AUTOCOMPLETE_GENERATORS, $event); |
177
|
|
|
$this->allAutocompleteGenerators = $event->types; |
|
|
|
|
178
|
|
|
|
179
|
|
|
return $this->allAutocompleteGenerators; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|