Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Oct 21, 2021
1 parent 6f21954 commit c756bbb
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 48 deletions.
5 changes: 5 additions & 0 deletions src/Exceptions/MinifierException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public static function forWrongReturnType(string $type)
{
return new self(lang('Minifier.wrongReturnType', [$type]));
}

public static function forFileCopyError(string $file1, string $file2)
{
return new self(lang('Minifier.fileCopyError'. [$file1, $file2]));
}
}
1 change: 1 addition & 0 deletions src/Language/en/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
'noVersioningFile' => 'There is no file with versioning. Run "php spark minify:all" command first.',
'incorrectDeploymentMode' => 'The "{0}" is not correct deployment mode.',
'wrongReturnType' => 'The "{0}" is not correct return type.',
'fileCopyError' => 'The "{0}" file cannot be copied to "{1}".',
];
67 changes: 30 additions & 37 deletions src/Minifier.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Michalsn\Minifier;

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

class Minifier
Expand Down Expand Up @@ -414,11 +415,10 @@ protected function deployJs(array $assets, string $dir, string $minDir = null):
if ($minDir === null)
{
$minDir = $dir;
}else{
if ($dir !== $minDir)
{
$this->emptyFolder($minDir);
}
}
elseif ($dir !== $minDir)
{
$this->emptyFolder($minDir);
}

$class = $this->config->adapterJs;
Expand All @@ -433,14 +433,11 @@ protected function deployJs(array $assets, string $dir, string $minDir = null):
if ($this->config->minify)
{
$miniJs->add($dir . DIRECTORY_SEPARATOR . $file);

}else{

if ($dir !== $minDir)
{
$this->copyFile($dir . DIRECTORY_SEPARATOR . $file, $minDir . DIRECTORY_SEPARATOR . $file);
$results[$file] = md5_file($minDir . DIRECTORY_SEPARATOR . $file);
}
}
elseif ($dir !== $minDir)
{
$this->copyFile($dir . DIRECTORY_SEPARATOR . $file, $minDir . DIRECTORY_SEPARATOR . $file);
$results[$file] = md5_file($minDir . DIRECTORY_SEPARATOR . $file);
}
}

Expand Down Expand Up @@ -471,11 +468,10 @@ protected function deployCss(array $assets, string $dir, string $minDir): array
if ($minDir === null)
{
$minDir = $dir;
}else{
if ($dir !== $minDir)
{
$this->emptyFolder($minDir);
}
}
elseif ($dir !== $minDir)
{
$this->emptyFolder($minDir);
}

$class = $this->config->adapterCss;
Expand All @@ -490,13 +486,11 @@ protected function deployCss(array $assets, string $dir, string $minDir): array
if ($this->config->minify)
{
$miniCss->add($dir . DIRECTORY_SEPARATOR . $file);
}else{

if ($dir !== $minDir)
{
$this->copyFile($dir . DIRECTORY_SEPARATOR . $file, $minDir . DIRECTORY_SEPARATOR . $file);
$results[$file] = md5_file($minDir . DIRECTORY_SEPARATOR . $file);
}
}
elseif ($dir !== $minDir)
{
$this->copyFile($dir . DIRECTORY_SEPARATOR . $file, $minDir . DIRECTORY_SEPARATOR . $file);
$results[$file] = md5_file($minDir . DIRECTORY_SEPARATOR . $file);
}
}

Expand All @@ -515,23 +509,23 @@ protected function deployCss(array $assets, string $dir, string $minDir): array
/**
* Copy File
*
* @param string $dir Directory
* @param string $minDir Directory
* @param string $dir Directory
* @param string $minDir Minified directory
*
* @return void
*/
protected function copyFile(string $dir, string $minDir) : void
protected function copyFile(string $dir, string $minDir): void
{
$path = pathinfo($minDir);

if (! file_exists($path['dirname']))
{
mkdir($path['dirname'], 0777, true);
mkdir($path['dirname'], 0755, true);
}

if (! copy($dir, $minDir))
{
throw MinifierException::forNoVersioningFile();
throw MinifierException::forFileCopyError($dir, $minDir);
}
}

Expand All @@ -540,21 +534,20 @@ protected function copyFile(string $dir, string $minDir) : void
/**
* Copy File
*
* @param string $dir Directory
* @param string $minDir Directory
* @param string $dir Directory
*
* @return void
*/
protected function emptyFolder(string $dir) : void
protected function emptyFolder(string $dir): void
{
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
foreach ($files as $fileInfo) {
$todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileInfo->getRealPath());
}
}
}
Empty file.
Empty file.
120 changes: 109 additions & 11 deletions tests/unit/MinifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,7 @@ public function setUp(): void
{
unlink($this->config->dirCss . '/new.css');
}

