Merge pull request #21916 from mattwire/recurprocessorid
[civicrm-core.git] / CRM / Extension / Container / Collection.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
15 */
16
17/**
18 * An extension container is a locally-accessible source tree which can be
19 * scanned for extensions.
20 */
21class CRM_Extension_Container_Collection implements CRM_Extension_Container_Interface {
22
23 /**
e97c66ff 24 * Containers.
25 *
26 * Format is [$name => CRM_Extension_Container_Interface]
27 *
041ecc95 28 * @var array
6a488035
TO
29 *
30 * Note: Treat as private. This is only public to facilitate debugging.
31 */
32 public $containers;
33
34 /**
e97c66ff 35 * @var CRM_Utils_Cache_Interface|null
6a488035
TO
36 *
37 * Note: Treat as private. This is only public to facilitate debugging.
38 */
39 public $cache;
40
41 /**
041ecc95 42 * The cache key used for any data stored by this container.
43 *
44 * @var string
6a488035
TO
45 *
46 * Note: Treat as private. This is only public to facilitate debugging.
47 */
48 public $cacheKey;
49
50 /**
e97c66ff 51 * K2C ....
52 *
53 * Format is ($key => $containerName).
54 *
55 * @var array
6a488035
TO
56 *
57 * Note: Treat as private. This is only public to facilitate debugging.
58 */
59 public $k2c;
60
61 /**
e97c66ff 62 * Class constructor.
63 *
78612209
TO
64 * @param array $containers
65 * Array($name => CRM_Extension_Container_Interface) in order from highest
66 * priority (winners) to lowest priority (losers).
6a488035 67 * @param CRM_Utils_Cache_Interface $cache
78612209
TO
68 * Cache in which to store extension metadata.
69 * @param string $cacheKey
70 * Unique name for this container.
6a488035
TO
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 /**
e7c15cb6 79 * @inheritDoc
4faa436b
SB
80 *
81 * @return array
6a488035 82 */
0bb81f24 83 public function checkRequirements() {
be2fb01f 84 $errors = [];
0bb81f24
TO
85 foreach ($this->containers as $container) {
86 $errors = array_merge($errors, $container->checkRequirements());
87 }
88 return $errors;
89 }
58119ee4
TO
90
91 /**
e7c15cb6 92 * @inheritDoc
4faa436b 93 *
e97c66ff 94 * @return array
0bb81f24 95 */
6a488035
TO
96 public function getKeys() {
97 $k2c = $this->getKeysToContainer();
98 return array_keys($k2c);
99 }
100
101 /**
e7c15cb6 102 * @inheritDoc
4faa436b
SB
103 *
104 * @param string $key
e97c66ff 105 *
106 * @throws \CRM_Extension_Exception_MissingException
6a488035
TO
107 */
108 public function getPath($key) {
109 return $this->getContainer($key)->getPath($key);
110 }
111
112 /**
e7c15cb6 113 * @inheritDoc
4faa436b
SB
114 *
115 * @param string $key
e97c66ff 116 *
117 * @throws \CRM_Extension_Exception_MissingException
6a488035
TO
118 */
119 public function getResUrl($key) {
120 return $this->getContainer($key)->getResUrl($key);
121 }
122
123 /**
e7c15cb6 124 * @inheritDoc
6a488035
TO
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 /**
fe482240 136 * Get the container which defines a particular key.
6a488035 137 *
78612209
TO
138 * @param string $key
139 * Extension name.
dd244018
EM
140 *
141 * @throws CRM_Extension_Exception_MissingException
6a488035 142 * @return CRM_Extension_Container_Interface
6a488035
TO
143 */
144 public function getContainer($key) {
145 $k2c = $this->getKeysToContainer();
22c810a7 146 if (isset($k2c[$key]) && isset($this->containers[$k2c[$key]])) {
6a488035 147 return $this->containers[$k2c[$key]];
78612209
TO
148 }
149 else {
6a488035
TO
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 *
a6c01b45
CW
158 * @return array
159 * ($key => $containerName)
6a488035
TO
160 */
161 public function getKeysToContainer() {
162 if ($this->cache) {
163 $k2c = $this->cache->get($this->cacheKey);
164 }
c3d574fc 165 if (!isset($k2c) || !is_array($k2c)) {
be2fb01f 166 $k2c = [];
6a488035
TO
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 }
96025800 180
6a488035 181}