Skip to content
Snippets Groups Projects
Unverified Commit 953fb16d authored by Eileen McNaughton's avatar Eileen McNaughton Committed by GitHub
Browse files

Merge pull request #18765 from totten/5.31-res-alias

#2117 - Add compatibility aliases for 'scriptFile' and 'styleFile' resources
parents 8af44ce5 920161ed
Branches
Tags
No related merge requests found
......@@ -50,6 +50,7 @@
*
* - type: string (markup, template, callback, script, scriptFile, scriptUrl, jquery, style, styleFile, styleUrl)
* - name: string, symbolic identifier for this resource
* - aliases: string[], list of alternative names for this resource
* - weight: int, default=1. Lower weights come before higher weights.
* (If two resources have the same weight, then a secondary ordering will be
* used to ensure reproducibility. However, the secondary ordering is
......
......@@ -114,6 +114,9 @@ trait CRM_Core_Resources_CollectionTrait {
}
$snippet['scriptFileUrls'] = [$res->getUrl($ext, $res->filterMinify($ext, $file), TRUE)];
}
if ($snippet['type'] === 'scriptFile' && !isset($snippet['aliases'])) {
$snippet['aliases'] = $snippet['scriptFileUrls'];
}
if ($snippet['type'] === 'styleFile' && !isset($snippet['styleFileUrls'])) {
/** @var Civi\Core\Themes $theme */
......@@ -121,6 +124,13 @@ trait CRM_Core_Resources_CollectionTrait {
list ($ext, $file) = $snippet['styleFile'];
$snippet['styleFileUrls'] = $theme->resolveUrls($theme->getActiveThemeKey(), $ext, $file);
}
if ($snippet['type'] === 'styleFile' && !isset($snippet['aliases'])) {
$snippet['aliases'] = $snippet['styleFileUrls'];
}
if (isset($snippet['aliases']) && !is_array($snippet['aliases'])) {
$snippet['aliases'] = [$snippet['aliases']];
}
$this->snippets[$snippet['name']] = $snippet;
$this->isSorted = FALSE;
......@@ -149,8 +159,15 @@ trait CRM_Core_Resources_CollectionTrait {
* @see CRM_Core_Resources_CollectionInterface::update()
*/
public function update($name, $snippet) {
$this->snippets[$name] = array_merge($this->snippets[$name], $snippet);
$this->isSorted = FALSE;
foreach ($this->resolveName($name) as $realName) {
$this->snippets[$realName] = array_merge($this->snippets[$realName], $snippet);
$this->isSorted = FALSE;
return $this;
}
Civi::log()->warning('Failed to update resource by name ({name})', [
'name' => $name,
]);
return $this;
}
......@@ -174,7 +191,12 @@ trait CRM_Core_Resources_CollectionTrait {
* @see CRM_Core_Resources_CollectionInterface::get()
*/
public function &get($name) {
return $this->snippets[$name];
foreach ($this->resolveName($name) as $realName) {
return $this->snippets[$realName];
}
$null = NULL;
return $null;
}
/**
......@@ -290,6 +312,24 @@ trait CRM_Core_Resources_CollectionTrait {
return $this;
}
/**
* @param string $name
* Name or alias.
* return array
* List of real names.
*/
protected function resolveName($name) {
if (isset($this->snippets[$name])) {
return [$name];
}
foreach ($this->snippets as $snippetName => $snippet) {
if (isset($snippet['aliases']) && in_array($name, $snippet['aliases'])) {
return [$snippetName];
}
}
return [];
}
/**
* @param $a
* @param $b
......
......@@ -222,6 +222,45 @@ trait CRM_Core_Resources_CollectionTestTrait {
$this->assertEquals(1, $count, 'Expect one registered snippet');
}
/**
* Create a few resources with aliases. Use a mix of reads+writes on both the
* canonical names and aliased names.
*/
public function testAliases() {
$b = $this->createEmptyCollection();
$b->add([
'styleUrl' => 'https://example.com/foo.css',
'name' => 'foo',
'aliases' => ['bar', 'borg'],
]);
$b->add([
'scriptUrl' => 'https://example.com/whiz.js',
'name' => 'whiz',
'aliases' => 'bang',
]);
$this->assertEquals('foo', $b->get('foo')['name']);
$this->assertEquals('foo', $b->get('bar')['name']);
$this->assertEquals('foo', $b->get('borg')['name']);
$this->assertEquals('whiz', $b->get('whiz')['name']);
$this->assertEquals('whiz', $b->get('bang')['name']);
$this->assertEquals(NULL, $b->get('snafu'));
// Go back+forth, updating with one name then reading with the other.
$b->get('borg')['borgify'] = TRUE;
$this->assertEquals(TRUE, $b->get('foo')['borgify']);
$b->get('foo')['d'] = 'ie';
$this->assertEquals('ie', $b->get('borg')['d']);
$b->update('bang', ['b52' => 'love shack']);
$this->assertEquals('love shack', $b->get('whiz')['b52']);
$b->update('whiz', ['golly' => 'gee']);
$this->assertEquals('gee', $b->get('bang')['golly']);
}
/**
* Add some items to a bundle - then clear() all of them.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment