Skip to content

Commit

Permalink
Merge branch 'daveblake-test-scss-assets-via-dispatch'
Browse files Browse the repository at this point in the history
Fixed Scss Imports
  • Loading branch information
bajb committed Oct 28, 2014
2 parents a9fd55d + 2528fcd commit 454194c
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/Assets/AbstractDispatchableAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@ abstract class AbstractDispatchableAsset extends AbstractAsset
* @var AssetManager
*/
protected $_assetManager;
protected $_workingDirectory;
protected $_processedContent = false;

public function setAssetManager(AssetManager $am)
{
$this->_assetManager = $am;
}

/**
* Set the current working directory for an asset
*
* @param $directory
*
* @return static
*/
public function setWorkingDirectory($directory)
{
$this->_workingDirectory = $directory;
return $this;
}

/**
* Set the asset content
*
Expand Down Expand Up @@ -51,7 +65,7 @@ protected function _processContent()
//Find all URL(.*) and dispatch their values
$this->_content = preg_replace_callback(
'~url\(\s*[\'"]?([^\s\'"]*?)[\'"]?\s*\)~',
array($this, "_dispatchNestedUrl"),
[$this, "_dispatchNestedUrl"],
$this->_content
);

Expand Down
9 changes: 9 additions & 0 deletions src/Assets/IDispatchableAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ interface IDispatchableAsset extends IAsset
* @return IDispatchableAsset
*/
public function setAssetManager(AssetManager $am);

/**
* Set the current working directory for an asset
*
* @param $directory
*
* @return IDispatchableAsset
*/
public function setWorkingDirectory($directory);
}
7 changes: 6 additions & 1 deletion src/Assets/ScssAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ public function getContent()

if($this->_assetManager !== null)
{
$compiler->setImportPaths($this->_assetManager->getRelativePath());
$compiler->setImportPaths(
build_path(
$this->_workingDirectory,
$this->_assetManager->getRelativePath()
)
);
}
return $compiler->compile(parent::getContent());
}
Expand Down
2 changes: 2 additions & 0 deletions src/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ public function getResponseForPath($path, Request $request)
if($asset instanceof IDispatchableAsset)
{
//Set the asset manager

$asset->setWorkingDirectory(realpath($directory));
$asset->setAssetManager(AssetManager::buildFromUri($path));
}

Expand Down
10 changes: 10 additions & 0 deletions tests/DispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ public function urlProvider()
. 'test(mStyle.background);}'
];

/**
* Scss import paths should work correctly when accessed through dispatch
*/
$tests[] = [
array_merge($baseConfig, []),
'res/p/domain/b/filehash/test-import.scss',
'www.packaged.in',
file_get_contents(__DIR__ . '/asset/test-import.expect.css')
];

return $tests;
}

Expand Down
1 change: 0 additions & 1 deletion tests/ScssImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public function testScssImports()
$am->setRelativePath(__DIR__ . '/asset3/');
$asset->setAssetManager($am);
$asset->setContent(file_get_contents(__DIR__ . '/asset3/' . 'test.scss'));
$asset->setContent(file_get_contents(__DIR__ . '/asset3/' . 'test.scss'));

$this->assertEquals($expect, $asset->getContent());
}
Expand Down
6 changes: 6 additions & 0 deletions tests/asset/includes/base.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import 'constants';

body {
background-color: $color-blue;
color: $color-red;
}
2 changes: 2 additions & 0 deletions tests/asset/includes/constants.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$color-red: #c00;
$color-blue: #00c;
9 changes: 9 additions & 0 deletions tests/asset/test-import.expect.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body {
background-color: #00c;
color: #c00; }

div.blue {
background-color: #00c; }

div.red {
background-color: #c00; }
9 changes: 9 additions & 0 deletions tests/asset/test-import.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@import 'includes/base';

div.blue {
background-color: $color-blue;
}

div.red {
background-color: $color-red;
}

0 comments on commit 454194c

Please sign in to comment.