1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Twigfield for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Provides a twig editor field with Twig & Craft API autocomplete |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2022 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\twigfield\services; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\Component; |
15
|
|
|
use craft\events\SectionEvent; |
16
|
|
|
use craft\services\Fields; |
17
|
|
|
use nystudio107\twigfield\autocompletes\SectionShorthandFieldsAutocomplete; |
18
|
|
|
use nystudio107\twigfield\base\Autocomplete as BaseAutoComplete; |
19
|
|
|
use nystudio107\twigfield\base\AutocompleteInterface; |
20
|
|
|
use nystudio107\twigfield\events\RegisterTwigfieldAutocompletesEvent; |
21
|
|
|
use nystudio107\twigfield\Twigfield; |
22
|
|
|
use yii\base\Event; |
23
|
|
|
use yii\caching\TagDependency; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class Autocomplete |
27
|
|
|
* |
28
|
|
|
* @author nystudio107 |
|
|
|
|
29
|
|
|
* @package Twigfield |
|
|
|
|
30
|
|
|
* @since 1.0.0 |
|
|
|
|
31
|
|
|
*/ |
|
|
|
|
32
|
|
|
class AutocompleteService extends Component |
33
|
|
|
{ |
34
|
|
|
// Constants |
35
|
|
|
// ========================================================================= |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @event RegisterTwigfieldAutocompletesEvent The event that is triggered when registering |
39
|
|
|
* Twigfield Autocomplete types |
40
|
|
|
* |
41
|
|
|
* Autocomplete Generator types must implement [[AutocompleteInterface]]. [[AutoComplete]] |
42
|
|
|
* provides a base implementation. |
43
|
|
|
* |
44
|
|
|
* ```php |
45
|
|
|
* use nystudio107\twigfield\services\AutocompleteService; |
46
|
|
|
* use nystudio107\twigfield\events\RegisterTwigfieldAutocompletesEvent; |
47
|
|
|
* use yii\base\Event; |
48
|
|
|
* |
49
|
|
|
* Event::on(AutocompleteService::class, |
50
|
|
|
* AutocompleteService::EVENT_REGISTER_TWIGFIELD_AUTOCOMPLETES, |
51
|
|
|
* function(RegisterTwigfieldAutocompletesEvent $event) { |
52
|
|
|
* $event->types[] = MyAutocomplete::class; |
53
|
|
|
* } |
54
|
|
|
* ); |
55
|
|
|
* |
56
|
|
|
* or to pass in a config array for the Autocomplete object: |
57
|
|
|
* |
58
|
|
|
* Event::on(AutocompleteService::class, |
59
|
|
|
* AutocompleteService::EVENT_REGISTER_TWIGFIELD_AUTOCOMPLETES, |
60
|
|
|
* function(RegisterTwigfieldAutocompletesEvent $event) { |
61
|
|
|
* $config = [ |
62
|
|
|
* 'property' => value, |
63
|
|
|
* ]; |
64
|
|
|
* $event->types[] = [MyAutocomplete::class => $config]; |
65
|
|
|
* } |
66
|
|
|
* ); |
67
|
|
|
* |
68
|
|
|
* ``` |
69
|
|
|
*/ |
70
|
|
|
const EVENT_REGISTER_TWIGFIELD_AUTOCOMPLETES = 'registerTwigfieldAutocompletes'; |
71
|
|
|
|
72
|
|
|
const AUTOCOMPLETE_CACHE_TAG = 'TwigFieldAutocompleteTag'; |
73
|
|
|
|
74
|
|
|
// Public Properties |
75
|
|
|
// ========================================================================= |
76
|
|
|
|
77
|
|
|
/** |
|
|
|
|
78
|
|
|
* @var string Prefix for the cache key |
79
|
|
|
*/ |
80
|
|
|
public $cacheKeyPrefix = 'TwigFieldAutocomplete'; |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* @var int Cache duration |
84
|
|
|
*/ |
85
|
|
|
public $cacheDuration = 3600; |
86
|
|
|
|
87
|
|
|
// Public Methods |
88
|
|
|
// ========================================================================= |
89
|
|
|
|
90
|
|
|
/** |
|
|
|
|
91
|
|
|
* @inerhitDoc |
92
|
|
|
*/ |
|
|
|
|
93
|
|
|
public function init(): void |
94
|
|
|
{ |
95
|
|
|
parent::init(); |
96
|
|
|
// Short cacheDuration if we're in devMode |
97
|
|
|
if (Craft::$app->getConfig()->getGeneral()->devMode) { |
98
|
|
|
$this->cacheDuration = 1; |
99
|
|
|
} |
100
|
|
|
// Invalidate any SectionShorthandFieldsAutocomplete caches whenever any field layout is edited |
101
|
|
|
Event::on(Fields::class, Fields::EVENT_AFTER_SAVE_FIELD_LAYOUT, function (SectionEvent $e) { |
|
|
|
|
102
|
|
|
$this->clearAutocompleteCache(SectionShorthandFieldsAutocomplete::class); |
103
|
|
|
}); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Call each of the autocompletes to generate their complete items |
108
|
|
|
* @param string $fieldType |
|
|
|
|
109
|
|
|
* @param array $twigfieldOptions |
|
|
|
|
110
|
|
|
* @return array |
|
|
|
|
111
|
|
|
*/ |
112
|
|
|
public function generateAutocompletes(string $fieldType = Twigfield::DEFAULT_FIELD_TYPE, array $twigfieldOptions = []): array |
113
|
|
|
{ |
114
|
|
|
$autocompleteItems = []; |
115
|
|
|
$autocompletes = $this->getAllAutocompleteGenerators($fieldType); |
116
|
|
|
foreach ($autocompletes as $autocompleteGenerator) { |
117
|
|
|
/* @var BaseAutoComplete $autocomplete */ |
118
|
|
|
// Assume the generator is a class name string |
119
|
|
|
$config = [ |
120
|
|
|
'fieldType' => $fieldType, |
121
|
|
|
'twigfieldOptions' => $twigfieldOptions, |
122
|
|
|
]; |
123
|
|
|
$autocompleteClass = $autocompleteGenerator; |
124
|
|
|
// If we're passed in an array instead, extract the class name and config from the key/value pair |
125
|
|
|
// in the form of [className => configArray] |
126
|
|
|
if (is_array($autocompleteGenerator)) { |
127
|
|
|
$autocompleteClass = array_key_first($autocompleteGenerator); |
128
|
|
|
/** @noinspection SlowArrayOperationsInLoopInspection */ |
|
|
|
|
129
|
|
|
$config = array_merge($config, $autocompleteGenerator[$autocompleteClass]); |
130
|
|
|
} |
131
|
|
|
$autocomplete = new $autocompleteClass($config); |
132
|
|
|
$name = $autocomplete->name; |
133
|
|
|
// Set up the cache parameters |
134
|
|
|
$cache = Craft::$app->getCache(); |
135
|
|
|
$cacheKey = $this->getAutocompleteCacheKey($autocomplete, $config); |
136
|
|
|
$dependency = new TagDependency([ |
|
|
|
|
137
|
|
|
'tags' => [ |
138
|
|
|
self::AUTOCOMPLETE_CACHE_TAG, |
139
|
|
|
self::AUTOCOMPLETE_CACHE_TAG . $name, |
140
|
|
|
self::AUTOCOMPLETE_CACHE_TAG . $autocomplete::class, |
141
|
|
|
], |
142
|
|
|
]); |
|
|
|
|
143
|
|
|
// Get the autocompletes from the cache, or generate them if they aren't cached |
144
|
|
|
$autocompleteItems[$name] = $cache->getOrSet($cacheKey, static function () use ($name, $autocomplete) { |
|
|
|
|
145
|
|
|
$autocomplete->generateCompleteItems(); |
146
|
|
|
return [ |
147
|
|
|
'name' => $name, |
148
|
|
|
'type' => $autocomplete->type, |
149
|
|
|
'hasSubProperties' => $autocomplete->hasSubProperties, |
150
|
|
|
BaseAutoComplete::COMPLETION_KEY => $autocomplete->getCompleteItems(), |
151
|
|
|
]; |
152
|
|
|
}, $this->cacheDuration, $dependency); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
Craft::info('Twigfield Autocompletes generated', __METHOD__); |
155
|
|
|
|
156
|
|
|
return $autocompleteItems; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Clear the specified autocomplete cache (or all autocomplete caches if left empty) |
161
|
|
|
* |
162
|
|
|
* @param string $autocompleteName |
|
|
|
|
163
|
|
|
* @return void |
|
|
|
|
164
|
|
|
*/ |
165
|
|
|
public function clearAutocompleteCache(string $autocompleteName = ''): void |
166
|
|
|
{ |
167
|
|
|
$cache = Craft::$app->getCache(); |
168
|
|
|
TagDependency::invalidate($cache, self::AUTOCOMPLETE_CACHE_TAG . $autocompleteName); |
169
|
|
|
Craft::info('Twigfield caches invalidated', __METHOD__); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Return the cache key to use for an Autocomplete's complete items |
174
|
|
|
* |
175
|
|
|
* @param AutocompleteInterface $autocomplete |
|
|
|
|
176
|
|
|
* @param array $config |
|
|
|
|
177
|
|
|
* @return string |
|
|
|
|
178
|
|
|
*/ |
179
|
|
|
public function getAutocompleteCacheKey(AutocompleteInterface $autocomplete, array $config): string |
180
|
|
|
{ |
181
|
|
|
return $this->cacheKeyPrefix . $autocomplete->name . md5(serialize($config)); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Protected Methods |
185
|
|
|
// ========================================================================= |
186
|
|
|
|
187
|
|
|
/** |
|
|
|
|
188
|
|
|
* Returns all available autocompletes classes. |
189
|
|
|
* |
190
|
|
|
* @return string[] The available autocompletes classes |
191
|
|
|
*/ |
192
|
|
|
public function getAllAutocompleteGenerators(string $fieldType = Twigfield::DEFAULT_FIELD_TYPE): array |
193
|
|
|
{ |
194
|
|
|
$event = new RegisterTwigfieldAutocompletesEvent([ |
|
|
|
|
195
|
|
|
'types' => Twigfield::$settings->defaultTwigfieldAutocompletes, |
196
|
|
|
'fieldType' => $fieldType, |
197
|
|
|
]); |
|
|
|
|
198
|
|
|
$this->trigger(self::EVENT_REGISTER_TWIGFIELD_AUTOCOMPLETES, $event); |
199
|
|
|
|
200
|
|
|
return $event->types; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|