-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from berarma/pipefilters
Add generic pipe filters
- Loading branch information
Showing
7 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* MiniAsset | ||
* Copyright (c) Mark Story (http://mark-story.com) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Mark Story (http://mark-story.com) | ||
* @since 0.0.1 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace MiniAsset\Filter; | ||
|
||
use MiniAsset\AssetTarget; | ||
use MiniAsset\Filter\AssetFilter; | ||
use MiniAsset\Filter\CssDependencyTrait; | ||
|
||
/** | ||
* Pre-processing filter that adds support for command pipes | ||
*/ | ||
class PipeInputFilter extends AssetFilter | ||
{ | ||
use CssDependencyTrait { | ||
getDependencies as getCssDependencies; | ||
} | ||
|
||
/** | ||
* Settings for PipeInputFilter | ||
* | ||
* - `ext` File extension used by the filter | ||
* - `dependencies` Set to true to calculate SCSS/LESS dependencies | ||
* - `optional_dependency_prefix` Filename prefix for dependencies or false | ||
* - `command` Command to run the file through | ||
* - `path` Sets PATH environment variable | ||
*/ | ||
protected $_settings = array( | ||
'ext' => '.css', | ||
'dependencies' => false, | ||
'optional_dependency_prefix' => false, | ||
'command' => '/bin/cat', | ||
'path' => '/bin', | ||
); | ||
|
||
/** | ||
* It will use prefixed files if they exist. | ||
* | ||
* @var string | ||
*/ | ||
protected $optionalDependencyPrefix = null; | ||
|
||
public function hasDependencies() | ||
{ | ||
return $this->_settings['dependencies']; | ||
} | ||
|
||
public function getDependencies(AssetTarget $file) | ||
{ | ||
if ($this->_settings['dependencies']) { | ||
$this->optionalDependencyPrefix = $this->_settings['optional_dependency_prefix']; | ||
|
||
return $this->getCssDependencies($file); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Runs command against any files that match the configured extension. | ||
* | ||
* @param string $filename The name of the input file. | ||
* @param string $input The content of the file. | ||
* @return string | ||
*/ | ||
public function input($filename, $input) | ||
{ | ||
if (substr($filename, strlen($this->_settings['ext']) * -1) !== $this->_settings['ext']) { | ||
return $input; | ||
} | ||
$filename = escapeshellarg($filename); | ||
$bin = $this->_settings['command'] . ' ' . $filename; | ||
$return = $this->_runCmd($bin, '', array('PATH' => $this->_settings['path'])); | ||
|
||
return $return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* MiniAsset | ||
* Copyright (c) Mark Story (http://mark-story.com) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Mark Story (http://mark-story.com) | ||
* @since 0.0.1 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace MiniAsset\Filter; | ||
|
||
use MiniAsset\Filter\AssetFilter; | ||
|
||
/** | ||
* Output pipe processor | ||
*/ | ||
class PipeOutputFilter extends AssetFilter | ||
{ | ||
|
||
/** | ||
* Settings for PipeOutputFilter | ||
* | ||
* - `command` Command to execute | ||
*/ | ||
protected $_settings = array( | ||
'command' => '/bin/cat', | ||
'path' => '/bin', | ||
); | ||
|
||
/** | ||
* Run command against the output and compress it. | ||
* | ||
* @param string $filename Name of the file being generated. | ||
* @param string $input The raw contents for $filename. | ||
* @return string Processed contents. | ||
*/ | ||
public function output($filename, $input) | ||
{ | ||
$cmd = $this->_settings['command']; | ||
|
||
return $this->_runCmd($cmd, $input, array('PATH' => $this->_settings['path'])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* MiniAsset | ||
* Copyright (c) Mark Story (http://mark-story.com) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Mark Story (http://mark-story.com) | ||
* @since 0.0.1 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace MiniAsset\Test\TestCase\Filter; | ||
|
||
use MiniAsset\AssetTarget; | ||
use MiniAsset\File\Local; | ||
use MiniAsset\Filter\PipeInputFilter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PipeInputFilterTest extends TestCase | ||
{ | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->_cssDir = APP . 'css' . DS; | ||
$this->filter = new PipeInputFilter(); | ||
$this->filter->settings([ | ||
'paths' => [$this->_cssDir], | ||
'ext' => '.scss', | ||
]); | ||
} | ||
|
||
public function testParsing() | ||
{ | ||
$this->filter->settings(array('command' => '/bin/cat')); | ||
|
||
$content = file_get_contents($this->_cssDir . 'test.scss'); | ||
$result = $this->filter->input($this->_cssDir . 'test.scss', $content); | ||
$expected = file_get_contents($this->_cssDir . 'test.scss'); | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function testGetDependenciesFalse() | ||
{ | ||
$this->filter->settings(array( | ||
'dependencies' => false, | ||
'optional_dependency_prefix' => false, | ||
)); | ||
|
||
$files = [ | ||
new Local($this->_cssDir . 'test.scss') | ||
]; | ||
$target = new AssetTarget('test.css', $files); | ||
$result = $this->filter->getDependencies($target); | ||
|
||
$this->assertEmpty($result); | ||
} | ||
|
||
public function testGetDependenciesTrue() | ||
{ | ||
$this->filter->settings(array( | ||
'dependencies' => true, | ||
'optional_dependency_prefix' => '_', | ||
)); | ||
|
||
$files = [ | ||
new Local($this->_cssDir . 'test.scss') | ||
]; | ||
$target = new AssetTarget('test.css', $files); | ||
$result = $this->filter->getDependencies($target); | ||
|
||
$this->assertCount(3, $result); | ||
$this->assertEquals('colors.scss', $result[0]->name()); | ||
$this->assertEquals('_utilities.scss', $result[1]->name()); | ||
$this->assertEquals('_reset.scss', $result[2]->name()); | ||
} | ||
|
||
public function testGetDependenciesMissingDependency() | ||
{ | ||
$this->filter->settings(array( | ||
'dependencies' => true, | ||
'optional_dependency_prefix' => '_', | ||
)); | ||
|
||
$files = [ | ||
new Local($this->_cssDir . 'broken.scss') | ||
]; | ||
$target = new AssetTarget('test.css', $files); | ||
$result = $this->filter->getDependencies($target); | ||
|
||
$this->assertCount(1, $result); | ||
$this->assertEquals('colors.scss', $result[0]->name()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* MiniAsset | ||
* Copyright (c) Mark Story (http://mark-story.com) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Mark Story (http://mark-story.com) | ||
* @since 0.0.1 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace MiniAsset\Test\TestCase\Filter; | ||
|
||
use MiniAsset\Filter\PipeOutputFilter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PipeOutputFilterTest extends TestCase | ||
{ | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->_cssDir = APP . 'css' . DS; | ||
$this->filter = new PipeOutputFilter(); | ||
} | ||
|
||
public function testOutput() | ||
{ | ||
$content = file_get_contents($this->_cssDir . 'unminified.css'); | ||
$result = $this->filter->output($this->_cssDir . 'unminified.css', $content); | ||
$expected = file_get_contents($this->_cssDir . 'unminified.css'); | ||
$this->assertEquals($expected, $result); | ||
} | ||
} |