Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

);
```
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/Minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
1 change: 1 addition & 0 deletions src/MinifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down
128 changes: 63 additions & 65 deletions src/Providers/BaseProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Devfactory\Minify\Providers;
<?php

namespace Devfactory\Minify\Providers;

use Devfactory\Minify\Exceptions\CannotRemoveFileException;
use Devfactory\Minify\Exceptions\CannotSaveFileException;
Expand Down Expand Up @@ -28,12 +30,12 @@ abstract class BaseProvider implements Countable
/**
* @var array
*/
protected $files = array();
protected $files = [];

/**
* @var array
*/
protected $headers = array();
protected $headers = [];

/**
* @var string
Expand All @@ -50,6 +52,11 @@ abstract class BaseProvider implements Countable
*/
private $disable_mtime;

/**
* @var boolean
*/
protected $disable_url_correction;

/**
* @var string
*/
Expand All @@ -66,19 +73,19 @@ public function __construct($publicPath = null, $config = null, Filesystem $file

$this->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',
];
}

/**
Expand All @@ -91,8 +98,7 @@ public function make($outputDir)

$this->checkDirectory();

if ($this->checkExistingFiles())
{
if ($this->checkExistingFiles()) {
return false;
}

Expand All @@ -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");
}

Expand All @@ -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);
}
Expand All @@ -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 {
Expand All @@ -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");
}
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}
Expand All @@ -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");
}
}
Expand All @@ -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");
}

Expand Down
8 changes: 6 additions & 2 deletions src/Providers/JavaScript.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Devfactory\Minify\Providers;
<?php

namespace Devfactory\Minify\Providers;

use Devfactory\Minify\Contracts\MinifyInterface;
use JShrink\Minifier;
Expand Down Expand Up @@ -27,7 +29,9 @@ public function minify()
*/
public function tag($file, array $attributes)
{
$attributes = array('src' => $file) + $attributes;
// dd($attributes);
// $attributes = array('src' => $file) + $attributes;
$attributes = array_merge(['src' => $file], $attributes);

return "<script {$this->attributes($attributes)}></script>" . PHP_EOL;
}
Expand Down
Loading