Skip to content

Commit

Permalink
hook_civicrm_angularModules - Allow JS files from assetBuilder and ot…
Browse files Browse the repository at this point in the history
…her ext's

== Before ==

In `hook_civicrm_angularModules`, the `js` resources are assumed
to be file-blobs relative to the declared extension.

== After ==

In `hook_civicrm_angularModules`, the `js` resources can be:

 * `assetBuilder://NAME?PARAMS`; in which case the JS file is loaded from the `asset_builder` service
 * `ext://EXTKEY/FILE`; in which case the JS file is loaded form another extension
 * Otherwise, the resource is assumed to be file-blob relative to the declared extension
  • Loading branch information
totten committed Sep 20, 2017
1 parent 0da76be commit 2e7ddc0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CRM/Core/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,9 @@ public function glob($ext, $patterns, $flags = NULL) {
$patterns = (array) $patterns;
$files = array();
foreach ($patterns as $pattern) {
if (preg_match(';^(assetBuilder|ext)://;', $pattern)) {
$files[] = $pattern;
}
if (CRM_Utils_File::isAbsolute($pattern)) {
// Absolute path.
$files = array_merge($files, (array) glob($pattern, $flags));
Expand Down
4 changes: 2 additions & 2 deletions CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2110,8 +2110,8 @@ public static function alterResourceSettings(&$data) {
* );
* $angularModules['myBigAngularModule'] = array(
* 'ext' => 'org.example.mymod',
* 'js' => array('js/part1.js', 'js/part2.js'),
* 'css' => array('css/myAngularModule.css'),
* 'js' => array('js/part1.js', 'js/part2.js', 'ext://other.ext.name/file.js', 'assetBuilder://dynamicAsset.js'),
* 'css' => array('css/myAngularModule.css', 'ext://other.ext.name/file.css', 'assetBuilder://dynamicAsset.css'),
* 'partials' => array('partials/myBigAngularModule'),
* 'requires' => array('otherModuleA', 'otherModuleB'),
* 'basePages' => array('civicrm/a'),
Expand Down
34 changes: 33 additions & 1 deletion Civi/Angular/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,12 @@ public function getResources($moduleNames, $resType, $refType) {
$module = $this->getModule($moduleName);
if (isset($module[$resType])) {
foreach ($module[$resType] as $file) {
switch ($refType) {
$refTypeSuffix = '';
if (preg_match(';^(assetBuilder|ext)://;', $file)) {
$refTypeSuffix = '-' . parse_url($file, PHP_URL_SCHEME);
}

switch ($refType . $refTypeSuffix) {
case 'path':
$result[] = $this->res->getPath($module['ext'], $file);
break;
Expand All @@ -340,6 +345,33 @@ public function getResources($moduleNames, $resType, $refType) {
$result[] = $this->res->getUrl($module['ext'], $file, TRUE);
break;

case 'path-assetBuilder':
$assetName = parse_url($file, PHP_URL_HOST) . parse_url($file, PHP_URL_PATH);
$assetParams = array();
parse_str('' . parse_url($file, PHP_URL_QUERY), $assetParams);
$result[] = \Civi::service('asset_builder')->getPath($assetName, $assetParams);
break;

case 'rawUrl-assetBuilder':
case 'cacheUrl-assetBuilder':
$assetName = parse_url($file, PHP_URL_HOST) . parse_url($file, PHP_URL_PATH);
$assetParams = array();
parse_str('' . parse_url($file, PHP_URL_QUERY), $assetParams);
$result[] = \Civi::service('asset_builder')->getUrl($assetName, $assetParams);
break;

case 'path-ext':
$result[] = $this->res->getPath(parse_url($file, PHP_URL_HOST), ltrim(parse_url($file, PHP_URL_PATH), '/'));
break;

case 'rawUrl-ext':
$result[] = $this->res->getUrl(parse_url($file, PHP_URL_HOST), ltrim(parse_url($file, PHP_URL_PATH), '/'));
break;

case 'cacheUrl-ext':
$result[] = $this->res->getUrl(parse_url($file, PHP_URL_HOST), ltrim(parse_url($file, PHP_URL_PATH), '/'), TRUE);
break;

case 'settings':
case 'requires':
if (!empty($module[$resType])) {
Expand Down
17 changes: 17 additions & 0 deletions Civi/Core/AssetBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ public function getUrl($name, $params = array()) {
}
}

/**
* @param string $name
* Ex: 'angular.json'.
* @param array $params
* @return string
* URL.
* Ex: '/var/www/files/civicrm/dyn/angular.abcd1234abcd1234.json'.
*/
public function getPath($name, $params = array()) {
if (!$this->isValidName($name)) {
throw new \RuntimeException("Invalid dynamic asset name");
}

$fileName = $this->build($name, $params);
return $this->getCachePath($fileName);
}

/**
* Build the cached copy of an $asset.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/Civi/Angular/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ public function testGetPartials_Hooked() {
// If crmMailing changes, feel free to use a different example.
}

public function testGetJs_Asset() {
\CRM_Utils_Hook::singleton()->setHook('civicrm_angularModules', array($this, 'hook_civicrm_angularModules_fooBar'));

$paths = $this->angular->getResources(array('fooBar'), 'js', 'path');
$this->assertRegExp('/visual-bundle.[a-z0-9]+.js/', $paths[0]);
$this->assertRegExp('/crossfilter/', file_get_contents($paths[0]));

$this->assertRegExp('/Common.js/', $paths[1]);
$this->assertRegExp('/console/', file_get_contents($paths[1]));
}

/**
* Get a translatable string from an example module.
*/
Expand Down Expand Up @@ -209,4 +220,14 @@ public function hook_civicrm_alterAngular($angular) {
);
}

public function hook_civicrm_angularModules_fooBar(&$angularModules) {
$angularModules['fooBar'] = array(
'ext' => 'civicrm',
'js' => array(
'assetBuilder://visual-bundle.js',
'ext://civicrm/js/Common.js',
),
);
}

}

0 comments on commit 2e7ddc0

Please sign in to comment.