/** * 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]); }
/** * 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; } $cfs = $this->f3->get('ASSETS.combine.slots'); $slots = array_fill_keys(array_keys($cfs), array()); $sn = array_flip($cfs); // sort to slots $exclude = $this->f3->get('ASSETS.combine.exclude'); foreach ($collection as $i => $asset) { $a_slot = isset($asset['slot']) ? $asset['slot'] : NULL; // auto-create slot if ($a_slot && !isset($sn[$a_slot])) { $i = 50; while (isset($slots[$i])) { $i++; } $slots[$i] = array(); $sn[$a_slot] = $i; } // inline if ($asset['origin'] == 'inline') { $slots[$sn[$a_slot ?: 'inline']][] = $asset; continue; } // external if ($asset['origin'] == 'external') { $slots[$sn[$a_slot ?: 'external']][] = $asset; } elseif (is_file($asset['path']) && ((!isset($asset['exclude']) || !in_array('combine', $this->f3->split($asset['exclude']))) && (empty($exclude) || !preg_match('/' . $exclude . '/i', $asset['path']))) && (!isset($asset['media']) || in_array($asset['media'], array('all', 'screen')))) { $slots[$sn[$a_slot ?: 'internal']][] = $asset; } else { // excluded internal $slots[$sn[$a_slot ?: 'excluded']][] = $asset; } } // proceed slots ksort($slots); $out = array(); foreach ($slots as $assets) { $internal = array(); $inline = array(); $hash_key = array(); // categorize per slot foreach ($assets as $asset) { if ($asset['origin'] == 'internal') { $internal[$asset['type']][] = $asset; // check if one of our combined files was changed (mtime) if (!isset($hash_key[$asset['type']])) { $hash_key[$asset['type']] = ''; } $hash_key[$asset['type']] .= $asset['path'] . filemtime($asset['path']); } elseif ($asset['origin'] == 'external') { $out[] = $asset; } elseif ($asset['origin'] == 'inline') { $inline[$asset['type']][] = $asset['data']; } } // combine internals to one file if (!empty($internal)) { foreach ($internal as $type => $int_a) { $filepath = $public_path . $this->f3->hash($hash_key[$type]) . '.' . $type; if (!is_dir($public_path)) { mkdir($public_path, 0777, true); } $content = array(); if (!is_file($filepath)) { foreach ($int_a as $asset) { $data = $this->f3->read($asset['path']); if ($type == 'css') { $data = $this->fixRelativePaths($data, pathinfo($asset['path'], PATHINFO_DIRNAME) . '/'); } $content[] = $data; } $this->f3->write($filepath, implode(($type == 'js' ? ';' : '') . "\n", $content)); } $out[] = array('path' => $filepath, 'type' => $type, 'origin' => 'internal'); } } // combine inline if (!empty($inline)) { foreach ($inline as $type => $inl_a) { $out[] = array('data' => implode($inl_a), 'type' => $type, 'origin' => 'inline'); } } } return $out; }