Merge pull request #125 from pradpnayak/CRM-11983
[civicrm-core.git] / CRM / Extension / Container / Collection.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 /**
69 * @param array $containers array($name => CRM_Extension_Container_Interface) in order from highest priority (winners) to lowest priority (losers)
70 * @param CRM_Utils_Cache_Interface $cache
71 * @param string $cacheKey unique name for this container
72 */
73 public function __construct($containers, CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
74 $this->containers = $containers;
75 $this->cache = $cache;
76 $this->cacheKey = $cacheKey;
77 }
78
79 /**
80 * {@inheritdoc}
81 */
0bb81f24
TO
82 public function checkRequirements() {
83 $errors = array();
84 foreach ($this->containers as $container) {
85 $errors = array_merge($errors, $container->checkRequirements());
86 }
87 return $errors;
88 }
89 /**
90 * {@inheritdoc}
91 */
6a488035
TO
92 public function getKeys() {
93 $k2c = $this->getKeysToContainer();
94 return array_keys($k2c);
95 }
96
97 /**
98 * {@inheritdoc}
99 */
100 public function getPath($key) {
101 return $this->getContainer($key)->getPath($key);
102 }
103
104 /**
105 * {@inheritdoc}
106 */
107 public function getResUrl($key) {
108 return $this->getContainer($key)->getResUrl($key);
109 }
110
111 /**
112 * {@inheritdoc}
113 */
114 public function refresh() {
115 if ($this->cache) {
116 $this->cache->delete($this->cacheKey);
117 }
118 foreach ($this->containers as $container) {
119 $container->refresh();
120 }
121 }
122
123 /**
124 * Get the container which defines a particular key
125 *
126 * @return CRM_Extension_Container_Interface
127 * @throws CRM_Extension_Exception
128 */
129 public function getContainer($key) {
130 $k2c = $this->getKeysToContainer();
131 if (isset($k2c[$key])) {
132 return $this->containers[$k2c[$key]];
133 } else {
134 throw new CRM_Extension_Exception_MissingException("Unknown extension: $key");
135 }
136 }
137
138 /**
139 * Get a list of all keys in these containers -- and the
140 * name of the container which defines each key.
141 *
142 * @return array ($key => $containerName)
143 */
144 public function getKeysToContainer() {
145 if ($this->cache) {
146 $k2c = $this->cache->get($this->cacheKey);
147 }
148 if (!is_array($k2c)) {
149 $k2c = array();
150 $containerNames = array_reverse(array_keys($this->containers));
151 foreach ($containerNames as $name) {
152 $keys = $this->containers[$name]->getKeys();
153 foreach ($keys as $key) {
154 $k2c[$key] = $name;
155 }
156 }
157 if ($this->cache) {
158 $this->cache->set($this->cacheKey, $k2c);
159 }
160 }
161 return $k2c;
162 }
163}