Skip to content
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
composer.lock
vendor/

# Editor specific folders
/nbproject/
10 changes: 9 additions & 1 deletion config/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@
// --------------------------------------------------------------------------
// You may want to disable this for development, to make debugging easier.
'enabled' => true,

// --------------------------------------------------------------------------
// Should we read css and js source files from resource path?
// --------------------------------------------------------------------------
// Setting to true means that, yes - resource path should be use
// otherwise public folder will be used
'use_resources' => false,

// --------------------------------------------------------------------------
// Where do we find the application's original assets?
// --------------------------------------------------------------------------
// This is a relative path from the root of the public folder.
// This is a relative path from the root of the public or resource/assets
// folder (see use_resource_path configuration item above)
'css_source' => 'css',
'js_source' => 'js',

Expand Down
53 changes: 48 additions & 5 deletions src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class Assets {
* @var bool
*/
private $enabled;

/**
* Use resource path instead of public for reading data?
*
* @var bool
*/
private $use_resources;

/**
* Where do we read CSS files.
Expand Down Expand Up @@ -180,14 +187,22 @@ class Assets {
* @var Filesystem
*/
private $public;

/**
* The filesystem corresponding to our public path.
*
* @var Filesystem
*/
private $resources;

/**
* Create an asset manager.
*
* @param array $config The local config, merged with the default config
* @param Filesystem $filesystem The public filesystem, where we read/write assets
* @param array $config The local config, merged with the default config
* @param Filesystem $public_filesystem The public filesystem, where we read/write assets
* @param Filesystem $resources_filesystem The resources filesystem, where we read/write assets
*/
public function __construct(array $config, Filesystem $filesystem) {
public function __construct(array $config, Filesystem $public_filesystem, Filesystem $resources_filesystem) {
$this
->setEnabled($config['enabled'])
->setCssSource($config['css_source'])
Expand All @@ -200,9 +215,33 @@ public function __construct(array $config, Filesystem $filesystem) {
->setNotifiers($config['notifiers'])
->setInlineThreshold($config['inline_threshold'])
->setGzipStatic($config['gzip_static'])
->setCollections($config['collections']);
->setCollections($config['collections'])
->setUseResources($config['use_resources']);

$this->public = $public_filesystem;
$this->resources = $resources_filesystem;
}

/**
* Sets use resource path property
*
* @param bool $use
*
* @return Assets
*/
public function setUseResources($use) {
$this->use_resources = $use;

$this->public = $filesystem;
return $this;
}

/**
* Gets use resource path property
*
* @return bool
*/
public function getUseResources() {
return $this->use_resources;
}

/**
Expand Down Expand Up @@ -504,12 +543,16 @@ private function processAssets(array $attributes, array $assets, $extension, $so
foreach ($assets as $asset) {
if ($this->isAbsoluteUrl($asset)) {
$hash = $this->hash($asset);
} elseif ($this->getUseResources()) {
$hash = $this->hash($asset . $this->resources->getTimestamp($source_dir . '/' . $asset));
} else {
$hash = $this->hash($asset . $this->public->getTimestamp($source_dir . '/' . $asset));
}
if (!$this->public->has($path . '/' . $hash . $extension)) {
if ($this->isAbsoluteUrl($asset)) {
$data = $this->getLoader()->loadUrl($asset);
} elseif ($this->getUseResources()) {
$data = $this->resources->read($source_dir . '/' . $asset);
} else {
$data = $this->public->read($source_dir . '/' . $asset);
}
Expand Down
5 changes: 3 additions & 2 deletions src/AssetsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public function register() {

// Bind our component into the IoC container.
$this->app->singleton('assets', function($app) {
$filesystem = new Filesystem(new Local(public_path()), ['visibility' => AdapterInterface::VISIBILITY_PUBLIC]);
$public_filesystem = new Filesystem(new Local(public_path()), ['visibility' => AdapterInterface::VISIBILITY_PUBLIC]);
$resources_filesystem = new Filesystem(new Local(resource_path('assets')), ['visibility' => AdapterInterface::VISIBILITY_PUBLIC]);

return new Assets($app['config']['assets'], $filesystem);
return new Assets($app['config']['assets'], $public_filesystem, $resources_filesystem);
});

// Command-line functions
Expand Down
19 changes: 19 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@
* @license GPLv3+
*/
class ConfigurationTest extends TestCase {

/**
* Test getting/setting the "enabled" option.
*
* @covers Fisharebest\LaravelAssets\Assets::__construct
* @covers Fisharebest\LaravelAssets\Assets::getUseResources
* @covers Fisharebest\LaravelAssets\Assets::setUseResources
*/
public function testUseResourcePath() {
$assets = $this->createDefaultAssets();
$this->assertFalse($assets->getUseResources());

$assets->setUseResources(false);
$this->assertFalse($assets->getUseResources());

$assets->setUseResources(true);
$this->assertTrue($assets->getUseResources());
}

/**
* Test getting/setting the "enabled" option.
*
Expand Down