Skip to content

Commit

Permalink
Support for less files
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jul 15, 2014
1 parent 3c8fabc commit 9f66578
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"packaged/config": "0.*",
"symfony/http-kernel": "2.*",
"symfony/console": "2.*",
"tedivm/jshrink": "~1.0"
"tedivm/jshrink": "~1.0",
"leafo/lessphp": "0.4.*"
},
"require-dev": {
"phpunit/phpunit": "*"
Expand Down
19 changes: 19 additions & 0 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,23 @@ public function requireCss($filename, $options = null)
);
}
}

/**
* Add a less file to the css store
*
* @param $filename
* @param $options
*/
public function requireLess($filename, $options = null)
{
$filenames = ValueAs::arr($filename);
foreach($filenames as $filename)
{
static::_addToStore(
'css',
$this->getResourceUri($filename . '.less'),
$options
);
}
}
}
1 change: 1 addition & 0 deletions src/AssetResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AssetResponse
'js' => 'Javascript',
'json' => 'Json',
'css' => 'Css',
'less' => 'Less',
'swf' => 'Flash',
'pdf' => 'Pdf',
'zip' => 'Zip',
Expand Down
21 changes: 21 additions & 0 deletions src/Assets/LessAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Packaged\Dispatch\Assets;

class LessAsset extends AbstractDispatchableAsset
{
public function getExtension()
{
return 'less';
}

public function getContentType()
{
return "text/css";
}

public function getContent()
{
$less = new \lessc;
return $less->compile(parent::getContent());
}
}
4 changes: 3 additions & 1 deletion tests/AssetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ public function testStore()
$dispatcher->handle($request);
$manager = \Packaged\Dispatch\AssetManager::assetType();
$manager->requireCss('test', ['delay' => true]);
$manager->requireLess('test');
$manager->requireJs('test');

$this->assertEquals(
[
'//www.packaged.in/res/p/8cac7/b/76d6c18/test.css' => ['delay' => true]
'//www.packaged.in/res/p/8cac7/b/76d6c18/test.css' => ['delay' => true],
'//www.packaged.in/res/p/8cac7/b/2900bb5/test.less' => null
],
\Packaged\Dispatch\AssetManager::getUrisByType('css')
);
Expand Down
1 change: 1 addition & 0 deletions tests/Assets/GenericAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public function testAsset($ext, \Packaged\Dispatch\Assets\IAsset $class)
$extType = [
"ico" => "image/x-icon",
"css" => "text/css",
"less" => "text/css",
"js" => "text/javascript",
"json" => "application/json",
"png" => "image/png",
Expand Down
62 changes: 62 additions & 0 deletions tests/Assets/LessAssetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

class LessAssetTest extends PHPUnit_Framework_TestCase
{
public function testMinify()
{
$original = '@base: 24px;
@border-color: #B2B;
.underline { border-bottom: 1px solid green }
#header {
color: black;
border: 1px solid @border-color + #222222;
.navigation {
font-size: @base / 2;
a {
.underline;
}
}
.logo {
width: 300px;
:hover { text-decoration: none }
}
}
';

$expect = '.underline {
border-bottom: 1px solid green;
}
#header {
color: black;
border: 1px solid #dd44dd;
}
#header .navigation {
font-size: 12px;
}
#header .navigation a {
border-bottom: 1px solid green;
}
#header .logo {
width: 300px;
}
#header .logo :hover {
text-decoration: none;
}
';

$asset = new \Packaged\Dispatch\Assets\LessAsset();

$asset->setContent($original);
$this->assertEquals($expect, $asset->getContent());
}

public function testAsset()
{
$asset = new \Packaged\Dispatch\Assets\LessAsset();
$this->assertEquals('less', $asset->getExtension());
$this->assertEquals('text/css', $asset->getContentType());
}
}
20 changes: 20 additions & 0 deletions tests/asset/test.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@base: 24px;
@border-color: #B2B;

.underline { border-bottom: 1px solid green }

#header {
color: black;
border: 1px solid @border-color + #222222;

.navigation {
font-size: @base / 2;
a {
.underline;
}
}
.logo {
width: 300px;
:hover { text-decoration: none }
}
}

0 comments on commit 9f66578

Please sign in to comment.