Completed
Push — 7.x-1.x ( 64022a...8b8e48 )
by Fabian
05:21
created

alterContainerDefinition()   D

Complexity

Conditions 16
Paths 480

Size

Total Lines 54
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 54
rs 4.5844
cc 16
eloc 30
nc 480
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\service_container\ServiceContainer\ServiceProvider\ServiceContainerServiceProvider
6
 */
7
8
namespace Drupal\service_container\ServiceContainer\ServiceProvider;
9
10
use Drupal\service_container\DependencyInjection\Container;
11
use Drupal\service_container\DependencyInjection\ServiceProviderInterface;
12
13
/**
14
 * Provides render cache service definitions.
15
 *
16
 * @codeCoverageIgnore
17
 *
18
 * @todo The alter and plugin_manager_parts are unit testable.
19
 */
20
class ServiceContainerServiceProvider implements ServiceProviderInterface {
21
22
  /**
23
   * {@inheritdoc}
24
   */
25
  public function getContainerDefinition() {
26
    $parameters = array();
27
    $parameters['service_container.static_event_listeners'] = array();
28
    $parameters['service_container.plugin_managers'] = array();
29
    $parameters['service_container.plugin_manager_types'] = array(
30
      'ctools' => '\Drupal\service_container\Plugin\Discovery\CToolsPluginDiscovery',
31
    );
32
33
    $services = array();
34
35
    $services['drupal7'] = array(
36
      'class' => 'Drupal\service_container\Legacy\Drupal7',
37
    );
38
39
    $services['service_container'] = array(
40
      'class' => '\Drupal\service_container\DependencyInjection\Container',
41
    );
42
43
    $services['module_handler'] = array(
44
      'class' => '\Drupal\service_container\Extension\ModuleHandler',
45
      'arguments' => array(DRUPAL_ROOT, '@drupal7'),
46
    );
47
48
    $services['module_installer'] = array(
49
      'class' => '\Drupal\service_container\Extension\ModuleInstaller',
50
      'arguments' => array('@drupal7'),
51
    );
52
53
    $services['database'] = array(
54
      'class' => 'Drupal\Core\Database\Connection',
55
      'factory' => 'Drupal\Core\Database\Database::getConnection',
56
      'arguments' => array('default'),
57
    );
58
59
    $services['flood'] = array(
60
      'class' => '\Drupal\service_container\Flood\LegacyBackend',
61
      'arguments' => array('@database', '@drupal7'),
62
      'tags' => array(
63
        array('name' => 'backend_overridable'),
64
      ),
65
    );
66
67
    $services['serialization.phpserialize'] = array(
68
      'class' => 'Drupal\Component\Serialization\PhpSerialize',
69
    );
70
71
    $parameters['factory.keyvalue'] = array();
72
    $parameters['factory.keyvalue.expirable'] = array();
73
    $services['keyvalue'] = array(
74
      'class' => 'Drupal\service_container\KeyValueStore\KeyValueFactory',
75
      'arguments' => array('@service_container', '%factory.keyvalue%')
76
    );
77
    $services['keyvalue.database'] = array(
78
      'class' => 'Drupal\Core\KeyValueStore\KeyValueDatabaseFactory',
79
      'arguments' => array('@serialization.phpserialize', '@database')
80
    );
81
    $services['keyvalue.expirable'] = array(
82
      'class' => '\Drupal\service_container\KeyValueStore\KeyValueExpirableFactory',
83
      'arguments' => array('@service_container', '%factory.keyvalue.expirable%')
84
    );
85
    $services['keyvalue.expirable.database'] = array(
86
      'class' => 'Drupal\service_container\KeyValueStore\KeyValueDatabaseExpirableFactory',
87
      'arguments' => array('@serialization.phpserialize', '@database'),
88
      'tags' => array(
89
        array('name' => 'needs_destruction'),
90
      ),
91
    );
92
93
    $services['state'] = array(
94
      'class' => 'Drupal\Core\State\State',
95
      'arguments' => array('@keyvalue'),
96
    );
97
98
    $services['variable'] = array(
99
      'class' => 'Drupal\service_container\Variable',
100
      'arguments' => array('@drupal7'),
101
    );
102
103
    $services['lock'] = array(
104
      'class' => 'Drupal\Core\Lock\DatabaseLockBackend',
105
      'arguments' => array('@database'),
106
      'tags' => array(
107
        array('name' => 'backend_overridable'),
108
      ),
109
    );
110
111
    $services['lock.persistent'] = array(
112
      'class' => 'Drupal\Core\Lock\PersistentDatabaseLockBackend',
113
      'arguments' => array('@database'),
114
      'tags' => array(
115
        array('name' => 'backend_overridable'),
116
      ),
117
    );
118
119
    $services['messenger'] = array(
120
      'class' => 'Drupal\service_container\Messenger\LegacyMessenger',
121
      'arguments' => array('@drupal7'),
122
    );
123
124
    $services['url_generator'] = array(
125
      'class' => 'Drupal\service_container\UrlGenerator',
126
      'arguments' => array('@drupal7'),
127
    );
128
129
    $services['link_generator'] = array(
130
      'class' => 'Drupal\service_container\LinkGenerator',
131
      'arguments' => array('@drupal7'),
132
    );
133
134
    $services['current_user'] = array(
135
      'class' => 'Drupal\service_container\Session\Account',
136
      'arguments' => array('@drupal7', '@variable'),
137
    );
138
139
    // Logging integration.
140
    $services['logger.factory'] = array(
141
      'class' => 'Drupal\service_container\Logger\LoggerChannelFactory',
142
      'calls' => array(
143
        array('addLogger', array('@logger.dblog')),
144
      ),
145
    );
146
147
    $services['logger.dblog'] = array(
148
      'class' => 'Drupal\service_container\Logger\WatchdogLogger',
149
      'arguments' => array('@drupal7'),
150
      'tags' => array(
151
        array('name' => 'logger'),
152
      ),
153
    );
154
155
    $services['logger.channel.default'] = array(
156
      'class' => 'Drupal\service_container\Logger\LoggerChannel',
157
      'factory' => array('@logger.factory', 'get'),
158
      'arguments' => array('system'),
159
    );
160
161
    $services['logger.channel.php'] = array(
162
      'class' => 'Drupal\service_container\Logger\LoggerChannel',
163
      'factory' => array('@logger.factory', 'get'),
164
      'arguments' => array('php'),
165
    );
166
167
    $services['logger.channel.cron'] = array(
168
      'class' => 'Drupal\service_container\Logger\LoggerChannel',
169
      'factory' => array('@logger.factory', 'get'),
170
      'arguments' => array('cron'),
171
    );
172
173
    return array(
174
      'parameters' => $parameters,
175
      'services' => $services,
176
    );
177
  }
178
179
  /**
180
   * {@inheritdoc}
181
   */
182
  public function alterContainerDefinition(&$container_definition) {
183
    foreach(array('annotated_plugins_auto_discovery', 'ctools_plugins_auto_discovery') as $prefix) {
184
      $container_definition['parameters'][$prefix] = array();
185
      foreach($container_definition['parameters'] as $parameter => $value) {
186
        if (strpos($parameter, $prefix) === 0) {
187
          $container_definition['parameters'][$prefix] = array_merge($container_definition['parameters'][$prefix], $value);
188
        }
189
      }
190
    }
191
192
    if (!empty($container_definition['parameters']['ctools_plugins_auto_discovery']) && $this->moduleExists('ctools')) {
193
      $ctools_types = $this->cToolsGetTypes();
194
      $filtered_types = array_intersect_key($ctools_types, array_flip($container_definition['parameters']['ctools_plugins_auto_discovery']));
195
      $this->registerCToolsPluginTypes($container_definition, $filtered_types);
196
    }
197
198
    if (!empty($container_definition['parameters']['annotated_plugins_auto_discovery']) && $this->moduleExists('service_container_annotation_discovery')) {
199
      $this->registerAnnotatedPluginTypes($container_definition, $container_definition['parameters']['annotated_plugins_auto_discovery']);
200
    }
201
202
    // Set empty value when its not set.
203
    if (empty($container_definition['tags']['plugin_manager'])) {
204
      $container_definition['tags']['plugin_manager'] = array();
205
    }
206
207
    // Process plugin managers of different types.
208
    $plugin_manager_types = $container_definition['parameters']['service_container.plugin_manager_types'];
209
    $all_plugin_managers = $container_definition['parameters']['service_container.plugin_managers'];
210
211
    foreach ($all_plugin_managers as $plugin_manager_type => $plugin_managers) {
212
      if (empty($plugin_manager_types[$plugin_manager_type])) {
213
        continue;
214
      }
215
      $discovery_class = $plugin_manager_types[$plugin_manager_type];
216
      $this->processPluginManagers($container_definition, $discovery_class, $plugin_managers);
217
    }
218
219
    // Register plugin manager plugins as private services in the container.
220
    foreach ($container_definition['tags']['plugin_manager'] as $service => $tags) {
221
      foreach ($tags as $tag) {
222
        $discovery_class = $tag['discovery_class'];
223
        $discovery = new $discovery_class($tag['plugin_manager_definition']);
224
        $definitions = $discovery->getDefinitions();
225
        foreach ($definitions as $key => $definition) {
226
          // CTools uses 'file' and 'path', while the service container uses only 'file'.
227
          // To make this compatible, combine 'file' and 'path' and remove 'path'.
228
          if (isset($definition['file']) && isset($definition['path'])) {
229
            $definition['file'] = $definition['path'] . '/' . $definition['file'];
230
          }
231
          $container_definition['services'][$tag['prefix'] . $key] = $definition + array('public' => FALSE);
232
        }
233
      }
234
    }
235
  }
236
237
  /**
238
   * Automatically register all annotated Plugins.
239
   *
240
   * @param array $container_definition
241
   *   The container definition to process.
242
   * @param array $definition
0 ignored issues
show
Documentation introduced by
There is no parameter named $definition. Did you maybe mean $container_definition?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
243
   *   The parameter definition.
244
   */
245
  public function registerAnnotatedPluginTypes(&$container_definition, $parameter_definitions) {
246
    foreach($parameter_definitions as $definition) {
247
      if (isset($definition['plugin_manager_name']) && !empty($definition['plugin_manager_name'])) {
248
        $plugin_manager_name = $definition['plugin_manager_name'];
249
      } else {
250
        $plugin_manager_name  = $definition['owner'] . '.' . $definition['type'];
251
        $this->registerAliasServices($container_definition, $definition['owner'], $definition['type']);
252
      }
253
254
      $container_definition['services'][$plugin_manager_name] = array();
255
      $container_definition['parameters']['service_container.plugin_managers']['annotated'][$plugin_manager_name] = $definition;
256
    }
257
  }
258
259
  /**
260
   * Automatically register all ctools plugins of the given types.
261
   *
262
   * @param array $container_definition
263
   *   The container definition to process.
264
   * @param array $ctools_types
265
   *   Array of plugin types, indexed by module name.
266
   */
267
  public function registerCToolsPluginTypes(&$container_definition, $ctools_types) {
268
    foreach($ctools_types as $owner => $plugins) {
269
      foreach($plugins as $plugin_type => $plugin_data) {
270
        if (isset($container_definition['parameters']['service_container.plugin_managers']['ctools'][$owner . '.' . $plugin_type])) {
271
          continue;
272
        }
273
        $this->registerAliasServices($container_definition, $owner, $plugin_type);
274
275
        $container_definition['parameters']['service_container.plugin_managers']['ctools'][$owner . '.' . $plugin_type] = array(
276
          'owner' => $owner,
277
          'type' => $plugin_type,
278
        );
279
      }
280
    }
281
  }
282
283
  /**
284
   * Register aliases for the service.
285
   *
286
   * @param array $container_definition
287
   *   The container definition to process.
288
   * @param string $owner
289
   *   The owner, here, the name of the module
290
   * @param string $plugin_type
291
   *   The type of plugin
292
   */
293
  public function registerAliasServices(&$container_definition, $owner, $plugin_type) {
294
    // Register service with original string.
295
    $name = $owner . '.' . $plugin_type;
296
    $container_definition['services'][$name] = array();
297
298
    // Check candidates for needed aliases.
299
    $candidates = array();
300
    $candidates[$owner . '.' . Container::underscore($plugin_type)] = TRUE;
301
    $candidates[$name] = FALSE;
302
303
    foreach ($candidates as $candidate => $value) {
304
      if ($value) {
305
        $container_definition['aliases'][$candidate] = $name;
306
      }
307
    }
308
  }
309
310
  /**
311
   * Processes plugin managers of varying types and registers them in the
312
   * container based on the given discovery class.
313
   *
314
   * @param array $container_definition
315
   *   The container definition to process.
316
   * @param string $discovery_class
317
   *   The discovery class this plugin manager type uses.
318
   * @param array $plugin_managers
319
   *   The plugin managers to register.
320
   */
321
  public function processPluginManagers(&$container_definition, $discovery_class, $plugin_managers) {
322
    foreach ($plugin_managers as $name => $plugin_manager) {
323
      if (!isset($container_definition['services'][$name]) || !empty($container_definition['services'][$name])) {
324
        continue;
325
      }
326
327
      $container_definition['services'][$name] = $this->getPluginManagerDefinition($name, $discovery_class, $plugin_manager);
328
      $tags = $container_definition['services'][$name]['tags'];
329
330 View Code Duplication
      foreach ($tags as $tag) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
331
        $tag_name = $tag['name'];
332
        unset($tag['name']);
333
        $container_definition['tags'][$tag_name][$name][] = $tag;
334
      }
335
    }
336
  }
