Resolve docblock inaccuracy in CRM_Contribute_Form_Task_TaskTrait
[civicrm-core.git] / CRM / Extension / Container / Collection.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * An extension container is a locally-accessible source tree which can be
19 * scanned for extensions.
20 */
21 class CRM_Extension_Container_Collection implements CRM_Extension_Container_Interface {
22
23 /**
24 * Containers.
25 *
26 * Format is [$name => CRM_Extension_Container_Interface]
27 *
28 * @var array
29 *
30 * Note: Treat as private. This is only public to facilitate debugging.
31 */
32 public $containers;
33
34 /**
35 * @var CRM_Utils_Cache_Interface|null
36 *
37 * Note: Treat as private. This is only public to facilitate debugging.
38 */
39 public $cache;
40
41 /**
42 * The cache key used for any data stored by this container.
43 *
44 * @var string
45 *
46 * Note: Treat as private. This is only public to facilitate debugging.
47 */
48 public $cacheKey;
49
50 /**
51 * K2C ....
52 *
53 * Format is ($key => $containerName).
54 *
55 * @var array
56 *
57 * Note: Treat as private. This is only public to facilitate debugging.
58 */
59 public $k2c;
60
61 /**
62 * Class constructor.
63 *
64 * @param array $containers
65 * Array($name => CRM_Extension_Container_Interface) in order from highest
66 * priority (winners) to lowest priority (losers).
67 * @param CRM_Utils_Cache_Interface $cache
68 * Cache in which to store extension metadata.
69 * @param string $cacheKey
70 * Unique name for this container.
71 */
72 public function __construct($containers, CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
73 $this->containers = $containers;
74 $this->cache = $cache;
75 $this->cacheKey = $cacheKey;
76 }
77
78 /**
79 * @inheritDoc
80 *
81 * @return array
82 */
83 public function checkRequirements() {
84 $errors = [];
85 foreach ($this->containers as $container) {
86 $errors = array_merge($errors, $container->checkRequirements());
87 }
88 return $errors;
89 }
90
91 /**
92 * @inheritDoc
93 *
94 * @return array
95 */
96 public function getKeys() {
97 $k2c = $this->getKeysToContainer();
98 return array_keys($k2c);
99 }
100
101 /**
102 * @inheritDoc
103 *
104 * @param string $key
105 *
106 * @throws \CRM_Extension_Exception_MissingException
107 */
108 public function getPath($key) {
109 return $this->getContainer($key)->getPath($key);
110 }
111
112 /**
113 * @inheritDoc
114 *
115 * @param string $key
116 *
117 * @throws \CRM_Extension_Exception_MissingException
118 */
119 public function getResUrl($key) {
120 return $this->getContainer($key)->getResUrl($key);
121 }
122
123 /**
124 * @inheritDoc
125 */
126 public function refresh() {
127 if ($this->cache) {
128 $this->cache->delete($this->cacheKey);
129 }
130 foreach ($this->containers as $container) {
131 $container->refresh();
132 }
133 }
134
135 /**
136 * Get the container which defines a particular key.
137 *
138 * @param string $key
139 * Extension name.
140 *
141 * @throws CRM_Extension_Exception_MissingException
142 * @return CRM_Extension_Container_Interface
143 */
144 public function getContainer($key) {
145 $k2c = $this->getKeysToContainer();
146 if (isset($k2c[$key]) && isset($this->containers[$k2c[$key]])) {
147 return $this->containers[$k2c[$key]];
148 }
149 else {
150 throw new CRM_Extension_Exception_MissingException("Unknown extension: $key");
151 }
152 }
153
154 /**
155 * Get a list of all keys in these containers -- and the
156 * name of the container which defines each key.
157 *
158 * @return array
159 * ($key => $containerName)
160 */
161 public function getKeysToContainer() {
162 if ($this->cache) {
163 $k2c = $this->cache->get($this->cacheKey);
164 }
165 if (!isset($k2c) || !is_array($k2c)) {
166 $k2c = [];
167 $containerNames = array_reverse(array_keys($this->containers));
168 foreach ($containerNames as $name) {
169 $keys = $this->containers[$name]->getKeys();
170 foreach ($keys as $key) {
171 $k2c[$key] = $name;
172 }
173 }
174 if ($this->cache) {
175 $this->cache->set($this->cacheKey, $k2c);
176 }
177 }
178 return $k2c;
179 }
180
181 }