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