Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions src/Config/ConfigFinder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace AssetCompress\Config;

use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use MiniAsset\AssetConfig;

Expand All @@ -26,28 +28,55 @@
* In addition for each file found the `asset_compress.local.ini`
* will be loaded if it is present.
*
* @param string $path The configuration file path to start loading from.
* @param string|null $path The configuration file path to start loading from.
* @param bool $skipPlugins Whether to skip config files from plugins. Default `false`.
* @param bool $skipLocal Whether to skip config *local* files. Default `false`.
* @param string|bool|null $cache Whether to cache the loaded config. Defaults to debug mode level.
* @return \MiniAsset\AssetConfig The completed configuration object.
*/
public function loadAll(?string $path = null, bool $skipPlugins = false): AssetConfig
{
public function loadAll(
?string $path = null,
bool $skipPlugins = false,
bool $skipLocal = false,
bool|string|null $cache = null,
): AssetConfig {
if ($cache === null) {
$cache = Configure::read('AssetCompress.cache', !Configure::read('debug'));
}
if ($cache === true) {
$cache = 'default';

Check warning on line 47 in src/Config/ConfigFinder.php

View check run for this annotation

Codecov / codecov/patch

src/Config/ConfigFinder.php#L47

Added line #L47 was not covered by tests
}
if ($cache) {
$cachedConfig = Cache::read('asset_compress_config', $cache);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the cache key be dependent on the args path and skipPlugins maybe?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is necessary. I'd like to learn more about the use case before exposing that.

if ($cachedConfig) {
return $cachedConfig;

Check warning on line 52 in src/Config/ConfigFinder.php

View check run for this annotation

Codecov / codecov/patch

src/Config/ConfigFinder.php#L50-L52

Added lines #L50 - L52 were not covered by tests
}
}

if (!$path) {
$path = CONFIG . 'asset_compress.ini';
}
$config = new AssetConfig([], [
'WEBROOT' => WWW_ROOT,
]);
$this->_load($config, $path);
$this->_load($config, $path, '', $skipLocal);

if ($skipPlugins) {
if ($cache) {
Cache::write('asset_compress_config', $config, $cache);

Check warning on line 66 in src/Config/ConfigFinder.php

View check run for this annotation

Codecov / codecov/patch

src/Config/ConfigFinder.php#L66

Added line #L66 was not covered by tests
}

return $config;
}

$plugins = Plugin::loaded();
foreach ($plugins as $plugin) {
$pluginConfig = Plugin::path($plugin) . 'config' . DS . 'asset_compress.ini';
$this->_load($config, $pluginConfig, $plugin . '.');
$this->_load($config, $pluginConfig, $plugin . '.', $skipLocal);
}

if ($cache) {
Cache::write('asset_compress_config', $config, $cache);

Check warning on line 79 in src/Config/ConfigFinder.php

View check run for this annotation

Codecov / codecov/patch

src/Config/ConfigFinder.php#L79

Added line #L79 was not covered by tests
}

return $config;
Expand All @@ -59,15 +88,20 @@
* @param \MiniAsset\AssetConfig $config The config object to update.
* @param string $path The config file to load.
* @param string $prefix The prefix to use.
* @param bool $skipLocal Skip *.local.ini file lookup
* @return void
*/
protected function _load(AssetConfig $config, string $path, string $prefix = ''): void
protected function _load(AssetConfig $config, string $path, string $prefix = '', bool $skipLocal = false): void
{
if (file_exists($path)) {
$config->load($path, $prefix);
}

$localConfig = preg_replace('/(.*)\.ini$/', '$1.local.ini', $path);
if ($skipLocal) {
return;
}

$localConfig = (string)preg_replace('/(.*)\.ini$/', '$1.local.ini', $path);
if (file_exists($localConfig)) {
$config->load($localConfig, $prefix);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/AssetCompressMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected function mapType(AssetTarget $build): string
*
* @param \MiniAsset\AssetConfig $config The config object to use.
* @param string $url The url to get an asset name from.
* @return string|bool false if no build can be parsed from URL
* @return string|false false if no build can be parsed from URL
* with url path otherwise
*/
protected function getName(AssetConfig $config, string $url): bool|string
Expand Down
24 changes: 19 additions & 5 deletions src/View/Helper/AssetCompressHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class AssetCompressHelper extends Helper
*/
public array $helpers = ['Html'];

/**
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'skipPlugins' => false,
'skipLocal' => false,
'configPath' => CONFIG . 'asset_compress.ini',
'noconfig' => false,
];

/**
* Configuration object
*
Expand Down Expand Up @@ -66,15 +76,19 @@ class AssetCompressHelper extends Helper
* Constructor - finds and parses the ini file the plugin uses.
*
* @param \Cake\View\View $view The view instance to use.
* @param array $settings The settings for the helper.
* @param array $config The settings for the helper.
* @return void
*/
public function __construct(View $view, array $settings = [])
public function __construct(View $view, array $config = [])
{
parent::__construct($view, $settings);
if (empty($settings['noconfig'])) {
parent::__construct($view, $config);
if (!$this->getConfig('noconfig')) {
$skipPlugins = $this->getConfig('skipPlugins');
$skipLocal = $this->getConfig('skipLocal');
$configFinder = new ConfigFinder();
$this->assetConfig($configFinder->loadAll());
$this->assetConfig(
$configFinder->loadAll($this->getConfig('configPath'), $skipPlugins, $skipLocal),
);
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/AssetScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class AssetScannerTest extends TestCase
{
protected string $_testFiles;
protected AssetScanner $Scanner;

public function setUp(): void
{
parent::setUp();
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Command/AssetCompressCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class AssetCompressCommandsTest extends TestCase
{
use ConsoleIntegrationTestTrait;

protected string $testConfig;

/**
* setup method.
*
Expand All @@ -36,7 +38,6 @@ public function setUp(): void
public function tearDown(): void
{
parent::tearDown();
unset($this->Shell);
$fs = new Filesystem();
$fs->deleteDir(WWW_ROOT . 'cache_js');
$fs->deleteDir(WWW_ROOT . 'cache_css');
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/Config/ConfigFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/
class ConfigFinderTest extends TestCase
{
protected $_testFiles;
protected $testConfig;

/**
* setup method
*
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase/Filter/ImportInlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class ImportInlineTest extends TestCase
{
protected ImportInline $filter;

public function setUp(): void
{
parent::setUp();
Expand Down
8 changes: 6 additions & 2 deletions tests/TestCase/Filter/SprocketsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

class SprocketsTest extends TestCase
{
protected string $_testFiles;
protected string $_jsDir;
protected Sprockets $filter;

public function setUp(): void
{
parent::setUp();
Expand All @@ -34,9 +38,9 @@ public function testThemeAndPluginInclusion()
];
$this->filter->settings($settings);

$this->_themeDir = $this->_testFiles . 'Plugin' . DS . $settings['theme'] . DS;
$themeDir = $this->_testFiles . 'Plugin' . DS . $settings['theme'] . DS;

$content = file_get_contents($this->_themeDir . 'webroot' . DS . 'theme.js');
$content = file_get_contents($themeDir . 'webroot' . DS . 'theme.js');
$result = $this->filter->input('theme.js', $content);
$expected = <<<TEXT
var Theme = new Class({
Expand Down
66 changes: 66 additions & 0 deletions tests/TestCase/View/Helper/AssetCompressHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,72 @@ public function tearDown(): void
unset($this->Helper);
}

/**
* Test that plugin scans are enabled
*/
public function testConfigPathWithPlugins(): void
{
$this->loadPlugins(['TestAssetIni']);
$helper = new AssetCompressHelper($this->View, [
'configPath' => APP . 'config/bare.ini',
]);
$config = $helper->assetConfig();
$this->assertTrue($config->hasTarget('pink.css'));
$this->assertTrue($config->hasTarget('TestAssetIni.all.css'));
}

/**
* Test that plugin scans can be disabled.
*/
public function testSkipPluginsOption(): void
{
$this->loadPlugins(['TestAssetIni']);
$helper = new AssetCompressHelper($this->View, [
'skipPlugins' => true,
'configPath' => APP . 'config/bare.ini',
]);
$config = $helper->assetConfig();
$this->assertTrue($config->hasTarget('pink.css'));
$this->assertFalse($config->hasTarget('TestAssetIni.all.css'));
}

/**
* Test that .local file scans can be disabled.
*/
public function testLoadLocalOption(): void
{
$this->loadPlugins(['TestAssetIni']);
$helper = new AssetCompressHelper($this->View, [
'skipPlugins' => true,
'configPath' => APP . 'config/overridable.ini',
]);
$config = $helper->assetConfig();
$this->assertEquals('', $config->get('general.cacheConfig'));
$this->assertEquals(
'/path/to/local/yuicompressor',
$config->filterConfig('YuiJs')['path'],
);
}

/**
* Test that .local file scans can be disabled.
*/
public function testSkipLocalOption(): void
{
$this->loadPlugins(['TestAssetIni']);
$helper = new AssetCompressHelper($this->View, [
'skipPlugins' => true,
'skipLocal' => true,
'configPath' => APP . 'config/overridable.ini',
]);
$config = $helper->assetConfig();
$this->assertEquals('1', $config->get('general.cacheConfig'));
$this->assertEquals(
'/path/to/yuicompressor',
$config->filterConfig('YuiJs')['path'],
);
}

/**
* Test that generated elements can have attributes added.
*
Expand Down