Merge pull request #12639 from aniesshsethh/issue_314
[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 * @var array ($name => CRM_Extension_Container_Interface)
41 *
42 * Note: Treat as private. This is only public to facilitate debugging.
43 */
44 public $containers;
45
46 /**
47 * @var CRM_Utils_Cache_Interface|NULL
48 *
49 * Note: Treat as private. This is only public to facilitate debugging.
50 */
51 public $cache;
52
53 /**
54 * @var string the cache key used for any data stored by this container
55 *
56 * Note: Treat as private. This is only public to facilitate debugging.
57 */
58 public $cacheKey;
59
60 /**
61 * @var array ($key => $containerName)
62 *
63 * Note: Treat as private. This is only public to facilitate debugging.
64 */
65 public $k2c;
66
67 /**
68 * @param array $containers
69 * Array($name => CRM_Extension_Container_Interface) in order from highest
70 * priority (winners) to lowest priority (losers).
71 * @param CRM_Utils_Cache_Interface $cache
72 * Cache in which to store extension metadata.
73 * @param string $cacheKey
74 * Unique name for this container.
75 */
76 public function __construct($containers, CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
77 $this->containers = $containers;
78 $this->cache = $cache;
79 $this->cacheKey = $cacheKey;
80 }
81
82 /**
83 * @inheritDoc
84 *
85 * @return array
86 */
87 public function checkRequirements() {
88 $errors = array();
89 foreach ($this->containers as $container) {
90 $errors = array_merge($errors, $container->checkRequirements());
91 }
92 return $errors;
93 }
94
95 /**
96 * @inheritDoc
97 *
98 * @return array_keys
99 */
100 public function getKeys() {
101 $k2c = $this->getKeysToContainer();
102 return array_keys($k2c);
103 }
104
105 /**
106 * @inheritDoc
107 *
108 * @param string $key
109 */
110 public function getPath($key) {
111 return $this->getContainer($key)->getPath($key);
112 }
113
114 /**
115 * @inheritDoc
116 *
117 * @param string $key
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 = array();
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 }