/**
* combine a whole asset group
* @param $collection
* @return array
*/
public function combine($collection)
{
$public_path = $this->f3->get('ASSETS.combine.public_path');
if (empty($collection) || count($collection) <= 1) {
return $collection;
}
$type = false;
$hash_key = '';
$slots = array(0 => array(), 1 => array(), 2 => array(), 3 => array());
foreach ($collection as $i => $asset) {
$type = $asset['type'];
if ($asset['origin'] == 'inline') {
$slots[3][] = $asset['data'];
continue;
}
$path = $asset['path'];
$exclude = $this->f3->get('ASSETS.combine.exclude');
if ($asset['origin'] == 'external') {
$slots[0][] = $asset;
} elseif (is_file($path) && ((!isset($asset['exclude']) || !in_array('combine', $this->f3->split($asset['exclude']))) && (empty($exclude) || !preg_match('/' . $exclude . '/i', $path))) && (!isset($asset['media']) || in_array($asset['media'], array('all', 'screen')))) {
// check if one of our combined files was changed (mtime)
$hash_key .= $path . filemtime($path);
$slots[1][] = $path;
} else {
$slots[2][] = $asset;
}
}
if (!empty($slots[1])) {
$filepath = $public_path . $this->f3->hash($hash_key) . '.' . $type;
if (!is_dir($public_path)) {
mkdir($public_path, 0777, true);
}
$content = array();
if (!is_file($filepath)) {
foreach ($slots[1] as $path) {
$data = $this->f3->read($path);
if ($type == 'css') {
$data = $this->fixRelativePaths($data, pathinfo($path, PATHINFO_DIRNAME) . '/');
}
$content[] = $data;
}
$this->f3->write($filepath, implode(($type == 'js' ? ';' : '') . "\n", $content));
}
$slots[1] = array(array('path' => $filepath, 'type' => $type, 'origin' => 'internal'));
}
if (!empty($slots[3])) {
$slots[3] = array(array('data' => implode($slots[3]), 'type' => $type, 'origin' => 'inline'));
}
return array_merge($slots[0], $slots[1], $slots[2], $slots[3]);
}