Merge pull request #14103 from jitendrapurohit/core-889
[civicrm-core.git] / CRM / Extension / Container / Collection.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 * @package CRM
6b83d5bd 30 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
31 */
32
33/**
34 * An extension container is a locally-accessible source tree which can be
35 * scanned for extensions.
36 */
37class CRM_Extension_Container_Collection implements CRM_Extension_Container_Interface {
38
39 /**
e97c66ff 40 * Containers.
41 *
42 * Format is [$name => CRM_Extension_Container_Interface]
43 *
041ecc95 44 * @var array
6a488035
TO
45 *
46 * Note: Treat as private. This is only public to facilitate debugging.
47 */
48 public $containers;
49
50 /**
e97c66ff 51 * @var CRM_Utils_Cache_Interface|null
6a488035
TO
52 *
53 * Note: Treat as private. This is only public to facilitate debugging.
54 */
55 public $cache;
56
57 /**
041ecc95 58 * The cache key used for any data stored by this container.
59 *
60 * @var string
6a488035
TO
61 *
62 * Note: Treat as private. This is only public to facilitate debugging.
63 */
64 public $cacheKey;
65
66 /**
e97c66ff 67 * K2C ....
68 *
69 * Format is ($key => $containerName).
70 *
71 * @var array
6a488035
TO
72 *
73 * Note: Treat as private. This is only public to facilitate debugging.
74 */
75 public $k2c;
76
77 /**
e97c66ff 78 * Class constructor.
79 *
78612209
TO
80 * @param array $containers
81 * Array($name => CRM_Extension_Container_Interface) in order from highest
82 * priority (winners) to lowest priority (losers).
6a488035 83 * @param CRM_Utils_Cache_Interface $cache
78612209
TO
84 * Cache in which to store extension metadata.
85 * @param string $cacheKey
86 * Unique name for this container.
6a488035
TO
87 */
88 public function __construct($containers, CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
89 $this->containers = $containers;
90 $this->cache = $cache;
91 $this->cacheKey = $cacheKey;
92 }
93
94 /**
e7c15cb6 95 * @inheritDoc
4faa436b
SB
96 *
97 * @return array
6a488035 98 */
0bb81f24 99 public function checkRequirements() {
be2fb01f 100 $errors = [];
0bb81f24
TO
101 foreach ($this->containers as $container) {
102 $errors = array_merge($errors, $container->checkRequirements());
103 }
104 return $errors;
105 }
58119ee4
TO
106
107 /**
e7c15cb6 108 * @inheritDoc
4faa436b 109 *
e97c66ff 110 * @return array
0bb81f24 111 */
6a488035
TO
112 public function getKeys() {
113 $k2c = $this->getKeysToContainer();
114 return array_keys($k2c);
115 }
116
117 /**
e7c15cb6 118 * @inheritDoc
4faa436b
SB
119 *
120 * @param string $key
e97c66ff 121 *
122 * @throws \CRM_Extension_Exception_MissingException
6a488035
TO
123 */
124 public function getPath($key) {
125 return $this->getContainer($key)->getPath($key);
126 }
127
128 /**
e7c15cb6 129 * @inheritDoc
4faa436b
SB
130 *
131 * @param string $key
e97c66ff 132 *
133 * @throws \CRM_Extension_Exception_MissingException
6a488035
TO
134 */
135 public function getResUrl($key) {
136 return $this->getContainer($key)->getResUrl($key);
137 }
138
139 /**
e7c15cb6 140 * @inheritDoc
6a488035
TO
141 */
142 public function refresh() {
143 if ($this->cache) {
144 $this->cache->delete($this->cacheKey);
145 }
146 foreach ($this->containers as $container) {
147 $container->refresh();
148 }
149 }
150
151 /**
fe482240 152 * Get the container which defines a particular key.
6a488035 153 *
78612209
TO
154 * @param string $key
155 * Extension name.
dd244018
EM
156 *
157 * @throws CRM_Extension_Exception_MissingException
6a488035 158 * @return CRM_Extension_Container_Interface
6a488035
TO
159 */
160 public function getContainer($key) {
161 $k2c = $this->getKeysToContainer();
22c810a7 162 if (isset($k2c[$key]) && isset($this->containers[$k2c[$key]])) {
6a488035 163 return $this->containers[$k2c[$key]];
78612209
TO
164 }
165 else {
6a488035
TO
166 throw new CRM_Extension_Exception_MissingException("Unknown extension: $key");
167 }
168 }
169
170 /**
171 * Get a list of all keys in these containers -- and the
172 * name of the container which defines each key.
173 *
a6c01b45
CW
174 * @return array
175 * ($key => $containerName)
6a488035
TO
176 */
177 public function getKeysToContainer() {
178 if ($this->cache) {
179 $k2c = $this->cache->get($this->cacheKey);
180 }
c3d574fc 181 if (!isset($k2c) || !is_array($k2c)) {
be2fb01f 182 $k2c = [];
6a488035
TO
183 $containerNames = array_reverse(array_keys($this->containers));
184 foreach ($containerNames as $name) {
185 $keys = $this->containers[$name]->getKeys();
186 foreach ($keys as $key) {
187 $k2c[$key] = $name;
188 }
189 }
190 if ($this->cache) {
191 $this->cache->set($this->cacheKey, $k2c);
192 }
193 }
194 return $k2c;
195 }
96025800 196
6a488035 197}