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