diff --git a/src/Exceptions/MinifierException.php b/src/Exceptions/MinifierException.php
index 5b4d231..3466d84 100644
--- a/src/Exceptions/MinifierException.php
+++ b/src/Exceptions/MinifierException.php
@@ -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]));
+ }
}
diff --git a/src/Language/en/Minifier.php b/src/Language/en/Minifier.php
index 83446cc..dee1ded 100644
--- a/src/Language/en/Minifier.php
+++ b/src/Language/en/Minifier.php
@@ -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}".',
];
diff --git a/src/Minifier.php b/src/Minifier.php
index 6c2b045..7677913 100644
--- a/src/Minifier.php
+++ b/src/Minifier.php
@@ -1,5 +1,6 @@
emptyFolder($minDir);
- }
+ }
+ elseif ($dir !== $minDir)
+ {
+ $this->emptyFolder($minDir);
}
$class = $this->config->adapterJs;
@@ -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);
}
}
@@ -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;
@@ -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);
}
}
@@ -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);
}
}
@@ -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());
}
}
}
diff --git a/tests/_support/public/css/.gitikeep b/tests/_support/public/css/.gitikeep
new file mode 100644
index 0000000..e69de29
diff --git a/tests/_support/public/js/.gitkeep b/tests/_support/public/js/.gitkeep
new file mode 100644
index 0000000..e69de29
diff --git a/tests/unit/MinifierTest.php b/tests/unit/MinifierTest.php
index 38eec96..e13f760 100644
--- a/tests/unit/MinifierTest.php
+++ b/tests/unit/MinifierTest.php
@@ -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');
@@ -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()
@@ -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()
@@ -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);
@@ -146,7 +144,26 @@ public function testLoadCss()
$result = $this->minifier->load('all.min.css');
$this->assertEquals('' . 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('' . 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('' . PHP_EOL, $result);
}
public function testLoadJsWithBaseJsUrl()
@@ -171,6 +188,30 @@ public function testLoadCssWithBaseCssUrl()
$this->assertEquals('' . 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('' . 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('' . PHP_EOL, $result);
+ }
+
public function testJsonReturnTypeWithLoadJs()
{
$this->config->returnType = 'json';
@@ -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)));
+ }
}