Skip to content

Commit

Permalink
Merge pull request #69 from mrothauer/master
Browse files Browse the repository at this point in the history
add filter for CleanCss
  • Loading branch information
markstory authored Aug 25, 2022
2 parents 0542e03 + 4f3a287 commit 5b3f7d7
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Filter/CleanCss.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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;

/**
* CleanCssFilter.
*
* Allows you to filter Css files through CleanCss. You need to install CleanCssCli with composer.
*/
class CleanCss extends AssetFilter
{

/**
* Settings for CleanCss
*
* - `node` Path to nodejs on your machine
* - `node_path` The path to the node_modules directory where cleancss is installed.
*/
protected $_settings = array(
'node' => '/usr/local/bin/node',
'cleancss' => '/usr/local/bin/clean-css-cli/bin/cleancss',
'node_path' => '/usr/local/lib/node_modules',
'options' => '',
);

/**
* Run `cleancss` against the output and compress it.
*
* @param string $target Name of the file being generated.
* @param string $content The uncompressed contents for $filename.
* @return string Compressed contents.
*/
public function output($target, $content)
{
$tmpFile = tempnam(sys_get_temp_dir(), 'CLEANCSS');
file_put_contents($tmpFile, $content);
$cmd = $this->_settings['node'] . ' ' .
$this->_settings['cleancss'] . $this->_settings['options'] . ' ' .
escapeshellarg($tmpFile);
$env = array('NODE_PATH' => $this->_settings['node_path']);
$result = $this->_runCmd($cmd, '', $env);
unlink($tmpFile);
return $result;
}
}

0 comments on commit 5b3f7d7

Please sign in to comment.