1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Contains \Drupal\Component\Plugin\Discovery\DiscoveryTrait. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Drupal\Component\Plugin\Discovery; |
9
|
|
|
|
10
|
|
|
use Drupal\Component\Plugin\Exception\PluginNotFoundException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @see Drupal\Component\Plugin\Discovery\DiscoveryInterface |
14
|
|
|
*/ |
15
|
|
|
trait DiscoveryTrait { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
|
|
abstract public function getDefinitions(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function getDefinition($plugin_id, $exception_on_invalid = TRUE) { |
26
|
|
|
$definitions = $this->getDefinitions(); |
27
|
|
|
return $this->doGetDefinition($definitions, $plugin_id, $exception_on_invalid); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets a specific plugin definition. |
32
|
|
|
* |
33
|
|
|
* @param array $definitions |
34
|
|
|
* An array of the available plugin definitions. |
35
|
|
|
* @param string $plugin_id |
36
|
|
|
* A plugin id. |
37
|
|
|
* @param bool $exception_on_invalid |
38
|
|
|
* (optional) If TRUE, an invalid plugin ID will throw an exception. |
39
|
|
|
* Defaults to FALSE. |
40
|
|
|
* |
41
|
|
|
* @return array|null |
42
|
|
|
* A plugin definition, or NULL if the plugin ID is invalid and |
43
|
|
|
* $exception_on_invalid is TRUE. |
44
|
|
|
* |
45
|
|
|
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
46
|
|
|
* Thrown if $plugin_id is invalid and $exception_on_invalid is TRUE. |
47
|
|
|
*/ |
48
|
|
|
protected function doGetDefinition(array $definitions, $plugin_id, $exception_on_invalid) { |
49
|
|
|
// Avoid using a ternary that would create a copy of the array. |
50
|
|
|
if (isset($definitions[$plugin_id])) { |
51
|
|
|
return $definitions[$plugin_id]; |
52
|
|
|
} |
53
|
|
|
elseif (!$exception_on_invalid) { |
54
|
|
|
return NULL; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw new PluginNotFoundException($plugin_id, sprintf('The "%s" plugin does not exist.', $plugin_id)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function hasDefinition($plugin_id) { |
64
|
|
|
return (bool) $this->getDefinition($plugin_id, FALSE); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|