-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPluginType.php
More file actions
280 lines (254 loc) · 10.3 KB
/
PluginType.php
File metadata and controls
280 lines (254 loc) · 10.3 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
namespace DrupalCodeBuilder\Generator;
use CaseConverter\CaseString;
/**
* Generator for a plugin type.
*/
class PluginType extends BaseGenerator {
use NameFormattingTrait;
/**
* {@inheritdoc}
*/
public static function componentDataDefinition() {
return parent::componentDataDefinition() + [
'discovery_type' => [
'label' => 'Plugin discovery type',
'description' => "The way in which plugins of this type are formed.",
'options' => [
'annotation' => 'Annotation: Plugins are classes with an annotation',
'yaml' => 'YAML: Plugins are declared in a single YAML file, usually sharing the same class',
],
'default' => 'annotation',
'required' => TRUE,
],
'plugin_type' => array(
'label' => 'Plugin type ID',
'description' => "The identifier of the plugin type. This is used to form the name of the manager service by prepending 'plugin.manager.'.",
'required' => TRUE,
),
'plugin_label' => [
'label' => 'Plugin label',
'description' => "The human-readable label for plugins of this type. This is used in documentation text.",
'process_default' => TRUE,
'default' => function($component_data) {
$plugin_type = $component_data['plugin_type'];
// Convert the plugin type to camel case. E.g., 'my_plugin' becomes
// 'My Plugin'.
return CaseString::snake($plugin_type)->sentence();
},
],
'plugin_subdirectory' => array(
'label' => 'Plugin subdirectory within the Plugins directory',
'required' => TRUE,
'default' => function($component_data) {
$plugin_type = $component_data['plugin_type'];
// Convert the plugin type to camel case. E.g., 'my_plugin' becomes
// 'MyPlugin'.
return CaseString::snake($plugin_type)->pascal();
},
'process_default' => TRUE,
),
'plugin_relative_namespace' => [
'label' => 'Plugin relative namespace',
'computed' => TRUE,
'default' => function($component_data) {
// The plugin subdirectory may be nested.
return str_replace('/', '\\', $component_data['plugin_subdirectory']);
},
],
'annotation_class' => array(
'label' => 'Annotation class name. (Only used for annotation discovery plugins.)',
'required' => TRUE,
'default' => function($component_data) {
$plugin_type = $component_data['plugin_type'];
// Convert the plugin type to camel case. E.g., 'my_plugin' becomes
// 'MyPlugin'.
return CaseString::snake($plugin_type)->pascal();
},
'process_default' => TRUE,
),
'interface' => array(
'label' => 'Interface',
'computed' => TRUE,
'default' => function($component_data) {
return '\\' . self::makeQualifiedClassName([
'Drupal',
'%module',
'Plugin',
$component_data['plugin_relative_namespace'],
$component_data['annotation_class'] . 'Interface',
]);
},
),
// TODO: Argh, do something about this mess of relative / qualified
// class names!
'base_class_short_name' => [
'computed' => TRUE,
'default' => function($component_data) {
$short_class_name = $component_data['annotation_class'];
// Append 'Base' to the base class name for annotation plugins, where
// the base class is actually a base class, but not for YAML plugins,
// where the base class really is the class that's mostly used for
// all plugins.
if ($component_data['discovery_type'] == 'annotation') {
$short_class_name .= 'Base';
}
return $short_class_name;
},
],
'base_class' => [
'label' => 'Base class',
'computed' => TRUE,
'default' => function($component_data) {
return '\\' . self::makeQualifiedClassName([
'Drupal',
'%module',
'Plugin',
$component_data['plugin_relative_namespace'],
$component_data['base_class_short_name'],
]);
},
],
'plugin_manager_service_id' => [
'computed' => TRUE,
'default' => function($component_data) {
// Namespace the service name after the prefix.
return 'plugin.manager.'
. $component_data['root_component_name']
. '_'
. $component_data['plugin_type'];
},
],
'info_alter_hook' => [
'label' => 'Alter hook name',
'description' => "The name of the hook used to alter plugin info, without the 'hook_' prefix.",
'required' => TRUE,
'default' => function($component_data) {
// Skip this for non-interactive UIs.
if (empty($component_data['plugin_type'])) {
return;
}
$plugin_type = $component_data['plugin_type'];
return "{$component_data['plugin_type']}_info";
},
'process_default' => TRUE,
],
];
}
/**
* {@inheritdoc}
*/
public function requiredComponents() {
$components = parent::requiredComponents();
$plugin_type = $this->component_data['plugin_type'];
$components['manager'] = array(
'component_type' => 'PluginTypeManager',
'prefixed_service_name' => $this->component_data['plugin_manager_service_id'],
// Use the annotation class name as the basis for the manager class name.
'service_class_name' => $this->component_data['annotation_class'] . 'Manager',
'injected_services' => [],
'docblock_first_line' => "Manages discovery and instantiation of {$this->component_data['plugin_label']} plugins.",
);
if ($this->component_data['discovery_type'] == 'annotation') {
// Annotation plugin managers inherit from DefaultPluginManager.
$components['manager']['parent'] = 'default_plugin_manager';
// TODO: a service should be able to detect the parent class name from
// service definitions.... if we had all of them.
$components['manager']['parent_class_name'] = '\Drupal\Core\Plugin\DefaultPluginManager';
}
else {
// YAML plugin managers need some services injecting.
$components['manager']['injected_services'] = [
'cache.discovery',
'module_handler',
];
// Don't inherit from the default plugin manager as a service, but do
// inherit from it as a class. (See menu YAML plugins for example.)
$components['manager']['parent_class_name'] = '\Drupal\Core\Plugin\DefaultPluginManager';
}
// TODO: remove the specialized PluginTypeManager generator, and instead
// set a constructor method generator to be contained by a Service.
/*
$components['__construct'] = array(
'component_type' => 'PHPFunction',
'containing_component' => "%requester:manager",
'doxygen_first' => "Constructs a new {$this->component_data['annotation_class']}Manager object.",
'declaration' => 'public function __construct()',
'body' => array(
"return [];",
),
);
*/
if ($this->component_data['discovery_type'] == 'annotation') {
$components['annotation'] = [
'component_type' => 'AnnotationClass',
'relative_class_name' => ['Annotation', $this->component_data['annotation_class']],
'parent_class_name' => '\Drupal\Component\Annotation\Plugin',
'class_docblock_lines' => [
"Defines the {$this->component_data['plugin_label']} plugin annotation object.",
"Plugin namespace: {$this->component_data['plugin_relative_namespace']}.",
],
// TODO: Some annotation properties such as ID and label.
];
}
$plugin_relative_namespace_pieces = explode('\\', $this->component_data['plugin_relative_namespace']);
$components['interface'] = [
'component_type' => 'PHPInterfaceFile',
'relative_class_name' => array_merge(
['Plugin'],
$plugin_relative_namespace_pieces,
[$this->component_data['annotation_class'] . 'Interface']
),
'docblock_first_line' => "Interface for {$this->component_data['plugin_label']} plugins.",
// TODO: parent interfaces.
];
$components['base_class'] = [
'component_type' => 'PHPClassFile',
'relative_class_name' => array_merge(
['Plugin'],
$plugin_relative_namespace_pieces,
[$this->component_data['base_class_short_name']]
),
'parent_class_name' => '\Drupal\Component\Plugin\PluginBase',
'interfaces' => [
$this->component_data['interface'],
],
// Abstract for annotation plugins, where each plugin provides a class;
// for YAML plugins, each plugin will typically just use this class.
'abstract'=> ($this->component_data['discovery_type'] == 'annotation'),
'docblock_first_line' => "Base class for {$this->component_data['plugin_label']} plugins.",
];
$module = $this->component_data['root_component_name'];
$components['plugin_type_yml'] = [
'component_type' => 'YMLFile',
'filename' => '%module.plugin_type.yml',
'yaml_data' => [
"{$module}.{$plugin_type}"=> [
'label' => $this->component_data['plugin_label'],
'plugin_manager_service_id' => $this->component_data['plugin_manager_service_id'],
'plugin_definition_decorator_class' => 'Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator',
],
],
];
// Request this even though the Module generator may have done, so we ensure
// it is there.
$components['api'] = [
'component_type' => 'API',
];
$components['alter_hook'] = [
'component_type' => 'PHPFunction',
'containing_component' => '%requester:api',
'declaration' => "function hook_{$this->component_data['info_alter_hook']}_alter(array &£info)",
'function_docblock_lines' => [
"Perform alterations on {$this->component_data['plugin_label']} definitions.",
'@param array $info',
" Array of information on {$this->component_data['plugin_label']} plugins.",
],
'body' => [
"// Change the class of the 'foo' plugin.",
"£info['foo']['class'] = SomeOtherClass::class;",
],
];
return $components;
}
}