337
338
  /**
339
   * Gets plugin manager definition to make it simpler to register plugins.
340
   *
341
   * @param string $name
342
   *   The owning module of the plugin.
343
   * @param string $discovery_class
344
   *   The discovery class in use to discover plugins.
345
   * @param array $plugin_manager
346
   *   The plugin manager definition
347
   *
348
   * @return array
349
   */
350
  protected function getPluginManagerDefinition($name, $discovery_class, $plugin_manager) {
351
    $prefix = "$name.internal.";
352
    return array(
353
      'class' => '\Drupal\service_container\Plugin\ContainerAwarePluginManager',
354
      'arguments' => array($prefix),
355
      'calls' => array(
356
        array('setContainer', array('@service_container')),
357
      ),
358
      'tags' => array(
359
        array(
360
          'name' => 'plugin_manager',
361
          'discovery_class' => $discovery_class,
362
          'prefix' => $prefix,
363
          'plugin_manager_definition' => $plugin_manager,
364
        ),
365
      ),
366
    );
367
  }
368
369
  /**
370
   * Return the full list of plugin type info for all plugin types registered in
371
   * the current system.
372
   *
373
   * This function manages its own cache getting/setting, and should always be
374
   * used as the way to initially populate the list of plugin types. Make sure you
375
   * call this function to properly populate the ctools_plugin_type_info static
376
   * variable.
377
   *
378
   * @return array
379
   *   A multilevel array of plugin type info, the outer array keyed on module
380
   *   name and each inner array keyed on plugin type name.
381
   */
382
  public function cToolsGetTypes() {
383
    ctools_include('plugins');
384
    return ctools_plugin_get_plugin_type_info();
385
  }
386
387
  /**
388
   * Determines whether a given module exists.
389
   *
390
   * @param string $name
391
   *   The name of the module (without the .module extension).
392
   *
393
   * @return bool
394
   *   TRUE if the module is both installed and enabled, FALSE otherwise.
395
   */
396
  public function moduleExists($name) {
397
    return module_exists($name);
398
  }
399
}
400