From 5244c98fa86b15fdbcc1f93bb15f4cc3fecb94a2 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 6 Mar 2013 19:11:02 -0500 Subject: [PATCH] CRM-12030 - Always use '/' in URLs (even for Windows) --- CRM/Extension/Container/Basic.php | 44 +++++++++++++++++++ .../CRM/Extension/Container/BasicTest.php | 13 ++++++ 2 files changed, 57 insertions(+) diff --git a/CRM/Extension/Container/Basic.php b/CRM/Extension/Container/Basic.php index 68ca76bf78..99138a0fb3 100644 --- a/CRM/Extension/Container/Basic.php +++ b/CRM/Extension/Container/Basic.php @@ -72,6 +72,17 @@ class CRM_Extension_Container_Basic implements CRM_Extension_Container_Interface */ public $relPaths = FALSE; + /** + * @var array($key => $relUrl) + * + * Derived from $relPaths. On Unix systems (where file-paths and + * URL-paths both use '/' separator), this isn't necessary. On Windows + * systems, this is derived from $relPaths. + * + * Note: Treat as private. This is only public to facilitate debugging. + */ + public $relUrls = FALSE; + /** * @param string $baseDir local path to the container * @param string $baseUrl public URL of the container @@ -170,4 +181,37 @@ class CRM_Extension_Container_Basic implements CRM_Extension_Container_Interface } return $this->relPaths; } + + /** + * Scan $basedir for a list of extension-keys + * + * @param string $dirSep the local system's directory separator + * @return array($key => $relUrl) + */ + protected function getRelUrls() { + if (DIRECTORY_SEPARATOR == '/') { + return $this->getRelPaths(); + } + if (!is_array($this->relUrls)) { + $this->relUrls = self::convertPathsToUrls(DIRECTORY_SEPARATOR, $this->getRelPaths()); + } + return $this->relUrls; + } + + /** + * Convert a list of relative paths to relative URLs. + * + * Note: Treat as private. This is only public to facilitate testing. + * + * @param string $dirSep + * @param array $relPaths ($key => $relPath) + * @return array($key => $relUrl) + */ + public static function convertPathsToUrls($dirSep, $relPaths) { + $relUrls = array(); + foreach ($relPaths as $key => $relPath) { + $relUrls[$key] = str_replace($dirSep, '/', $relPath); + } + return $relUrls; + } } diff --git a/tests/phpunit/CRM/Extension/Container/BasicTest.php b/tests/phpunit/CRM/Extension/Container/BasicTest.php index 8fd1fd4f53..ed3c25427e 100644 --- a/tests/phpunit/CRM/Extension/Container/BasicTest.php +++ b/tests/phpunit/CRM/Extension/Container/BasicTest.php @@ -96,4 +96,17 @@ class CRM_Extension_Container_BasicTest extends CiviUnitTestCase { $c = new CRM_Extension_Container_Basic($basedir . $appendPathGarbage, 'http://example/basedir' . $appendPathGarbage, $cache, $cacheKey); return array($basedir, $c); } + + function testConvertPathsToUrls() { + $relPaths = array( + 'foo.bar' => 'foo\bar', + 'whiz.bang' => 'tests\extensions\whiz\bang' + ); + $expectedRelUrls = array( + 'foo.bar' => 'foo/bar', + 'whiz.bang' => 'tests/extensions/whiz/bang', + ); + $actualRelUrls = CRM_Extension_Container_Basic::convertPathsToUrls('\\', $relPaths); + $this->assertEquals($expectedRelUrls, $actualRelUrls); + } } -- 2.25.1