Merge pull request #14097 from eileenmcnaughton/mailin_test
[civicrm-core.git] / CRM / Extension / Container / Collection.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2019
31 */
32
33 /**
34 * An extension container is a locally-accessible source tree which can be
35 * scanned for extensions.
36 */
37 class CRM_Extension_Container_Collection implements CRM_Extension_Container_Interface {
38
39 /**
40 * Containers.
41 *
42 * Format is [$name => CRM_Extension_Container_Interface]
43 *
44 * @var [string => CRM_Extension_Container_Interface]
45 *
46 * Note: Treat as private. This is only public to facilitate debugging.
47 */
48 public $containers;
49
50 /**
51 * @var CRM_Utils_Cache_Interface|null
52 *
53 * Note: Treat as private. This is only public to facilitate debugging.
54 */
55 public $cache;
56
57 /**
58 * @var string the cache key used for any data stored by this container
59 *
60 * Note: Treat as private. This is only public to facilitate debugging.
61 */
62 public $cacheKey;
63
64 /**
65 * K2C ....
66 *
67 * Format is ($key => $containerName).
68 *
69 * @var array
70 *
71 * Note: Treat as private. This is only public to facilitate debugging.
72 */
73 public $k2c;
74
75 /**
76 * Class constructor.
77 *
78 * @param array $containers
79 * Array($name => CRM_Extension_Container_Interface) in order from highest
80 * priority (winners) to lowest priority (losers).
81 * @param CRM_Utils_Cache_Interface $cache
82 * Cache in which to store extension metadata.
83 * @param string $cacheKey
84 * Unique name for this container.
85 */
86 public function __construct($containers, CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
87 $this->containers = $containers;
88 $this->cache = $cache;
89 $this->cacheKey = $cacheKey;
90 }
91
92 /**
93 * @inheritDoc
94 *
95 * @return array
96 */
97 public function checkRequirements() {
98 $errors = [];
99 foreach ($this->containers as $container) {
100 $errors = array_merge($errors, $container->checkRequirements());
101 }
102 return $errors;
103 }
104
105 /**
106 * @inheritDoc
107 *
108 * @return array
109 */
110 public function getKeys() {
111 $k2c = $this->getKeysToContainer();
112 return array_keys($k2c);
113 }
114
115 /**
116 * @inheritDoc
117 *
118 * @param string $key
119 *
120 * @throws \CRM_Extension_Exception_MissingException
121 */
122 public function getPath($key) {
123 return $this->getContainer($key)->getPath($key);
124 }
125
126 /**
127 * @inheritDoc
128 *
129 * @param string $key
130 *
131 * @throws \CRM_Extension_Exception_MissingException
132 */
133 public function getResUrl($key) {
134 return $this->getContainer($key)->getResUrl($key);
135 }
136
137 /**
138 * @inheritDoc
139 */
140 public function refresh() {
141 if ($this->cache) {
142 $this->cache->delete($this->cacheKey);
143 }
144 foreach ($this->containers as $container) {
145 $container->refresh();
146 }
147 }
148
149 /**
150 * Get the container which defines a particular key.
151 *
152 * @param string $key
153 * Extension name.
154 *
155 * @throws CRM_Extension_Exception_MissingException
156 * @return CRM_Extension_Container_Interface
157 */
158 public function getContainer($key) {
159 $k2c = $this->getKeysToContainer();
160 if (isset($k2c[$key]) && isset($this->containers[$k2c[$key]])) {
161 return $this->containers[$k2c[$key]];
162 }
163 else {
164 throw new CRM_Extension_Exception_MissingException("Unknown extension: $key");
165 }
166 }
167
168 /**
169 * Get a list of all keys in these containers -- and the
170 * name of the container which defines each key.
171 *
172 * @return array
173 * ($key => $containerName)
174 */
175 public function getKeysToContainer() {
176 if ($this->cache) {
177 $k2c = $this->cache->get($this->cacheKey);
178 }
179 if (!isset($k2c) || !is_array($k2c)) {
180 $k2c = [];
181 $containerNames = array_reverse(array_keys($this->containers));
182 foreach ($containerNames as $name) {
183 $keys = $this->containers[$name]->getKeys();
184 foreach ($keys as $key) {
185 $k2c[$key] = $name;
186 }
187 }
188 if ($this->cache) {
189 $this->cache->set($this->cacheKey, $k2c);
190 }
191 }
192 return $k2c;
193 }
194
195 }