diff --git a/.gitignore b/.gitignore index d8a7996..42c9aca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ composer.lock vendor/ + +# Editor specific folders +/nbproject/ diff --git a/config/assets.php b/config/assets.php index ca6b82a..3c6b64a 100644 --- a/config/assets.php +++ b/config/assets.php @@ -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', diff --git a/src/Assets.php b/src/Assets.php index ec06c3f..ae88ab2 100644 --- a/src/Assets.php +++ b/src/Assets.php @@ -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. @@ -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']) @@ -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; } /** @@ -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); } diff --git a/src/AssetsServiceProvider.php b/src/AssetsServiceProvider.php index 40df010..bc04dd8 100644 --- a/src/AssetsServiceProvider.php +++ b/src/AssetsServiceProvider.php @@ -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 diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 1f15113..d3113fc 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -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. *