Back to WebAssetRegistry class

Method parseRegistryFile

protected void
parseRegistryFile
(mixed $path)
Parse registry file
Parameters
  • string $path Relative path to the data file
Returns
  • void
Since
  • 4.0.0
-
  • \RuntimeException If file is empty or invalid

Method parseRegistryFile - Source code

/**
 * Parse registry file
 *
 * @param   string  $path  Relative path to the data file
 *
 * @return  void
 *
 * @throws  \RuntimeException If file is empty or invalid
 *
 * @since   4.0.0
 */
protected function parseRegistryFile($path)
{
    $data = file_get_contents(JPATH_ROOT . '/' . $path);
    $data = $data ? json_decode($data, true) : null;
    if ($data === null) {
        throw new \RuntimeException(sprintf('Asset registry file "%s" contains invalid JSON', $path));
    }
    // Check if asset field exists and contains data. If it doesn't - we can just bail here.
    if (empty($data['assets'])) {
        return;
    }
    // Keep source info
    $assetSource = ['registryFile' => $path];
    $namespace = \array_key_exists('namespace', $data) ? $data['namespace'] : null;
    // Prepare WebAssetItem instances
    foreach ($data['assets'] as $i => $item) {
        if (empty($item['name'])) {
            throw new \RuntimeException(sprintf('Failed parsing asset registry file "%s". Property "name" is required for asset index "%s"', $path, $i));
        }
        if (empty($item['type'])) {
            throw new \RuntimeException(sprintf('Failed parsing asset registry file "%s". Property "type" is required for asset "%s"', $path, $item['name']));
        }
        $item['type'] = strtolower($item['type']);
        $name = $item['name'];
        $uri = $item['uri'] ?? '';
        $options = $item;
        $options['assetSource'] = $assetSource;
        unset($options['uri'], $options['name']);
        // Inheriting the Namespace
        if ($namespace && !\array_key_exists('namespace', $options)) {
            $options['namespace'] = $namespace;
        }
        $assetItem = $this->createAsset($name, $uri, $options);
        $this->add($item['type'], $assetItem);
    }
}