Skip to content

Commit

Permalink
Merge branch 'feature/raw-on-development-or-production'
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlewis committed Jun 9, 2013
2 parents d09ecf4 + 1a52214 commit 72c8570
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 30 deletions.
31 changes: 29 additions & 2 deletions src/Basset/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class Asset extends Filterable {
*/
protected $log;

/**
* Application environment.
*
* @var string
*/
protected $appEnvironment;

/**
* Absolute path to the asset.
*
Expand Down Expand Up @@ -89,15 +96,17 @@ class Asset extends Filterable {
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Basset\Factory\FilterFactory $filterFactory
* @param \Illuminate\Log\Writer $log
* @param string $appEnvironment
* @param string $absolutePath
* @param string $relativePath
* @return void
*/
public function __construct(Filesystem $files, FilterFactory $filterFactory, Writer $log, $absolutePath, $relativePath)
public function __construct(Filesystem $files, FilterFactory $filterFactory, Writer $log, $appEnvironment, $absolutePath, $relativePath)
{
$this->files = $files;
$this->filterFactory = $filterFactory;
$this->log = $log;
$this->appEnvironment = $appEnvironment;
$this->absolutePath = $absolutePath;
$this->relativePath = $relativePath;
$this->filters = new \Illuminate\Support\Collection;
Expand Down Expand Up @@ -344,12 +353,30 @@ public function raw()
return $this;
}

/**
* Sets the asset to be served raw when the application is running in a given environment.
*
* @param string|array $environment
* @return \Basset\Asset
*/
public function rawOnEnvironment()
{
$environments = array_flatten(func_get_args());

if (in_array($this->appEnvironment, $environments))
{
return $this->raw();
}

return $this;
}

/**
* Determines if the asset is to be served raw.
*
* @return bool
*/
public function serveRaw()
public function isRaw()
{
return $this->raw;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Basset/BassetServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected function registerFactories()
{
$this->app['basset.factory.asset'] = $this->app->share(function($app)
{
return new AssetFactory($app['files'], $app['basset.factory.filter'], $app['basset.log'], $app['path.public']);
return new AssetFactory($app['files'], $app['basset.factory.filter'], $app['basset.log'], $app['env'], $app['path.public']);
});

$this->app['basset.factory.filter'] = $this->app->share(function($app)
Expand Down
4 changes: 2 additions & 2 deletions src/Basset/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getAssetsOnlyRaw($group = null)
// as being raw.
$assets = $this->getAssets($group, true)->filter(function($asset)
{
return $asset->serveRaw();
return $asset->isRaw();
});

return $assets;
Expand All @@ -84,7 +84,7 @@ public function getAssets($group = null, $raw = true)

foreach ($assets as $key => $asset)
{
if ( ! $raw and $asset->serveRaw() or ! is_null($group) and ! $asset->{'is'.ucfirst(str_singular($group))}())
if ( ! $raw and $asset->isRaw() or ! is_null($group) and ! $asset->{'is'.ucfirst(str_singular($group))}())
{
$assets->forget($key);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Basset/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,18 @@ public function raw()
return $this;
}

/**
* All assets within directory will be served raw on a given environment.
*
* @return \Basset\Directory
*/
public function rawOnEnvironment($environment)
{
$this->assets->each(function($asset) use ($environment) { $asset->rawOnEnvironment($environment); });

return $this;
}

/**
* Get the path to the directory.
*
Expand Down
13 changes: 11 additions & 2 deletions src/Basset/Factory/AssetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class AssetFactory implements FactoryInterface {
*/
protected $log;

/**
* Application environment.
*
* @var string
*/
protected $appEnvironment;

/**
* Path to the public directory.
*
Expand All @@ -47,14 +54,16 @@ class AssetFactory implements FactoryInterface {
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Basset\Factory\FilterFactory $filter
* @param \Illuminate\Log\Writer $log
* @param string $appEnvironment
* @param string $publicPath
* @return void
*/
public function __construct(Filesystem $files, FilterFactory $filter, Writer $log, $publicPath)
public function __construct(Filesystem $files, FilterFactory $filter, Writer $log, $appEnvironment, $publicPath)
{
$this->files = $files;
$this->filter = $filter;
$this->log = $log;
$this->appEnvironment = $appEnvironment;
$this->publicPath = $publicPath;
}

Expand All @@ -70,7 +79,7 @@ public function make($path)

$relativePath = $this->buildRelativePath($absolutePath);

$asset = new Asset($this->files, $this->filter, $this->log, $absolutePath, $relativePath);
$asset = new Asset($this->files, $this->filter, $this->log, $this->appEnvironment, $absolutePath, $relativePath);

return $asset->setOrder($this->nextAssetOrder());
}
Expand Down
6 changes: 3 additions & 3 deletions src/Basset/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ protected function serveDevelopmentCollection(Collection $collection, $group, $f

foreach ($collection->getAssetsWithRaw($group) as $asset)
{
if ($asset->serveRaw() or ! $path = $entry->getDevelopmentAsset($asset))
if ( ! $asset->isRaw() and $path = $entry->getDevelopmentAsset($asset))
{
$path = $asset->getRelativePath();
$path = $this->prefixBuildPath($identifier.'/'.$path);
}
else
{
$path = $this->prefixBuildPath($identifier.'/'.$path);
$path = $asset->getRelativePath();
}

$responses[] = $this->{'create'.studly_case($group).'Element'}($path, $format);
Expand Down
8 changes: 4 additions & 4 deletions tests/Basset/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function setUp()

$this->log = m::mock('Illuminate\Log\Writer');

$this->asset = new Asset($this->files, $this->filter, $this->log, 'path/to/public/foo/bar.sass', 'foo/bar.sass');
$this->asset = new Asset($this->files, $this->filter, $this->log, 'testing', 'path/to/public/foo/bar.sass', 'foo/bar.sass');
$this->asset->setOrder(1);
$this->asset->setGroup('stylesheets');
}
Expand All @@ -43,7 +43,7 @@ public function testGetAssetProperties()
public function testAssetsCanBeServedRaw()
{
$this->asset->raw();
$this->assertTrue($this->asset->serveRaw());
$this->assertTrue($this->asset->isRaw());
}


Expand All @@ -63,15 +63,15 @@ public function testCheckingOfAssetGroupWhenNoGroupSupplied()

public function testAssetCanBeRemotelyHosted()
{
$asset = new Asset($this->files, $this->filter, $this->log, 'http://foo.com/bar.css', 'http://foo.com/bar.css');
$asset = new Asset($this->files, $this->filter, $this->log, 'testing', 'http://foo.com/bar.css', 'http://foo.com/bar.css');

$this->assertTrue($asset->isRemote());
}


public function testAssetCanBeRemotelyHostedWithRelativeProtocol()
{
$asset = new Asset($this->files, $this->filter, $this->log, '//foo.com/bar.css', '//foo.com/bar.css');
$asset = new Asset($this->files, $this->filter, $this->log, 'testing', '//foo.com/bar.css', '//foo.com/bar.css');

$this->assertTrue($asset->isRemote());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Basset/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function testGettingCollectionRawAssets()

public function newAsset($relative, $absolute, $group, $order)
{
$asset = new Asset(m::mock('Illuminate\Filesystem\Filesystem'), m::mock('Basset\Factory\FilterFactory'), m::mock('Illuminate\Log\Writer'), $absolute, $relative);
$asset = new Asset(m::mock('Illuminate\Filesystem\Filesystem'), m::mock('Basset\Factory\FilterFactory'), m::mock('Illuminate\Log\Writer'), 'testing', $absolute, $relative);

return $asset->setOrder($order)->setGroup($group);
}
Expand Down
20 changes: 10 additions & 10 deletions tests/Basset/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public function setUp()
$this->files = m::mock('Illuminate\Filesystem\Filesystem');
$this->finder = m::mock('Basset\AssetFinder');
$this->filter = m::mock('Basset\Factory\FilterFactory');
$this->asset = m::mock('Basset\Factory\AssetFactory', array($this->files, $this->filter, $this->log, 'path/to/public'))->shouldDeferMissing();
$this->asset = m::mock('Basset\Factory\AssetFactory', array($this->files, $this->filter, $this->log, 'testing', 'path/to/public'))->shouldDeferMissing();

$this->directory = new Directory($this->log, $this->asset, $this->filter, $this->finder, 'foo');
}


public function testAddingBasicAssetFromPublicDirectory()
{
$asset = new Asset($this->files, $this->filter, $this->log, null, null);
$asset = new Asset($this->files, $this->filter, $this->log, 'testing', null, null);

$this->finder->shouldReceive('find')->once()->with('foo.css')->andReturn('path/to/foo.css');
$this->asset->shouldReceive('make')->once()->with('path/to/foo.css')->andReturn($asset);
Expand All @@ -40,7 +40,7 @@ public function testAddingBasicAssetFromPublicDirectory()

public function testAddingInvalidAssetReturnsBlankAssetInstance()
{
$asset = new Asset($this->files, $this->filter, $this->log, null, null);
$asset = new Asset($this->files, $this->filter, $this->log, 'testing', null, null);

$this->finder->shouldReceive('find')->once()->with('foo.css')->andThrow('Basset\Exceptions\AssetNotFoundException');
$this->asset->shouldReceive('make')->once()->with(null)->andReturn($asset);
Expand All @@ -54,7 +54,7 @@ public function testAddingInvalidAssetReturnsBlankAssetInstance()

public function testAddingAssetFiresCallback()
{
$asset = new Asset($this->files, $this->filter, $this->log, null, null);
$asset = new Asset($this->files, $this->filter, $this->log, 'testing', null, null);

$this->finder->shouldReceive('find')->once()->with('foo.js')->andReturn('path/to/foo.js');
$this->asset->shouldReceive('make')->once()->with('path/to/foo.js')->andReturn($asset);
Expand Down Expand Up @@ -118,7 +118,7 @@ public function testRequireCurrentWorkingDirectory()
$files[0]->shouldReceive('getPathname')->andReturn('foo/bar.css');
$files[1]->shouldReceive('isFile')->andReturn(false);

$asset = new Asset($this->files, $this->filter, $this->log, null, null);
$asset = new Asset($this->files, $this->filter, $this->log, 'testing', null, null);
$this->finder->shouldReceive('find')->once()->with('foo/bar.css')->andReturn('foo/bar.css');
$this->asset->shouldReceive('make')->once()->with('foo/bar.css')->andReturn($asset);

Expand All @@ -144,7 +144,7 @@ public function testRequireDirectoryChangesDirectoryAndRequiresNewWorkingDirecto
$file->shouldReceive('isFile')->andReturn(true);
$file->shouldReceive('getPathname')->andReturn('foo/bar/baz.css');

$asset = new Asset($this->files, $this->filter, $this->log, null, null);
$asset = new Asset($this->files, $this->filter, $this->log, 'testing', null, null);
$this->finder->shouldReceive('find')->once()->with('foo/bar/baz.css')->andReturn('foo/bar/baz.css');
$this->asset->shouldReceive('make')->once()->with('foo/bar/baz.css')->andReturn($asset);

Expand Down Expand Up @@ -210,9 +210,9 @@ public function testGettingOfDirectoryPath()

public function testExcludingOfAssetsFromDirectory()
{
$fooAsset = new Asset($this->files, $this->filter, $this->log, 'path/to/foo.css', 'foo.css');
$fooAsset = new Asset($this->files, $this->filter, $this->log, 'testing', 'path/to/foo.css', 'foo.css');
$fooAsset->setOrder(1);
$barAsset = new Asset($this->files, $this->filter, $this->log, 'path/to/bar.css', 'bar.css');
$barAsset = new Asset($this->files, $this->filter, $this->log, 'testing', 'path/to/bar.css', 'bar.css');
$barAsset->setOrder(1);

$this->finder->shouldReceive('find')->once()->with('foo.css')->andReturn('path/to/foo.css');
Expand All @@ -232,9 +232,9 @@ public function testExcludingOfAssetsFromDirectory()

public function testIncludingOfAssetsFromDirectory()
{
$fooAsset = new Asset($this->files, $this->filter, $this->log, 'path/to/foo.css', 'foo.css');
$fooAsset = new Asset($this->files, $this->filter, $this->log, 'testing', 'path/to/foo.css', 'foo.css');
$fooAsset->setOrder(1);
$barAsset = new Asset($this->files, $this->filter, $this->log, 'path/to/bar.css', 'bar.css');
$barAsset = new Asset($this->files, $this->filter, $this->log, 'testing', 'path/to/bar.css', 'bar.css');
$barAsset->setOrder(1);

$this->finder->shouldReceive('find')->once()->with('foo.css')->andReturn('path/to/foo.css');
Expand Down
2 changes: 1 addition & 1 deletion tests/Basset/Factory/AssetFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function setUp()
$this->files = m::mock('Illuminate\Filesystem\Filesystem');
$this->filter = m::mock('Basset\Factory\FilterFactory');
$this->log = m::mock('Illuminate\Log\Writer');
$this->factory = new Basset\Factory\AssetFactory($this->files, $this->filter, $this->log, __DIR__, 'testing');
$this->factory = new Basset\Factory\AssetFactory($this->files, $this->filter, $this->log, 'testing', __DIR__);
}


Expand Down
2 changes: 1 addition & 1 deletion tests/Basset/Manifest/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testAddingDevelopmentAssetToEntry()

public function testAddingDevelopmentAssetToEntryFromAssetInstance()
{
$asset = new Basset\Asset($files = m::mock('Illuminate\Filesystem\Filesystem'), m::mock('Basset\Factory\FilterFactory'), m::mock('Illuminate\Log\Writer'), 'foo/bar.sass', 'foo/bar.sass');
$asset = new Basset\Asset($files = m::mock('Illuminate\Filesystem\Filesystem'), m::mock('Basset\Factory\FilterFactory'), m::mock('Illuminate\Log\Writer'), 'testing', 'foo/bar.sass', 'foo/bar.sass');
$files->shouldReceive('lastModified')->once()->with('foo/bar.sass')->andReturn(time());
$this->entry->addDevelopmentAsset($asset);
$this->data['development']['stylesheets']['foo/bar.sass'] = 'foo/bar-'.md5('[]'.time()).'.css';
Expand Down
37 changes: 34 additions & 3 deletions tests/Basset/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ public function testServingDevelopmentCollectionReturnsExpectedHtml()
m::mock('Basset\Asset')
));

$assets[0]->shouldReceive('serveRaw')->once()->andReturn(false);
$assets[0]->shouldReceive('isRaw')->once()->andReturn(false);
$assets[0]->shouldReceive('getGroup')->once()->andReturn('stylesheets');
$assets[0]->shouldReceive('getRelativePath')->once()->andReturn('bar.less');
$assets[1]->shouldReceive('serveRaw')->once()->andReturn(true);
$assets[1]->shouldReceive('isRaw')->once()->andReturn(true);
$assets[1]->shouldReceive('getRelativePath')->once()->andReturn('qux.css');
$assets[2]->shouldReceive('serveRaw')->once()->andReturn(false);
$assets[2]->shouldReceive('isRaw')->once()->andReturn(false);
$assets[2]->shouldReceive('getGroup')->once()->andReturn('stylesheets');
$assets[2]->shouldReceive('getRelativePath')->once()->andReturn('baz.sass');

Expand Down Expand Up @@ -149,4 +149,35 @@ public function testServingCollectionsWithCustomFormat()
}


public function testServingRawAssetsOnGivenEnvironment()
{
$this->app['basset']->shouldReceive(array('offsetExists' => true, 'offsetGet' => $collection = m::mock('Basset\Collection')))->with('foo');
$this->app['config']->shouldReceive('get')->once()->with('basset::production')->andReturn('production');

$this->app['basset.builder']->shouldReceive('buildAsDevelopment')->once()->with($collection, 'stylesheets');
$this->app['basset.builder.cleaner']->shouldReceive('clean')->once()->with('foo');

$collection->shouldReceive('getIdentifier')->andReturn('foo');
$collection->shouldReceive('getAssetsWithRaw')->once()->with('stylesheets')->andReturn($assets = array(
m::mock('Basset\Asset', array(new Filesystem, m::mock('Basset\Factory\FilterFactory'), m::mock('Illuminate\Log\Writer'), 'testing', null, null))->shouldDeferMissing(),
m::mock('Basset\Asset')->shouldDeferMissing()
));

$assets[0]->rawOnEnvironment('testing');
$assets[0]->shouldReceive('getRelativePath')->once()->andReturn('bar.css');
$assets[1]->shouldReceive('isRaw')->once()->andReturn(false);
$assets[1]->shouldReceive('getGroup')->once()->andReturn('stylesheets');
$assets[1]->shouldReceive('getRelativePath')->once()->andReturn('baz.sass');

$entry = $this->app['basset.manifest']->make($collection);
$entry->addDevelopmentAsset('baz.sass', 'baz.css', 'stylesheets');

$this->app['config']->shouldReceive('get')->with('basset::build_path')->andReturn('assets');

$expected = '<link rel="stylesheet" type="text/css" href="http://localhost/bar.css" />'.PHP_EOL.
'<link rel="stylesheet" type="text/css" href="http://localhost/assets/foo/baz.css" />';
$this->assertEquals($expected, $this->server->serve('foo', 'stylesheets'));
}


}

0 comments on commit 72c8570

Please sign in to comment.