CRM-12030 - Always use '/' in URLs (even for Windows)
[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 * @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
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
99 /**
100 * {@inheritdoc}
101 */
102 public function getKeys() {
103 return array_keys($this->getRelPaths());
104 }
105
106 /**
107 * {@inheritdoc}
108 */
109 public function getPath($key) {
110 return $this->baseDir . $this->getRelPath($key);
111 }
112
113 /**
114 * {@inheritdoc}
115 */
116 public function getResUrl($key) {
117 return $this->baseUrl . $this->getRelPath($key);
118 }
119
120 /**
121 * {@inheritdoc}
122 */
123 public function refresh() {
124 $this->relPaths = NULL;
125 if ($this->cache) {
126 $this->cache->delete($this->cacheKey);
127 }
128 }
129
130 /**
131 * @return string
132 */
133 public function getBaseDir() {
134 return $this->baseDir;
135 }
136
137 /**
138 * Determine the relative path of an extension directory
139 *
140 * @return string
141 * @throws CRM_Extension_Exception
142 */
143 protected function getRelPath($key) {
144 $keypaths = $this->getRelPaths();
145 if (! isset($keypaths[$key])) {
146 throw new CRM_Extension_Exception_MissingException("Failed to find extension: $key");
147 }
148 return $keypaths[$key];
149 }
150
151 /**
152 * Scan $basedir for a list of extension-keys
153 *
154 * @return array($key => $relPath)
155 */
156 protected function getRelPaths() {
157 if (!is_array($this->relPaths)) {
158 if ($this->cache) {
159 $this->relPaths = $this->cache->get($this->cacheKey);
160 }
161 if (!is_array($this->relPaths)) {
162 $this->relPaths = array();
163 $infoPaths = CRM_Utils_File::findFiles($this->baseDir, 'info.xml');
164 foreach ($infoPaths as $infoPath) {
165 $relPath = CRM_Utils_File::relativize(dirname($infoPath), $this->baseDir);
166 try {
167 $info = CRM_Extension_Info::loadFromFile($infoPath);
168 } catch (CRM_Extension_Exception_ParseException $e) {
169 CRM_Core_Session::setStatus(ts('Parse error in extension: %1', array(
170 1 => $e->getMessage(),
171 )), '', 'error');
172 CRM_Core_Error::debug_log_message("Parse error in extension: " . $e->getMessage());
173 continue;
174 }
175 $this->relPaths[$info->key] = $relPath;
176 }
177 if ($this->cache) {
178 $this->cache->set($this->cacheKey, $this->relPaths);
179 }
180 }
181 }
182 return $this->relPaths;
183 }
184
185 /**
186 * Scan $basedir for a list of extension-keys
187 *
188 * @param string $dirSep the local system's directory separator
189 * @return array($key => $relUrl)
190 */
191 protected function getRelUrls() {
192 if (DIRECTORY_SEPARATOR == '/') {
193 return $this->getRelPaths();
194 }
195 if (!is_array($this->relUrls)) {
196 $this->relUrls = self::convertPathsToUrls(DIRECTORY_SEPARATOR, $this->getRelPaths());
197 }
198 return $this->relUrls;
199 }
200
201 /**
202 * Convert a list of relative paths to relative URLs.
203 *
204 * Note: Treat as private. This is only public to facilitate testing.
205 *
206 * @param string $dirSep
207 * @param array $relPaths ($key => $relPath)
208 * @return array($key => $relUrl)
209 */
210 public static function convertPathsToUrls($dirSep, $relPaths) {
211 $relUrls = array();
212 foreach ($relPaths as $key => $relPath) {
213 $relUrls[$key] = str_replace($dirSep, '/', $relPath);
214 }
215 return $relUrls;
216 }
217 }