*
* @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;
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
*/
$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
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", "<extension key='com.example.ext' type='report'><file>oddball</file></extension>");
+ file_put_contents("$basedir/com.example.ext/js/example.js", "alert('Boo!');");
// not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
$c = new CRM_Extension_Container_Basic($basedir, 'http://ext-dir', $cache, $cacheKey);
$mapper = new CRM_Extension_Mapper($c, NULL, NULL, '/pathto/civicrm', 'http://core-app');