__Before__: Every snippet/resource has a singular name. If the name ever changes,
it becomes a backward-compatibility break.
__After__: Every snippet/resource has a singular name, and it may optionally
have a list of aliases. If the name ever changes, you can leave an alias
for backward-compatibility.
*
* - 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
$snippet['styleFileUrls'] = $theme->resolveUrls($theme->getActiveThemeKey(), $ext, $file);
}
+ if (isset($snippet['aliases']) && !is_array($snippet['aliases'])) {
+ $snippet['aliases'] = [$snippet['aliases']];
+ }
+
$this->snippets[$snippet['name']] = $snippet;
$this->isSorted = FALSE;
return $snippet;
* @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;
}
* @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;
}
/**
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
$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.
*/