Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * @package CRM | |
06b69b18 | 30 | * @copyright CiviCRM LLC (c) 2004-2014 |
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 | */ | |
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 | ||
5244c98f TO |
75 | /** |
76 | * @var array($key => $relUrl) | |
77 | * | |
78 | * Derived from $relPaths. On Unix systems (where file-paths and | |
79 | * URL-paths both use '/' separator), this isn't necessary. On Windows | |
80 | * systems, this is derived from $relPaths. | |
81 | * | |
82 | * Note: Treat as private. This is only public to facilitate debugging. | |
83 | */ | |
84 | public $relUrls = FALSE; | |
85 | ||
6a488035 TO |
86 | /** |
87 | * @param string $baseDir local path to the container | |
88 | * @param string $baseUrl public URL of the container | |
89 | * @param CRM_Utils_Cache_Interface $cache | |
90 | * @param string $cacheKey unique name for this container | |
91 | */ | |
92 | public function __construct($baseDir, $baseUrl, CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) { | |
93 | $this->cache = $cache; | |
94 | $this->cacheKey = $cacheKey; | |
95 | $this->baseDir = rtrim($baseDir, '/'); | |
96 | $this->baseUrl = rtrim($baseUrl, '/'); | |
97 | } | |
98 | ||
0bb81f24 TO |
99 | /** |
100 | * {@inheritdoc} | |
101 | */ | |
102 | public function checkRequirements() { | |
103 | $errors = array(); | |
104 | ||
105 | if (empty($this->baseDir) || !is_dir($this->baseDir)) { | |
106 | $errors[] = array( | |
107 | 'title' => ts('Invalid Base Directory'), | |
108 | 'message' => ts('An extension container has been defined with a blank directory.'), | |
109 | ); | |
110 | } | |
111 | if (empty($this->baseUrl)) { | |
0bb81f24 TO |
112 | $errors[] = array( |
113 | 'title' => ts('Invalid Base URL'), | |
114 | 'message' => ts('An extension container has been defined with a blank URL.'), | |
115 | ); | |
116 | } | |
117 | ||
118 | return $errors; | |
119 | } | |
120 | ||
6a488035 TO |
121 | /** |
122 | * {@inheritdoc} | |
123 | */ | |
124 | public function getKeys() { | |
125 | return array_keys($this->getRelPaths()); | |
126 | } | |
127 | ||
128 | /** | |
129 | * {@inheritdoc} | |
130 | */ | |
131 | public function getPath($key) { | |
132 | return $this->baseDir . $this->getRelPath($key); | |
133 | } | |
134 | ||
135 | /** | |
136 | * {@inheritdoc} | |
137 | */ | |
138 | public function getResUrl($key) { | |
f7a9471a TO |
139 | if (! $this->baseUrl) { |
140 | CRM_Core_Session::setStatus( | |
141 | ts('Failed to determine URL for extension (%1). Please update <a href="%2">Resource URLs</a>.', | |
142 | array( | |
143 | 1 => $key, | |
144 | 2 => CRM_Utils_System::url('civicrm/admin/setting/url', 'reset=1'), | |
145 | ) | |
146 | ) | |
147 | ); | |
148 | } | |
bb9944e6 | 149 | return $this->baseUrl . $this->getRelUrl($key); |
6a488035 TO |
150 | } |
151 | ||
152 | /** | |
153 | * {@inheritdoc} | |
154 | */ | |
155 | public function refresh() { | |
156 | $this->relPaths = NULL; | |
157 | if ($this->cache) { | |
158 | $this->cache->delete($this->cacheKey); | |
159 | } | |
160 | } | |
161 | ||
162 | /** | |
163 | * @return string | |
164 | */ | |
165 | public function getBaseDir() { | |
166 | return $this->baseDir; | |
167 | } | |
168 | ||
169 | /** | |
170 | * Determine the relative path of an extension directory | |
171 | * | |
fd31fa4c EM |
172 | * @param $key |
173 | * | |
174 | * @throws CRM_Extension_Exception_MissingException | |
6a488035 | 175 | * @return string |
6a488035 TO |
176 | */ |
177 | protected function getRelPath($key) { | |
178 | $keypaths = $this->getRelPaths(); | |
0bb81f24 | 179 | if (!isset($keypaths[$key])) { |
6a488035 TO |
180 | throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key"); |
181 | } | |
182 | return $keypaths[$key]; | |
183 | } | |
184 | ||
185 | /** | |
186 | * Scan $basedir for a list of extension-keys | |
187 | * | |
188 | * @return array($key => $relPath) | |
189 | */ | |
190 | protected function getRelPaths() { | |
191 | if (!is_array($this->relPaths)) { | |
192 | if ($this->cache) { | |
193 | $this->relPaths = $this->cache->get($this->cacheKey); | |
194 | } | |
195 | if (!is_array($this->relPaths)) { | |
196 | $this->relPaths = array(); | |
197 | $infoPaths = CRM_Utils_File::findFiles($this->baseDir, 'info.xml'); | |
198 | foreach ($infoPaths as $infoPath) { | |
199 | $relPath = CRM_Utils_File::relativize(dirname($infoPath), $this->baseDir); | |
200 | try { | |
201 | $info = CRM_Extension_Info::loadFromFile($infoPath); | |
202 | } catch (CRM_Extension_Exception_ParseException $e) { | |
203 | CRM_Core_Session::setStatus(ts('Parse error in extension: %1', array( | |
204 | 1 => $e->getMessage(), | |
205 | )), '', 'error'); | |
206 | CRM_Core_Error::debug_log_message("Parse error in extension: " . $e->getMessage()); | |
207 | continue; | |
208 | } | |
209 | $this->relPaths[$info->key] = $relPath; | |
210 | } | |
211 | if ($this->cache) { | |
212 | $this->cache->set($this->cacheKey, $this->relPaths); | |
213 | } | |
214 | } | |
215 | } | |
216 | return $this->relPaths; | |
217 | } | |
5244c98f | 218 | |
bb9944e6 TO |
219 | /** |
220 | * Determine the relative path of an extension directory | |
221 | * | |
fd31fa4c EM |
222 | * @param $key |
223 | * | |
224 | * @throws CRM_Extension_Exception_MissingException | |
bb9944e6 | 225 | * @return string |
bb9944e6 TO |
226 | */ |
227 | protected function getRelUrl($key) { | |
228 | $relUrls = $this->getRelUrls(); | |
0bb81f24 | 229 | if (!isset($relUrls[$key])) { |
bb9944e6 TO |
230 | throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key"); |
231 | } | |
232 | return $relUrls[$key]; | |
233 | } | |
234 | ||
5244c98f TO |
235 | /** |
236 | * Scan $basedir for a list of extension-keys | |
237 | * | |
dd244018 | 238 | * @internal param string $dirSep the local system's directory separator |
5244c98f TO |
239 | * @return array($key => $relUrl) |
240 | */ | |
241 | protected function getRelUrls() { | |
242 | if (DIRECTORY_SEPARATOR == '/') { | |
243 | return $this->getRelPaths(); | |
244 | } | |
245 | if (!is_array($this->relUrls)) { | |
246 | $this->relUrls = self::convertPathsToUrls(DIRECTORY_SEPARATOR, $this->getRelPaths()); | |
247 | } | |
248 | return $this->relUrls; | |
249 | } | |
250 | ||
251 | /** | |
252 | * Convert a list of relative paths to relative URLs. | |
253 | * | |
254 | * Note: Treat as private. This is only public to facilitate testing. | |
255 | * | |
256 | * @param string $dirSep | |
257 | * @param array $relPaths ($key => $relPath) | |
258 | * @return array($key => $relUrl) | |
259 | */ | |
260 | public static function convertPathsToUrls($dirSep, $relPaths) { | |
261 | $relUrls = array(); | |
262 | foreach ($relPaths as $key => $relPath) { | |
263 | $relUrls[$key] = str_replace($dirSep, '/', $relPath); | |
264 | } | |
265 | return $relUrls; | |
266 | } | |
6a488035 | 267 | } |