Skip to content

Commit

Permalink
Allow custom class per asset
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Jan 29, 2019
1 parent 63be0d2 commit 24d7438
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion libraries/src/WebAsset/WebAssetRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ public function exists(string $name): bool
*/
public function createAsset(string $name, array $data = []): WebAssetItem
{
$nameSpace = array_key_exists('namespace', $data) ? $data['namespace'] : __NAMESPACE__;
$className = array_key_exists('class', $data) ? $data['class'] : null;

if ($className && class_exists($nameSpace . '\\' . $className))
{
$className = $nameSpace . '\\' . $className;

return new $className($name, $data);
}

return new WebAssetItem($name, $data);
}

Expand Down Expand Up @@ -253,12 +263,20 @@ protected function parseRegistryFile($path)
'registryFile' => $path,
];

$namespace = array_key_exists('namespace', $data) ? $data['namespace'] : null;

// Prepare WebAssetItem instances
foreach ($data['assets'] as $item)
{
if (empty($item['name']))
{
throw new \RuntimeException('Asset data file "' . $path . '" contains incorrect asset defination');
throw new \RuntimeException('Asset data file "' . $path . '" contains incorrect asset definition');
}

// Inheriting the Namespace
if ($namespace && !array_key_exists('namespace', $item))
{
$item['namespace'] = $namespace;
}

$item['assetSource'] = $assetSource;
Expand Down

0 comments on commit 24d7438

Please sign in to comment.