Skip to content

Commit

Permalink
started CssCrush filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kriswallsmith committed Jul 18, 2011
1 parent 3333d37 commit 3a059bc
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Expand Up @@ -26,6 +26,7 @@
<!-- <server name="CSSMIN" value="/path/to/cssmin/source/CssMin.php" /> -->
<!-- <server name="CSSEMBED_JAR" value="/path/to/cssembed.jar" /> -->
<!-- <server name="PACKAGER" value="/path/to/packager.php" /> -->
<!-- <server name="CSSCRUSH" value="/path/to/CssCrush.php" /> -->
</php>

<filter>
Expand Down
83 changes: 83 additions & 0 deletions src/Assetic/Filter/CssCrushFilter.php
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the Assetic package, an OpenSky project.
*
* (c) 2010-2011 OpenSky Project Inc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Assetic\Filter;

use Assetic\Asset\AssetInterface;
use Assetic\Filter\FilterInterface;

/**
* Loads CssCrush files.
*
* @link http://the-echoplex.net/csscrush/
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
*
* @todo import directives do not work
*/
class CssCrushFilter implements FilterInterface
{
private $debug = false;
private $boilerplate;
private $versioning = false;

public function setDebug($debug)
{
$this->debug = $debug;
}

public function setBoilerplate($boilerplate)
{
$this->boilerplate = $boilerplate;
}

public function setVersioning($versioning)
{
$this->versioning = $versioning;
}

public function filterLoad(AssetInterface $asset)
{
$options = array();

if (null !== $this->debug) {
$options['debug'] = (Boolean) $this->debug;
}

if (null !== $this->boilerplate) {
$options['boilerplate'] = (Boolean) $this->boilerplate;
}

if (null !== $this->versioning) {
$options['versioning'] = (Boolean) $this->versioning;
}

// remember the previous document root
$snapshot = \CssCrush::$config->docRoot;

// setup the input
$input = tempnam(sys_get_temp_dir(), 'assetic_csscrush');
file_put_contents($input, $asset->getContent());

// process the asset
\CssCrush::$config->docRoot = dirname($input);
$output = \CssCrush::file('/'.basename($input), $options);
$asset->setContent(file_get_contents(\CssCrush::$config->docRoot.$output));

// cleanup
unlink($input);
unlink(\CssCrush::$config->docRoot.$output);
\CssCrush::$config->docRoot = $snapshot;
}

public function filterDump(AssetInterface $asset)
{
}
}
39 changes: 39 additions & 0 deletions tests/Assetic/Test/Filter/CssCrushFilterTest.php
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Assetic package, an OpenSky project.
*
* (c) 2010-2011 OpenSky Project Inc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Assetic\Test\Filter;

use Assetic\Asset\FileAsset;
use Assetic\Filter\CssCrushFilter;

/**
* @group integration
*/
class CssCrushFilterTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!class_exists('CssCrush', false)) {
$this->markTestSkipped('CssCrush is not available.');
}
}

public function testLoad()
{
$asset = new FileAsset(__DIR__.'/fixtures/csscrush/main.css', array(), __DIR__.'/fixtures/csscrush', 'main.css');
$asset->load();

$filter = new CssCrushFilter();
$filter->filterLoad($asset);

$this->assertContains('strong { font-weight: bold; }', $asset->getContent(), '->filterLoad() calls CssCrush');
}
}
2 changes: 2 additions & 0 deletions tests/Assetic/Test/Filter/fixtures/csscrush/main.css
@@ -0,0 +1,2 @@
@import url("more.css");
body { color: red; }
1 change: 1 addition & 0 deletions tests/Assetic/Test/Filter/fixtures/csscrush/more.css
@@ -0,0 +1 @@
strong { font-weight: bold; }
4 changes: 4 additions & 0 deletions tests/bootstrap.php
Expand Up @@ -43,3 +43,7 @@
if (isset($_SERVER['PACKAGER'])) {
require_once $_SERVER['PACKAGER'];
}

if (isset($_SERVER['CSSCRUSH'])) {
require_once $_SERVER['CSSCRUSH'];
}

0 comments on commit 3a059bc

Please sign in to comment.