Merge pull request #42 from pradpnayak/CRM-11983
[civicrm-core.git] / CRM / Extension / Container / Basic.php
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 */
38 class CRM_Extension_Container_Basic implements CRM_Extension_Container_Interface {
39
40 /**
41 * @var string
42 *
43 * Note: Treat as private. This is only public to facilitate debugging.
44 */
45 public $baseDir;
46
47 /**
48 * @var string
49 *
50 * Note: Treat as private. This is only public to facilitate debugging.
51 */
52 public $baseUrl;
53
54 /**
55 * @var CRM_Utils_Cache_Interface|NULL
56 *
57 * Note: Treat as private. This is only public to facilitate debugging.
58 */
59 public $cache;
60
61 /**
62 * @var string the cache key used for any data stored by this container
63 *
64 * Note: Treat as private. This is only public to facilitate debugging.
65 */
66 public $cacheKey;
67
68 /**
69 * @var array($key => $relPath)
70 *
71 * Note: Treat as private. This is only public to facilitate debugging.
72 */
73 public $relPaths = FALSE;
74
75 /**
76 * @param string $baseDir local path to the container
77 * @param string $baseUrl public URL of the container
78 * @param CRM_Utils_Cache_Interface $cache
79 * @param string $cacheKey unique name for this container
80 */
81 public function __construct($baseDir, $baseUrl, CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
82 $this->cache = $cache;
83 $this->cacheKey = $cacheKey;
84 $this->baseDir = rtrim($baseDir, '/');
85 $this->baseUrl = rtrim($baseUrl, '/');
86 }
87
88 /**
89 * {@inheritdoc}
90 */
91 public function getKeys() {
92 return array_keys($this->getRelPaths());
93 }
94
95 /**
96 * {@inheritdoc}
97 */
98 public function getPath($key) {
99 return $this->baseDir . $this->getRelPath($key);
100 }
101
102 /**
103 * {@inheritdoc}
104 */
105 public function getResUrl($key) {
106 return $this->baseUrl . $this->getRelPath($key);
107 }
108
109 /**
110 * {@inheritdoc}
111 */
112 public function refresh() {
113 $this->relPaths = NULL;
114 if ($this->cache) {
115 $this->cache->delete($this->cacheKey);
116 }
117 }
118
119 /**
120 * @return string
121 */
122 public function getBaseDir() {
123 return $this->baseDir;
124 }
125
126 /**
127 * Determine the relative path of an extension directory
128 *
129 * @return string
130 * @throws CRM_Extension_Exception
131 */
132 protected function getRelPath($key) {
133 $keypaths = $this->getRelPaths();
134 if (! isset($keypaths[$key])) {
135 throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key");
136 }
137 return $keypaths[$key];
138 }
139
140 /**
141 * Scan $basedir for a list of extension-keys
142 *
143 * @return array($key => $relPath)
144 */
145 protected function getRelPaths() {
146 if (!is_array($this->relPaths)) {
147 if ($this->cache) {
148 $this->relPaths = $this->cache->get($this->cacheKey);
149 }
150 if (!is_array($this->relPaths)) {
151 $this->relPaths = array();
152 $infoPaths = CRM_Utils_File::findFiles($this->baseDir, 'info.xml');
153 foreach ($infoPaths as $infoPath) {
154 $relPath = CRM_Utils_File::relativize(dirname($infoPath), $this->baseDir);
155 try {
156 $info = CRM_Extension_Info::loadFromFile($infoPath);
157 } catch (CRM_Extension_Exception_ParseException $e) {
158 CRM_Core_Session::setStatus(ts('Parse error in extension: %1', array(
159 1 => $e->getMessage(),
160 )), '', 'error');
161 CRM_Core_Error::debug_log_message("Parse error in extension: " . $e->getMessage());
162 continue;
163 }
164 $this->relPaths[$info->key] = $relPath;
165 }
166 if ($this->cache) {
167 $this->cache->set($this->cacheKey, $this->relPaths);
168 }
169 }
170 }
171 return $this->relPaths;
172 }
173 }