Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Entity / DependencyTrait.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 /**
6  * Provides a trait for managing an object's dependencies.
7  */
8 trait DependencyTrait {
9
10   /**
11    * The object's dependencies.
12    *
13    * @var array
14    */
15   protected $dependencies = [];
16
17   /**
18    * Adds a dependency.
19    *
20    * @param string $type
21    *   Type of dependency being added: 'module', 'theme', 'config', 'content'.
22    * @param string $name
23    *   If $type is 'module' or 'theme', the name of the module or theme. If
24    *   $type is 'config' or 'content', the result of
25    *   EntityInterface::getConfigDependencyName().
26    *
27    * @see \Drupal\Core\Entity\EntityInterface::getConfigDependencyName()
28    *
29    * @return $this
30    */
31   protected function addDependency($type, $name) {
32     if (empty($this->dependencies[$type])) {
33       $this->dependencies[$type] = [$name];
34       if (count($this->dependencies) > 1) {
35         // Ensure a consistent order of type keys.
36         ksort($this->dependencies);
37       }
38     }
39     elseif (!in_array($name, $this->dependencies[$type])) {
40       $this->dependencies[$type][] = $name;
41       // Ensure a consistent order of dependency names.
42       sort($this->dependencies[$type], SORT_FLAG_CASE);
43     }
44     return $this;
45   }
46
47   /**
48    * Adds multiple dependencies.
49    *
50    * @param array $dependencies
51    *   An array of dependencies keyed by the type of dependency. One example:
52    *   @code
53    *   array(
54    *     'module' => array(
55    *       'node',
56    *       'field',
57    *       'image',
58    *     ),
59    *   );
60    *   @endcode
61    *
62    * @see \Drupal\Core\Entity\DependencyTrait::addDependency
63    */
64   protected function addDependencies(array $dependencies) {
65     foreach ($dependencies as $dependency_type => $list) {
66       foreach ($list as $name) {
67         $this->addDependency($dependency_type, $name);
68       }
69     }
70   }
71
72 }