Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / migrate / src / Plugin / Discovery / ProviderFilterDecorator.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\Discovery;
4
5 use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
6 use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
7
8 /**
9  * Remove plugin definitions with non-existing providers.
10  *
11  * @internal
12  *   This is a temporary solution to the fact that migration source plugins have
13  *   more than one provider. This functionality will be moved to core in
14  *   https://www.drupal.org/node/2786355.
15  */
16 class ProviderFilterDecorator implements DiscoveryInterface {
17
18   use DiscoveryTrait;
19
20   /**
21    * The Discovery object being decorated.
22    *
23    * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
24    */
25   protected $decorated;
26
27   /**
28    * A callable for testing if a provider exists.
29    *
30    * @var callable
31    */
32   protected $providerExists;
33
34   /**
35    * Constructs a InheritProviderDecorator object.
36    *
37    * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated
38    *   The object implementing DiscoveryInterface that is being decorated.
39    * @param callable $provider_exists
40    *   A callable, gets passed a provider name, should return TRUE if the
41    *   provider exists and FALSE if not.
42    */
43   public function __construct(DiscoveryInterface $decorated, callable $provider_exists) {
44     $this->decorated = $decorated;
45     $this->providerExists = $provider_exists;
46   }
47
48   /**
49    * Removes plugin definitions with non-existing providers.
50    *
51    * @param mixed[] $definitions
52    *   An array of plugin definitions (empty array if no definitions were
53    *   found). Keys are plugin IDs.
54    * @param callable $provider_exists
55    *   A callable, gets passed a provider name, should return TRUE if the
56    *   provider exists and FALSE if not.
57    *
58    * @return array|\mixed[]
59    *   An array of plugin definitions. If a definition is an array and has a
60    *   provider key that provider is guaranteed to exist.
61    */
62   public static function filterDefinitions(array $definitions, callable $provider_exists) {
63     // Besides what the caller accepts, we also accept core or component.
64     $provider_exists = function ($provider) use ($provider_exists) {
65       return in_array($provider, ['core', 'component']) || $provider_exists($provider);
66     };
67     return array_filter($definitions, function ($definition) use ($provider_exists) {
68       // Plugin definitions can be objects (for example, Typed Data) those will
69       // become empty array here and cause no problems.
70       $definition = (array) $definition + ['provider' => []];
71       // There can be one or many providers, handle them as multiple always.
72       $providers = (array) $definition['provider'];
73       return count($providers) == count(array_filter($providers, $provider_exists));
74     });
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function getDefinitions() {
81     return static::filterDefinitions($this->decorated->getDefinitions(), $this->providerExists);
82   }
83
84   /**
85    * Passes through all unknown calls onto the decorated object.
86    *
87    * @param string $method
88    *   The method to call on the decorated object.
89    * @param array $args
90    *   Call arguments.
91    *
92    * @return mixed
93    *   The return value from the method on the decorated object.
94    */
95   public function __call($method, array $args) {
96     return call_user_func_array([$this->decorated, $method], $args);
97   }
98
99 }