Merge pull request #9637 from omarabuhussein/CRM-19832-searchtask-hook
[civicrm-core.git] / CRM / Extension / Container / Collection.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 30 * @copyright CiviCRM LLC (c) 2004-2017
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 /**
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 /**
78612209
TO
68 * @param array $containers
69 * Array($name => CRM_Extension_Container_Interface) in order from highest
70 * priority (winners) to lowest priority (losers).
6a488035 71 * @param CRM_Utils_Cache_Interface $cache
78612209
TO
72 * Cache in which to store extension metadata.
73 * @param string $cacheKey
74 * Unique name for this container.
6a488035
TO
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 /**
e7c15cb6 83 * @inheritDoc
4faa436b
SB
84 *
85 * @return array
6a488035 86 */
0bb81f24
TO
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 }
58119ee4
TO
94
95 /**
e7c15cb6 96 * @inheritDoc
4faa436b
SB
97 *
98 * @return array_keys
0bb81f24 99 */
6a488035
TO
100 public function getKeys() {
101 $k2c = $this->getKeysToContainer();
102 return array_keys($k2c);
103 }
104
105 /**
e7c15cb6 106 * @inheritDoc
4faa436b
SB
107 *
108 * @param string $key
6a488035
TO
109 */
110 public function getPath($key) {
111 return $this->getContainer($key)->getPath($key);
112 }
113
114 /**
e7c15cb6 115 * @inheritDoc
4faa436b
SB
116 *
117 * @param string $key
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)) {
6a488035
TO
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 }
96025800 180
6a488035 181}