diff --git a/src/Config/ConfigFinder.php b/src/Config/ConfigFinder.php index 8658e679..8d4c894c 100644 --- a/src/Config/ConfigFinder.php +++ b/src/Config/ConfigFinder.php @@ -1,6 +1,8 @@ WWW_ROOT, ]); - $this->_load($config, $path); + $this->_load($config, $path, '', $skipLocal); if ($skipPlugins) { + if ($cache) { + Cache::write('asset_compress_config', $config, $cache); + } + 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); } return $config; @@ -59,15 +88,20 @@ public function loadAll(?string $path = null, bool $skipPlugins = false): AssetC * @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); } diff --git a/src/Middleware/AssetCompressMiddleware.php b/src/Middleware/AssetCompressMiddleware.php index a73dd60b..bfe46658 100644 --- a/src/Middleware/AssetCompressMiddleware.php +++ b/src/Middleware/AssetCompressMiddleware.php @@ -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 diff --git a/src/View/Helper/AssetCompressHelper.php b/src/View/Helper/AssetCompressHelper.php index 919948c0..ec8d98a6 100644 --- a/src/View/Helper/AssetCompressHelper.php +++ b/src/View/Helper/AssetCompressHelper.php @@ -34,6 +34,16 @@ class AssetCompressHelper extends Helper */ public array $helpers = ['Html']; + /** + * @var array + */ + protected array $_defaultConfig = [ + 'skipPlugins' => false, + 'skipLocal' => false, + 'configPath' => CONFIG . 'asset_compress.ini', + 'noconfig' => false, + ]; + /** * Configuration object * @@ -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), + ); } } diff --git a/tests/TestCase/AssetScannerTest.php b/tests/TestCase/AssetScannerTest.php index 7bee1726..75321350 100644 --- a/tests/TestCase/AssetScannerTest.php +++ b/tests/TestCase/AssetScannerTest.php @@ -8,6 +8,9 @@ class AssetScannerTest extends TestCase { + protected string $_testFiles; + protected AssetScanner $Scanner; + public function setUp(): void { parent::setUp(); diff --git a/tests/TestCase/Command/AssetCompressCommandsTest.php b/tests/TestCase/Command/AssetCompressCommandsTest.php index 47a1b0e2..d522bbca 100644 --- a/tests/TestCase/Command/AssetCompressCommandsTest.php +++ b/tests/TestCase/Command/AssetCompressCommandsTest.php @@ -14,6 +14,8 @@ class AssetCompressCommandsTest extends TestCase { use ConsoleIntegrationTestTrait; + protected string $testConfig; + /** * setup method. * @@ -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'); diff --git a/tests/TestCase/Config/ConfigFinderTest.php b/tests/TestCase/Config/ConfigFinderTest.php index 8d943a41..c11ffb4b 100644 --- a/tests/TestCase/Config/ConfigFinderTest.php +++ b/tests/TestCase/Config/ConfigFinderTest.php @@ -10,6 +10,9 @@ */ class ConfigFinderTest extends TestCase { + protected $_testFiles; + protected $testConfig; + /** * setup method * diff --git a/tests/TestCase/Filter/ImportInlineTest.php b/tests/TestCase/Filter/ImportInlineTest.php index 4a902c81..74d034ef 100644 --- a/tests/TestCase/Filter/ImportInlineTest.php +++ b/tests/TestCase/Filter/ImportInlineTest.php @@ -8,6 +8,8 @@ class ImportInlineTest extends TestCase { + protected ImportInline $filter; + public function setUp(): void { parent::setUp(); diff --git a/tests/TestCase/Filter/SprocketsTest.php b/tests/TestCase/Filter/SprocketsTest.php index 648a7dfb..565ab278 100644 --- a/tests/TestCase/Filter/SprocketsTest.php +++ b/tests/TestCase/Filter/SprocketsTest.php @@ -8,6 +8,10 @@ class SprocketsTest extends TestCase { + protected string $_testFiles; + protected string $_jsDir; + protected Sprockets $filter; + public function setUp(): void { parent::setUp(); @@ -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 = <<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. *