diff --git a/README.md b/README.md index f3d4944..0ac5c41 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,18 @@ You can use this Facade anywhere in your application | value. By default if empty HTTP_HOST would be used. | */ - 'base_url' => '' + 'base_url' => '', + + /* + |-------------------------------------------------------------------------- + | Disable URL Correction for CSS + |-------------------------------------------------------------------------- + | + | By default the ALL url('') declarations in CSS files are transformed to + | absolute addresses. Set to true to disable URL correction. + | + */ + 'disable_url_correction' => false ); ``` diff --git a/composer.json b/composer.json index d3a3ece..5559fe0 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,11 @@ } ], "require": { - "php": ">=5.4.0", + "php": ">=7.2.0", "tedivm/jshrink": "~1.0", "natxet/CssMin": "3.*", - "illuminate/filesystem": "~5.0|^6.0", - "illuminate/support": "~5.0|^6.0" + "illuminate/filesystem": "~5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", + "illuminate/support": "~5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0" }, "require-dev": { "phpspec/phpspec": "2.0.0", diff --git a/src/Minify.php b/src/Minify.php index 6cfb6c3..3ec196f 100644 --- a/src/Minify.php +++ b/src/Minify.php @@ -83,7 +83,7 @@ public function javascript($file, $attributes = array()) { * @return string */ public function stylesheet($file, $attributes = array()) { - $this->provider = new StyleSheet(public_path(), ['hash_salt' => $this->config['hash_salt'], 'disable_mtime' => $this->config['disable_mtime']]); + $this->provider = new StyleSheet(public_path(), ['hash_salt' => $this->config['hash_salt'], 'disable_mtime' => $this->config['disable_mtime'], 'disable_url_correction' => $this->config['disable_url_correction']]); $this->buildPath = $this->config['css_build_path']; $this->attributes = $attributes; $this->buildExtension = 'css'; @@ -99,7 +99,7 @@ public function stylesheet($file, $attributes = array()) { * @return string */ public function stylesheetDir($dir, $attributes = array()) { - $this->provider = new StyleSheet(public_path(), ['hash_salt' => $this->config['hash_salt'], 'disable_mtime' => $this->config['disable_mtime']]); + $this->provider = new StyleSheet(public_path(), ['hash_salt' => $this->config['hash_salt'], 'disable_mtime' => $this->config['disable_mtime'], 'disable_url_correction' => $this->config['disable_url_correction']]); $this->buildPath = $this->config['css_build_path']; $this->attributes = $attributes; $this->buildExtension = 'css'; diff --git a/src/MinifyServiceProvider.php b/src/MinifyServiceProvider.php index 7dc31c2..3dac933 100644 --- a/src/MinifyServiceProvider.php +++ b/src/MinifyServiceProvider.php @@ -53,6 +53,7 @@ protected function registerServices() { 'reverse_sort' => config('minify.config.reverse_sort'), 'disable_mtime' => config('minify.config.disable_mtime'), 'hash_salt' => config('minify.config.hash_salt'), + 'disable_url_correction' => config('minify.config.disable_url_correction'), ), $app->environment() ); diff --git a/src/Providers/BaseProvider.php b/src/Providers/BaseProvider.php index a99a2b1..5b7084b 100755 --- a/src/Providers/BaseProvider.php +++ b/src/Providers/BaseProvider.php @@ -1,4 +1,6 @@ -disable_mtime = $config['disable_mtime'] ?: false; $this->hash_salt = $config['hash_salt'] ?: ''; + $this->disable_url_correction = @$config['disable_url_correction'] ?: false; - $value = function($key) - { + $value = function ($key) { return isset($_SERVER[$key]) ? $_SERVER[$key] : ''; }; - $this->headers = array( - 'User-Agent' => $value('HTTP_USER_AGENT'), - 'Accept' => $value('HTTP_ACCEPT'), + $this->headers = [ + 'User-Agent' => $value('HTTP_USER_AGENT'), + 'Accept' => $value('HTTP_ACCEPT'), 'Accept-Language' => $value('HTTP_ACCEPT_LANGUAGE'), 'Accept-Encoding' => 'identity', - 'Connection' => 'close', - ); + 'Connection' => 'close', + ]; } /** @@ -91,8 +98,7 @@ public function make($outputDir) $this->checkDirectory(); - if ($this->checkExistingFiles()) - { + if ($this->checkExistingFiles()) { return false; } @@ -109,18 +115,15 @@ public function make($outputDir) */ public function add($file) { - if (is_array($file)) - { - foreach ($file as $value) $this->add($value); - } - else if ($this->checkExternalFile($file)) - { + if (is_array($file)) { + foreach ($file as $value) { + $this->add($value); + } + } elseif ($this->checkExternalFile($file)) { $this->files[] = $file; - } - else { + } else { $file = $this->publicPath . $file; - if (!file_exists($file)) - { + if (!file_exists($file)) { throw new FileNotExistException("File '{$file}' does not exist"); } @@ -137,8 +140,7 @@ public function add($file) public function tags($baseUrl, $attributes) { $html = ''; - foreach($this->files as $file) - { + foreach ($this->files as $file) { $file = $baseUrl . str_replace($this->publicPath, '', $file); $html .= $this->tag($file, $attributes); } @@ -160,25 +162,24 @@ public function count() protected function appendFiles() { foreach ($this->files as $file) { - if ($this->checkExternalFile($file)) - { - if (strpos($file, '//') === 0) $file = 'http:' . $file; + if ($this->checkExternalFile($file)) { + if (strpos($file, '//') === 0) { + $file = 'http:' . $file; + } $headers = $this->headers; - foreach ($headers as $key => $value) - { + foreach ($headers as $key => $value) { $headers[$key] = $key . ': ' . $value; } - $context = stream_context_create(array('http' => array( + $context = stream_context_create(['http' => [ 'ignore_errors' => true, 'header' => implode("\r\n", $headers), - ))); + ]]); - $http_response_header = array(false); + $http_response_header = [false]; $contents = file_get_contents($file, false, $context); - if (strpos($http_response_header[0], '200') === false) - { + if (strpos($http_response_header[0], '200') === false) { throw new FileNotExistException("File '{$file}' does not exist"); } } else { @@ -205,16 +206,14 @@ protected function checkExistingFiles() */ protected function checkDirectory() { - if (!file_exists($this->outputDir)) - { - // Try to create the directory - if (!$this->file->makeDirectory($this->outputDir, 0775, true)) { - throw new DirNotExistException("Buildpath '{$this->outputDir}' does not exist"); - } + if (!file_exists($this->outputDir)) { + // Try to create the directory + if (!$this->file->makeDirectory($this->outputDir, 0775, true)) { + throw new DirNotExistException("Buildpath '{$this->outputDir}' does not exist"); + } } - if (!is_writable($this->outputDir)) - { + if (!is_writable($this->outputDir)) { throw new DirNotWritableException("Buildpath '{$this->outputDir}' is not writable"); } } @@ -244,15 +243,16 @@ protected function buildMinifiedFilename() */ protected function attributes($attributes) { - $html = array(); - foreach ((array) $attributes as $key => $value) - { + $html = []; + foreach ((array) $attributes as $key => $value) { $element = $this->attributeElement($key, $value); - if ( ! is_null($element)) $html[] = $element; + if (!is_null($element)) { + $html[] = $element; + } } - $output = count($html) > 0 ? ' '.implode(' ', $html) : ''; + $output = count($html) > 0 ? ' ' . implode(' ', $html) : ''; return trim($output); } @@ -266,13 +266,17 @@ protected function attributes($attributes) */ protected function attributeElement($key, $value) { - if (is_numeric($key)) $key = $value; + if (is_numeric($key)) { + $key = $value; + } - if(is_bool($value)) + if (is_bool($value)) { return $key; + } - if ( ! is_null($value)) - return $key.'="'.htmlentities($value, ENT_QUOTES, 'UTF-8', false).'"'; + if (!is_null($value)) { + return $key . '="' . htmlentities($value, ENT_COMPAT, 'UTF-8', false) . '"'; + } return null; } @@ -283,7 +287,7 @@ protected function attributeElement($key, $value) protected function getHashedFilename() { $publicPath = $this->publicPath; - return md5(implode('-', array_map(function($file) use ($publicPath) { return str_replace($publicPath, '', $file); }, $this->files)) . $this->hash_salt); + return md5(implode('-', array_map(function ($file) use ($publicPath) { return str_replace($publicPath, '', $file); }, $this->files)) . $this->hash_salt); } /** @@ -293,14 +297,11 @@ protected function countModificationTime() { $time = 0; - foreach ($this->files as $file) - { - if ($this->checkExternalFile($file)) - { + foreach ($this->files as $file) { + if ($this->checkExternalFile($file)) { $userAgent = isset($this->headers['User-Agent']) ? $this->headers['User-Agent'] : ''; $time += hexdec(substr(md5($file . $userAgent), 0, 8)); - } - else { + } else { $time += filemtime($file); } } @@ -316,11 +317,9 @@ protected function removeOldFiles() $pattern = $this->outputDir . $this->getHashedFilename() . '*'; $find = glob($pattern); - if( is_array($find) && count($find) ) - { - foreach ($find as $file) - { - if ( ! unlink($file) ) { + if (is_array($find) && count($find)) { + foreach ($find as $file) { + if (!unlink($file)) { throw new CannotRemoveFileException("File '{$file}' cannot be removed"); } } @@ -334,8 +333,7 @@ protected function removeOldFiles() */ protected function put($minified) { - if(file_put_contents($this->outputDir . $this->filename, $minified) === false) - { + if (file_put_contents($this->outputDir . $this->filename, $minified) === false) { throw new CannotSaveFileException("File '{$this->outputDir}{$this->filename}' cannot be saved"); } diff --git a/src/Providers/JavaScript.php b/src/Providers/JavaScript.php index c90f51c..c5033df 100644 --- a/src/Providers/JavaScript.php +++ b/src/Providers/JavaScript.php @@ -1,4 +1,6 @@ - $file) + $attributes; + // dd($attributes); + // $attributes = array('src' => $file) + $attributes; + $attributes = array_merge(['src' => $file], $attributes); return "" . PHP_EOL; } diff --git a/src/Providers/StyleSheet.php b/src/Providers/StyleSheet.php index 91a4366..5c12953 100644 --- a/src/Providers/StyleSheet.php +++ b/src/Providers/StyleSheet.php @@ -27,57 +27,64 @@ public function minify() * @param array $attributes * @return string */ - public function tag($file, array $attributes = array()) + public function tag($file, array $attributes = []) { - $attributes = array('href' => $file, 'rel' => 'stylesheet') + $attributes; + $attributes = array_merge(['href' => $file, 'rel' => 'stylesheet'], $attributes); + // $attributes = array('href' => $file, 'rel' => 'stylesheet') + $attributes; - return "attributes($attributes)}>".PHP_EOL; + return "attributes($attributes)}>" . PHP_EOL; } /** * Override appendFiles to solve css url path issue - * + * * @throws \Devfactory\Minify\Exceptions\FileNotExistException */ protected function appendFiles() { foreach ($this->files as $file) { if ($this->checkExternalFile($file)) { - if (strpos($file, '//') === 0) $file = 'http:'.$file; + if (strpos($file, '//') === 0) { + $file = 'http:' . $file; + } $headers = $this->headers; foreach ($headers as $key => $value) { - $headers[$key] = $key.': '.$value; + $headers[$key] = $key . ': ' . $value; } - $context = stream_context_create(array('http' => array( - 'ignore_errors' => true, - 'header' => implode("\r\n", $headers), - ))); - - $http_response_header = array(false); + $context = stream_context_create(['http' => [ + 'ignore_errors' => true, + 'header' => implode("\r\n", $headers), + ]]); + $http_response_header = [false]; if (strpos($http_response_header[0], '200') === false) { throw new FileNotExistException("File '{$file}' does not exist"); } } $contents = $this->urlCorrection($file); - $this->appended .= $contents."\n"; + $this->appended .= $contents . "\n"; } } /** * Css url path correction - * + * * @param string $file * @return string */ public function urlCorrection($file) { - $folder = str_replace(public_path(), '', $file); - $folder = str_replace(basename($folder), '', $folder); - $content = file_get_contents($file); - $contentReplace = []; + $folder = str_replace(public_path(), '', $file); + $folder = str_replace(basename($folder), '', $folder); + $content = file_get_contents($file); + + if ($this->disable_url_correction) { + return $content; + } + + $contentReplace = []; $contentReplaceWith = []; preg_match_all('/url\(([\s])?([\"|\'])?(.*?)([\"|\'])?([\s])?\)/i', $content, $matches, PREG_PATTERN_ORDER); if (!count($matches)) { @@ -85,14 +92,14 @@ public function urlCorrection($file) } foreach ($matches[0] as $match) { if (strpos($match, "'") != false) { - $contentReplace[] = $match; - $contentReplaceWith[] = str_replace('url(\'', 'url(\''.$folder, $match); + $contentReplace[] = $match; + $contentReplaceWith[] = str_replace('url(\'', 'url(\'' . $folder, $match); } elseif (strpos($match, '"') !== false) { - $contentReplace[] = $match; - $contentReplaceWith[] = str_replace('url("', 'url("'.$folder, $match); + $contentReplace[] = $match; + $contentReplaceWith[] = str_replace('url("', 'url("' . $folder, $match); } else { - $contentReplace[] = $match; - $contentReplaceWith[] = str_replace('url(', 'url('.$folder, $match); + $contentReplace[] = $match; + $contentReplaceWith[] = str_replace('url(', 'url(' . $folder, $match); } } return str_replace($contentReplace, $contentReplaceWith, $content); diff --git a/src/config/config.php b/src/config/config.php index e7faa7f..ee36ce3 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -80,5 +80,16 @@ | value. By default if empty HTTP_HOST would be used. | */ - 'base_url' => '' + 'base_url' => '', + + /* + |-------------------------------------------------------------------------- + | Disable URL Correction for CSS + |-------------------------------------------------------------------------- + | + | By default the ALL url('') declarations in CSS files are transformed to + | absolute addresses. Set to true to disable URL correction. + | + */ + 'disable_url_correction' => false );