-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathReportPluginData.php
More file actions
200 lines (169 loc) · 5.74 KB
/
ReportPluginData.php
File metadata and controls
200 lines (169 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* @file
* Contains DrupalCodeBuilder\Task\ReportPluginData.
*/
namespace DrupalCodeBuilder\Task;
use DrupalCodeBuilder\Definition\OptionDefinition;
use MutableTypedData\Definition\OptionSetDefininitionInterface;
use DrupalCodeBuilder\Definition\VariantMappingProviderInterface;
use DrupalCodeBuilder\Task\Report\SectionReportInterface;
/**
* Task handler for reporting on plugin data.
*
* TODO: revisit some of these and clean up names / clean up how many we have.
* Consider merging into a ReportComponentData Task.
*/
class ReportPluginData extends ReportHookDataFolder
implements OptionSetDefininitionInterface, VariantMappingProviderInterface, SectionReportInterface {
use OptionsProviderTrait;
use SectionReportSimpleCountTrait;
/**
* The sanity level this task requires to operate.
*/
protected $sanity_level = 'component_data_processed';
/**
* Cached plugin type data.
*
* @var array
*/
protected $cache;
/**
* {@inheritdoc}
*/
public function getInfo(): array {
return [
'key' => 'plugins',
'label' => 'Plugin types',
'weight' => 5,
];
}
/**
* {@inheritdoc}
*/
public function getDataSummary(): array {
return $this->listPluginNamesOptions();
}
/**
* {@inheritdoc}
*/
public function getVariantMapping(): array {
return $this->getPluginTypesMapping();
}
/**
* Get the list of plugin data.
*
* @param $discovery_type
* (optional) The short name of a discovery class to limit the plugin list
* by. Possible values include:
* - AnnotatedClassDiscovery
* - AttributeDiscoveryWithAnnotations
* - YamlDiscovery
*
* @return
* The processed plugin data.
*
* @see \DrupalCodeBuilder\Task\Collect::gatherPluginTypeInfo()
*
* @todo Split this method into two.
*/
function listPluginData($discovery_type = NULL) {
if ($discovery_type) {
// We may come here several times, so cache a filtered result.
// TODO: look into finer-grained caching higher up.
if (isset($this->cache[$discovery_type])) {
return $this->cache[$discovery_type];
}
}
$plugin_data = $this->environment->getStorage()->retrieve('plugins');
// Filter the plugins if there's a requested discovery type.
if ($discovery_type) {
$plugin_data = array_filter($plugin_data, function($item) use ($discovery_type) {
$discovery_pieces = explode('\\', $item['discovery']);
$discovery_short_name = array_pop($discovery_pieces);
return ($discovery_short_name == $discovery_type);
});
$this->cache[$discovery_type] = $plugin_data;
}
return $plugin_data;
}
/**
* Returns a list of annotated plugin types, keyed by subdirectory.
*
* @return
* A list of all plugin types that use annotation discovery, keyed by the
* subdirectory the plugin files go in, for example, 'Block', 'QueueWorker'.
*/
public function listPluginDataBySubdirectory() {
$plugin_types_data = $this->listPluginData('AnnotatedClassDiscovery');
$plugin_types_data_by_subdirectory = [];
foreach ($plugin_types_data as $plugin_id => $plugin_definition) {
if (!empty($plugin_definition['subdir'])) {
$subdir = substr($plugin_definition['subdir'], strlen('Plugin/'));
$plugin_types_data_by_subdirectory[$subdir] = $plugin_definition;
}
}
return $plugin_types_data_by_subdirectory;
}
/**
* {@inheritdoc}
*/
public function getOptions(): array {
$data = $this->listPluginData();
$options = [];
foreach ($data as $plugin_type_name => $plugin_type_info) {
$url = NULL;
if (isset($plugin_type_info['plugin_interface_filepath'])) {
if (str_starts_with($plugin_type_info['plugin_interface_filepath'], 'core')) {
$url = $this->createClassLikeApiUrl($plugin_type_info['plugin_interface_filepath'], 'interface');
}
}
$options[$plugin_type_name] = OptionDefinition::create(
$plugin_type_name,
$plugin_type_info['type_label'],
api_url: $url ?? NULL,
);
}
return $options;
}
/**
* Get plugin types as a list of options.
*
* @param $discovery_type
* (optional) The short name of a discovery class to limit the plugin list
*
* @return
* An array of plugin types as options suitable for FormAPI.
*/
function listPluginNamesOptions($discovery_type = NULL) {
$data = $this->listPluginData($discovery_type);
$return = [];
foreach ($data as $plugin_type_name => $plugin_type_info) {
$return[$plugin_type_name] = $plugin_type_info['type_label'];
}
return $return;
}
public function getPluginTypesMapping() :array {
$mapping = [];
$data = $this->listPluginData();
// TODO: move all this to analysis!
$types = [
'Drupal\\Core\\Plugin\\Discovery\\AnnotatedClassDiscovery' => 'annotation',
'Drupal\\Core\\Plugin\\Discovery\\AttributeClassDiscovery' => 'attribute',
'Drupal\\Core\\Plugin\\Discovery\\AttributeDiscoveryWithAnnotations' => 'attribute',
'Drupal\\Core\\Plugin\\Discovery\\YamlDiscovery' => 'yaml',
'Drupal\Core\Config\Schema\ConfigSchemaDiscovery' => 'yaml', // ????
];
foreach ($data as $plugin_type_name => $plugin_type_info) {
// Quick hack: default to 'yaml' variant, as so far there's only
// migrations that we don't handle, which are YAML.
// TODO: when this is done in analysis, types we don't support should be
// filtered out.
$mapping[$plugin_type_name] = $types[$plugin_type_info['discovery']] ?? 'yaml';
}
// Special cases for plugins which have their own generator.
$mapping['validation.constraint'] = 'validation.constraint';
$mapping['element_info'] = 'element_info';
return $mapping;
}
}