diff --git a/src/Config/Minifier.php b/src/Config/Minifier.php index 52caef3..d680a08 100644 --- a/src/Config/Minifier.php +++ b/src/Config/Minifier.php @@ -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: @@ -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: @@ -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'; diff --git a/src/Minifier.php b/src/Minifier.php index 7677913..360cb99 100644 --- a/src/Minifier.php +++ b/src/Minifier.php @@ -1,6 +1,7 @@ config = $config; @@ -75,7 +76,7 @@ public function load(string $filename) if ($this->config->autoDeployOnChange) { - $this->autoDeployCheck($filename, $ext); + $this->autoDeployCheckFile($ext, $filename); } // load versions @@ -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); @@ -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); } //-------------------------------------------------------------------- @@ -196,35 +191,29 @@ 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); } //-------------------------------------------------------------------- @@ -232,19 +221,23 @@ protected function autoDeployCheckJs(string $filename): bool /** * 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; } @@ -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; } } @@ -297,7 +290,6 @@ protected function determineUrl(string $ext): string } return $dir; - } //-------------------------------------------------------------------- @@ -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, '/'); @@ -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 = []; @@ -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