From: Tim Otten Date: Fri, 16 Jan 2015 02:33:30 +0000 (-0800) Subject: CRM-15832 - CRM_Core_Resources - Add glob() function X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=16cd1eca7e4f57d6f4d40e993775a7be58849deb;p=civicrm-core.git CRM-15832 - CRM_Core_Resources - Add glob() function --- diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 61c7095345..9e97ed0321 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -443,14 +443,17 @@ class CRM_Core_Resources { * * @param string $ext * extension name; use 'civicrm' for core. - * @param string $file + * @param string|NULL $file * file path -- relative to the extension base dir. * * @return bool|string * full file path or FALSE if not found */ - public function getPath($ext, $file) { + public function getPath($ext, $file = NULL) { // TODO consider caching results + if ($file === NULL) { + return $this->extMapper->keyToBasePath($ext); + } $path = $this->extMapper->keyToBasePath($ext) . '/' . $file; if (is_file($path)) { return $path; @@ -480,6 +483,40 @@ class CRM_Core_Resources { return $this->extMapper->keyToUrl($ext) . '/' . $file; } + /** + * Evaluate a glob pattern in the context of a particular extension. + * + * @param string $ext + * Extension name; use 'civicrm' for core. + * @param string|array $patterns + * Glob pattern; e.g. "*.html". + * @param null|int $flags + * See glob(). + * @return array + * List of matching files, relative to the extension base dir. + * @see glob() + */ + public function glob($ext, $patterns, $flags = NULL) { + $path = $this->getPath($ext); + $patterns = (array) $patterns; + $files = array(); + foreach ($patterns as $pattern) { + if ($pattern{0} === '/') { + // Absolute path. + $files = array_merge($files, (array) glob($pattern, $flags)); + } + else { + // Relative path. + $files = array_merge($files, (array) glob("$path/$pattern", $flags)); + } + } + sort($files); // Deterministic order. + $files = array_unique($files); + return array_map(function ($file) use ($path) { + return CRM_Utils_File::relativize($file, "$path/"); + }, $files); + } + /** * @return string */ diff --git a/tests/phpunit/CRM/Core/ResourcesTest.php b/tests/phpunit/CRM/Core/ResourcesTest.php index 397a24be54..f91b84a034 100644 --- a/tests/phpunit/CRM/Core/ResourcesTest.php +++ b/tests/phpunit/CRM/Core/ResourcesTest.php @@ -281,6 +281,21 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase { $this->assertEquals('http://ext-dir/com.example.ext/', $actual); } + public function testGlob() { + $this->assertEquals( + array('info.xml'), + $this->res->glob('com.example.ext', 'info.xml') + ); + $this->assertEquals( + array('js/example.js'), + $this->res->glob('com.example.ext', 'js/*.js') + ); + $this->assertEquals( + array('js/example.js'), + $this->res->glob('com.example.ext', array('js/*.js')) + ); + } + /** * @param CRM_Utils_Cache_Interface $cache * @param string $cacheKey @@ -291,7 +306,9 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase { public function _createMapper(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) { $basedir = rtrim($this->createTempDir('ext-'), '/'); mkdir("$basedir/com.example.ext"); + mkdir("$basedir/com.example.ext/js"); file_put_contents("$basedir/com.example.ext/info.xml", "oddball"); + file_put_contents("$basedir/com.example.ext/js/example.js", "alert('Boo!');"); // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "