Skip to content

Commit

Permalink
refactor minifier class
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Nov 21, 2021
1 parent 1d5357e commit 1000119
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 99 deletions.
6 changes: 3 additions & 3 deletions src/Config/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Minifier extends BaseConfig
// Base JS URL for assets
//--------------------------------------------------------------------
// Use this variable when your JS assets are served from subdomain.
// Bare in mind that in this case variable $dirJs won't be added
// Bear in mind that in this case variable $dirJs won't be added
// to the URL.
//
// Example value:
Expand All @@ -41,7 +41,7 @@ class Minifier extends BaseConfig
// Base CSS URL for assets
//--------------------------------------------------------------------
// Use this variable when your CSS assets are served from subdomain.
// Bare in mind that in this case variable $dirCSS won't be added
// Bear in mind that in this case variable $dirCSS won't be added
// to the URL.
//
// Example value:
Expand Down Expand Up @@ -110,7 +110,7 @@ class Minifier extends BaseConfig
// 'html' and it uses the $tagJs and $tagCss variables. Using 'array'
// will return the php array and 'json' type will return a json string.
//
// Avaliable types:
// Available types:
// 'html', 'array', 'json'

public $returnType = 'html';
Expand Down
173 changes: 77 additions & 96 deletions src/Minifier.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php namespace Michalsn\Minifier;

use Exception;
use Michalsn\Minifier\Config\Minifier as Config;
use Michalsn\Minifier\Exceptions\MinifierException;

class Minifier
{
/**
* Config object.
*
* @var \Config\Minify
* @var Config
*/
protected $config;

Expand All @@ -24,9 +25,9 @@ class Minifier
/**
* Prepare config to use
*
* @param \Config\Minify $config
* @param Config $config
*/
public function __construct($config)
public function __construct(Config $config)
{
$this->config = $config;

Expand Down Expand Up @@ -75,7 +76,7 @@ public function load(string $filename)

if ($this->config->autoDeployOnChange)
{
$this->autoDeployCheck($filename, $ext);
$this->autoDeployCheckFile($ext, $filename);
}

// load versions
Expand Down Expand Up @@ -131,14 +132,14 @@ public function deploy(string $mode = 'all'): bool
switch ($mode)
{
case 'js':
$files = $this->deployJs($this->config->js, $this->config->dirJs, $this->config->dirMinJs);
$files = $this->deployFiles('js', $this->config->js, $this->config->dirJs, $this->config->dirMinJs);
break;
case 'css':
$files = $this->deployCss($this->config->css, $this->config->dirCss, $this->config->dirMinCss);
$files = $this->deployFiles('css', $this->config->css, $this->config->dirCss, $this->config->dirMinCss);
break;
default:
$files['js'] = $this->deployJs($this->config->js, $this->config->dirJs, $this->config->dirMinJs);
$files['css'] = $this->deployCss($this->config->css, $this->config->dirCss, $this->config->dirMinCss);
$files['js'] = $this->deployFiles('js', $this->config->js, $this->config->dirJs, $this->config->dirMinJs);
$files['css'] = $this->deployFiles('css', $this->config->css, $this->config->dirCss, $this->config->dirMinCss);
}

$this->setVersion($mode, $files, $this->config->dirVersion);
Expand Down Expand Up @@ -174,19 +175,13 @@ public function getError(): string
* @param string $filename Filename
* @param string $ext File extension
*
* @deprecated deprecated since version 1.4.0 - use autoDeployCheckFile() instead
*
* @return void
*/
protected function autoDeployCheck(string $filename, string $ext): void
{
switch ($ext)
{
case 'js':
$this->autoDeployCheckJs($filename);
break;
case 'css':
$this->autoDeployCheckCss($filename);
break;
}
$this->autoDeployCheckFile($ext, $filename);
}

//--------------------------------------------------------------------
Expand All @@ -196,55 +191,53 @@ protected function autoDeployCheck(string $filename, string $ext): void
*
* @param string $filename Filename
*
* @deprecated deprecated since version 1.4.0 - use autoDeployCheckFile() instead
*
* @return bool
*/
protected function autoDeployCheckJs(string $filename): bool
{
$assets = [$filename => $this->config->js[$filename]];
$filePath = $this->config->dirJs . '/' . $filename;

// if file is not deployed
if (! file_exists($filePath))
{
$this->deployJs($assets, $this->config->dirJs, $this->config->dirMinJs);
return true;
}

// get last deploy time
$lastDeployTime = filemtime($filePath);
return $this->autoDeployCheckFile('js', $filename);
}

// loop though the files and check last update time
foreach ($assets[$filename] as $file)
{
$currentFileTime = filemtime($this->config->dirJs . '/' . $file);
if ($currentFileTime > $lastDeployTime)
{
$this->deployJs($assets, $this->config->dirJs, $this->config->dirMinJs);
return true;
}
}
//--------------------------------------------------------------------

return false;
/**
* Auto deploy check for CSS files
*
* @param string $filename Filename
*
* @deprecated deprecated since version 1.4.0 - use autoDeployCheckFile() instead
*
* @return bool
*/
protected function autoDeployCheckCss(string $filename): bool
{
return $this->autoDeployCheckFile('css', $filename);
}

//--------------------------------------------------------------------

/**
* Auto deploy check for CSS files
*
* @param string $fileType File type [css, js]
* @param string $filename Filename
*
* @return bool
*/
protected function autoDeployCheckCss(string $filename): bool
protected function autoDeployCheckFile(string $fileType, string $filename): bool
{
$assets = [$filename => $this->config->css[$filename]];
$filePath = $this->config->dirCss . '/' . $filename;
$dir = 'dir' . ucfirst(strtolower($fileType));
$dirMin = 'dirMin' . ucfirst(strtolower($fileType));

$assets = [$filename => $this->config->$fileType[$filename]];
$filePath = $this->config->$dir . '/' . $filename;

// if file is not deployed
if (! file_exists($filePath))
{
$this->deployCss($assets, $this->config->dirCss, $this->config->dirMinCss);
$this->deployFiles($fileType, $assets, $this->config->$dir, $this->config->$dirMin);
return true;
}

Expand All @@ -254,10 +247,10 @@ protected function autoDeployCheckCss(string $filename): bool
// loop though the files and check last update time
foreach ($assets[$filename] as $file)
{
$currentFileTime = filemtime($this->config->dirCss . '/' . $file);
$currentFileTime = filemtime($this->config->$dir . '/' . $file);
if ($currentFileTime > $lastDeployTime)
{
$this->deployCss($assets, $this->config->dirCss, $this->config->dirMinCss);
$this->deployFiles($fileType, $assets, $this->config->$dir, $this->config->$dirMin);
return true;
}
}
Expand Down Expand Up @@ -297,7 +290,6 @@ protected function determineUrl(string $ext): string
}

return $dir;

}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -375,9 +367,13 @@ protected function getVersion(string $dir): array
/**
* Set version
*
* @param string $mode Mode
* @param array $files Files
* @param string $dir Directory
*
* @return void
*/
protected function setVersion($mode, $files, $dir): void
protected function setVersion(string $mode, array $files, string $dir): void
{
$dir = rtrim($dir, '/');

Expand All @@ -403,78 +399,61 @@ protected function setVersion($mode, $files, $dir): void
/**
* Deploy JS
*
* @param array $assets JS assets
* @param string $dir Directory
* @param array $assets JS assets
* @param string $dir Directory
* @param string|null $minDir Minified directory
*
* @deprecated deprecated since version 1.4.0 - use deployFiles() instead
*
* @return array
*/
protected function deployJs(array $assets, string $dir, string $minDir = null): array
{
$dir = rtrim($dir, '/');

if ($minDir === null)
{
$minDir = $dir;
}
elseif ($dir !== $minDir)
{
$this->emptyFolder($minDir);
}

$class = $this->config->adapterJs;

$results = [];

foreach ($assets as $asset => $files)
{
$miniJs = new $class();
foreach ($files as $file)
{
if ($this->config->minify)
{
$miniJs->add($dir . DIRECTORY_SEPARATOR . $file);
}
elseif ($dir !== $minDir)
{
$this->copyFile($dir . DIRECTORY_SEPARATOR . $file, $minDir . DIRECTORY_SEPARATOR . $file);
$results[$file] = md5_file($minDir . DIRECTORY_SEPARATOR . $file);
}
}
return $this->deployFiles('js', $assets, $dir, $minDir);
}

if ($this->config->minify)
{
$miniJs->minify($minDir . DIRECTORY_SEPARATOR . $asset);
$results[$asset] = md5_file($minDir . DIRECTORY_SEPARATOR . $asset);
}
}
//--------------------------------------------------------------------

return $results;
/**
* Deploy CSS
*
* @param array $assets CSS assets
* @param string $dir Directory
* @param string|null $minDir Minified directory
*
* @deprecated deprecated since version 1.4.0 - use deployFiles() instead
*
* @return array
*/
protected function deployCss(array $assets, string $dir, string $minDir = null): array
{
return $this->deployFiles('css', $assets, $dir, $minDir);
}

//--------------------------------------------------------------------

/**
* Deploy CSS
* Deploy files
*
* @param array $assets CSS assets
* @param string $dir Directory
* @param string $fileType File type [css, js]
* @param array $assets CSS assets
* @param string $dir Directory
* @param string|null $minDir Minified directory
*
* @return array
*/
protected function deployCss(array $assets, string $dir, string $minDir): array
protected function deployFiles(string $fileType, array $assets, string $dir, string $minDir = null): array
{
$adapterType = 'adapter' . ucfirst(strtolower($fileType));

$dir = rtrim($dir, '/');

if ($minDir === null)
{
$minDir = $dir;
}
elseif ($dir !== $minDir)
{
$this->emptyFolder($minDir);
}

$class = $this->config->adapterCss;
$class = $this->config->$adapterType;

$results = [];

Expand Down Expand Up @@ -536,6 +515,8 @@ protected function copyFile(string $dir, string $minDir): void
*
* @param string $dir Directory
*
* @deprecated deprecated since version 1.4.0
*
* @return void
*/
protected function emptyFolder(string $dir): void
Expand Down

0 comments on commit 1000119

Please sign in to comment.