Lime\Helper\Assets::compile PHP Method

compile() public method

Compile assets into one file
public compile ( Array $assets, String $type ) : String
$assets Array
$type String js or css
return String
    public function compile($assets, $type)
    {
        $self = $this;
        $rewriteCssUrls = function ($content, $asset) use($self) {
            $source_dir = dirname($asset["file"]);
            $root_dir = $self->app['docs_root'];
            $csspath = "";
            if (strlen($root_dir) < strlen($source_dir)) {
                $csspath = '/' . trim(str_replace($root_dir, '', $source_dir), "/") . "/";
            } else {
                // todo
            }
            $offset = 0;
            while (($pos = strpos($content, 'url(', $offset)) !== false) {
                if (($urlend = strpos($content, ')', $pos)) !== false) {
                    $path = trim(str_replace(array('"', "'"), "", substr($content, $pos + 4, $urlend - ($pos + 4))));
                    if (!preg_match("#^(http|/|data\\:)#", trim($path))) {
                        $content = str_replace($path, $csspath . $path, $content);
                    }
                }
                $offset = $pos + 1;
            }
            return $content;
        };
        $output = array();
        foreach ((array) $assets as $file) {
            $asset = array("ext" => pathinfo($file, PATHINFO_EXTENSION), "file" => $file);
            $ext = $asset['ext'];
            $content = '';
            if (strpos($file, ':') !== false && ($____file = $this->app->path($file))) {
                $asset['file'] = $file = $____file;
            }
            if ($ext != $type) {
                continue;
            }
            switch ($asset['ext']) {
                case 'js':
                    $content = @file_get_contents($file);
                    break;
                case 'css':
                    $content = @file_get_contents($file);
                    $content = $rewriteCssUrls($content, $asset);
                    break;
                default:
                    continue;
            }
            // Remove references to source maps
            $content = preg_replace('~/[/|\\*]# sourceMappingURL=.*~', '', $content);
            $output[] = $content;
        }
        // Add newlines between files to fix problem with stacking comments.
        return implode("\n", $output);
    }