public function AssetFactory::createAsset

Creates a new asset.

Prefixing a filter name with a question mark will cause it to be omitted when the factory is in debug mode.

Available options:

  • output: An output string
  • name: An asset name for interpolation in output patterns
  • debug: Forces debug mode on or off for this asset
  • root: An array or string of more root directories

Parameters

array|string $inputs An array of input strings:

array|string $filters An array of filter names:

array $options An array of options:

Return value

AssetCollection An asset collection

File

drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Factory/AssetFactory.php, line 152

Class

AssetFactory
The asset factory creates asset objects.

Namespace

Assetic\Factory

Code

public function createAsset($inputs = array(), $filters = array(), array $options = array()) {
  if (!is_array($inputs)) {
    $inputs = array(
      $inputs,
    );
  }
  if (!is_array($filters)) {
    $filters = array(
      $filters,
    );
  }
  if (!isset($options['output'])) {
    $options['output'] = $this->output;
  }
  if (!isset($options['vars'])) {
    $options['vars'] = array();
  }
  if (!isset($options['debug'])) {
    $options['debug'] = $this->debug;
  }
  if (!isset($options['root'])) {
    $options['root'] = array(
      $this->root,
    );
  }
  else {
    if (!is_array($options['root'])) {
      $options['root'] = array(
        $options['root'],
      );
    }
    $options['root'][] = $this->root;
  }
  if (!isset($options['name'])) {
    $options['name'] = $this
      ->generateAssetName($inputs, $filters, $options);
  }
  $asset = $this
    ->createAssetCollection(array(), $options);
  $extensions = array();

  // inner assets
  foreach ($inputs as $input) {
    if (is_array($input)) {

      // nested formula
      $asset
        ->add(call_user_func_array(array(
        $this,
        'createAsset',
      ), $input));
    }
    else {
      $asset
        ->add($this
        ->parseInput($input, $options));
      $extensions[pathinfo($input, PATHINFO_EXTENSION)] = true;
    }
  }

  // filters
  foreach ($filters as $filter) {
    if ('?' != $filter[0]) {
      $asset
        ->ensureFilter($this
        ->getFilter($filter));
    }
    elseif (!$options['debug']) {
      $asset
        ->ensureFilter($this
        ->getFilter(substr($filter, 1)));
    }
  }

  // append variables
  if (!empty($options['vars'])) {
    $toAdd = array();
    foreach ($options['vars'] as $var) {
      if (false !== strpos($options['output'], '{' . $var . '}')) {
        continue;
      }
      $toAdd[] = '{' . $var . '}';
    }
    if ($toAdd) {
      $options['output'] = str_replace('*', '*.' . implode('.', $toAdd), $options['output']);
    }
  }

  // append consensus extension if missing
  if (1 == count($extensions) && !pathinfo($options['output'], PATHINFO_EXTENSION) && ($extension = key($extensions))) {
    $options['output'] .= '.' . $extension;
  }

  // output --> target url
  $asset
    ->setTargetPath(str_replace('*', $options['name'], $options['output']));

  // apply workers and return
  return $this
    ->applyWorkers($asset);
}