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