/*
if (file_exists($this->config->dirJs . '/all.min.js'))
{
unlink($this->config->dirJs . '/all.min.js');
}
if (file_exists($this->config->dirCss . '/all.min.css'))
{
unlink($this->config->dirCss . '/all.min.css');
}
if (file_exists($this->config->dirVersion . '/versions.js'))
{
unlink($this->config->dirVersion . '/versions.js');
Expand Down Expand Up @@ -110,6 +99,8 @@ public function testDeployJs()
$result = $this->minifier->deploy('js');

$this->assertTrue($result);

$this->assertTrue(file_exists($this->config->dirJs . DIRECTORY_SEPARATOR . array_key_first($this->config->js)));
}

public function testDeployCss()
Expand All @@ -119,6 +110,8 @@ public function testDeployCss()
$result = $this->minifier->deploy('css');

$this->assertTrue($result);

$this->assertTrue(file_exists($this->config->dirCss . DIRECTORY_SEPARATOR . array_key_first($this->config->css)));
}

public function testDeployAll()
Expand All @@ -128,8 +121,13 @@ public function testDeployAll()
$result = $this->minifier->deploy('all');

$this->assertTrue($result);

$this->assertTrue(file_exists($this->config->dirJs . DIRECTORY_SEPARATOR . array_key_first($this->config->js)));
$this->assertTrue(file_exists($this->config->dirCss . DIRECTORY_SEPARATOR . array_key_first($this->config->css)));
}



public function testLoadJs()
{
$this->minifier = new Minifier($this->config);
Expand All @@ -146,7 +144,26 @@ public function testLoadCss()
$result = $this->minifier->load('all.min.css');

$this->assertEquals('<link rel="stylesheet" href="http://localhost' . SUPPORTPATH . 'assets/css/all.min.css?v=' . $this->ver['css'] . '">' . PHP_EOL, $result);
}

public function testLoadJsWithDirMinJs()
{
$this->config->dirMinJs = SUPPORTPATH . 'public/js';
$this->minifier = new Minifier($this->config);

$result = $this->minifier->load('all.min.js');

$this->assertEquals('<script type="text/javascript" src="http://localhost' . SUPPORTPATH . 'public/js/all.min.js?v=' . $this->ver['js'] . '"></script>' . PHP_EOL, $result);
}

public function testLoadCssWithDirMinCss()
{
$this->config->dirMinCss = SUPPORTPATH . 'public/css';
$this->minifier = new Minifier($this->config);

$result = $this->minifier->load('all.min.css');

$this->assertEquals('<link rel="stylesheet" href="http://localhost' . SUPPORTPATH . 'public/css/all.min.css?v=' . $this->ver['css'] . '">' . PHP_EOL, $result);
}

public function testLoadJsWithBaseJsUrl()
Expand All @@ -171,6 +188,30 @@ public function testLoadCssWithBaseCssUrl()
$this->assertEquals('<link rel="stylesheet" href="http://css.localhost/all.min.css?v=' . $this->ver['css'] . '">' . PHP_EOL, $result);
}

public function testLoadJsWithBaseJsUrlAndDirMinJs()
{
$this->config->baseJsUrl = 'http://js.localhost/';
$this->config->dirMinJs = SUPPORTPATH . 'public/js';

$this->minifier = new Minifier($this->config);

$result = $this->minifier->load('all.min.js');

$this->assertEquals('<script type="text/javascript" src="http://js.localhost/all.min.js?v=' . $this->ver['js'] . '"></script>' . PHP_EOL, $result);
}

public function testLoadCssWithBaseCssUrlAndDirMinCss()
{
$this->config->baseCssUrl = 'http://css.localhost/';
$this->config->dirMinCss = SUPPORTPATH . 'public/css';

$this->minifier = new Minifier($this->config);

$result = $this->minifier->load('all.min.css');

$this->assertEquals('<link rel="stylesheet" href="http://css.localhost/all.min.css?v=' . $this->ver['css'] . '">' . PHP_EOL, $result);
}

public function testJsonReturnTypeWithLoadJs()
{
$this->config->returnType = 'json';
Expand Down Expand Up @@ -272,4 +313,61 @@ public function testAutoDeployOnChangeCssTrue()
$this->assertTrue($method('all.min.css'));
}

public function testDeployJsWithDirMinJs()
{
if (file_exists($this->config->dirMinJs . '/all.min.js'))
{
unlink($this->config->dirMinJs . '/all.min.js');
}

$this->config->dirMinJs = SUPPORTPATH . 'public/js';
$this->minifier = new Minifier($this->config);

$result = $this->minifier->deploy('js');

$this->assertTrue($result);

$this->assertTrue(file_exists($this->config->dirMinJs . DIRECTORY_SEPARATOR . array_key_first($this->config->js)));
}

public function testDeployCssWithDirMinCss()
{
if (file_exists($this->config->dirMinCss . '/all.min.css'))
{
unlink($this->config->dirMinCss . '/all.min.css');
}

$this->config->dirMinCss = SUPPORTPATH . 'public/css';
$this->minifier = new Minifier($this->config);

$result = $this->minifier->deploy('css');

$this->assertTrue($result);

$this->assertTrue(file_exists($this->config->dirMinCss . DIRECTORY_SEPARATOR . array_key_first($this->config->css)));
}

public function testDeployAllWithDirMinJsAndCss()
{
if (file_exists($this->config->dirMinJs . '/all.min.js'))
{
unlink($this->config->dirMinJs . '/all.min.js');
}

if (file_exists($this->config->dirMinCss . '/all.min.css'))
{
unlink($this->config->dirMinCss . '/all.min.css');
}

$this->config->dirMinJs = SUPPORTPATH . 'public/js';
$this->config->dirMinCss = SUPPORTPATH . 'public/css';
$this->minifier = new Minifier($this->config);

$result = $this->minifier->deploy('all');

$this->assertTrue($result);

$this->assertTrue(file_exists($this->config->dirMinJs . DIRECTORY_SEPARATOR . array_key_first($this->config->js)));
$this->assertTrue(file_exists($this->config->dirMinCss . DIRECTORY_SEPARATOR . array_key_first($this->config->css)));
}
}

0 comments on commit c756bbb

Please sign in to comment.