Skip to content

Commit f5f0fc9

Browse files
authored
Merge pull request #374 from dereuromark/5.x-loading-fixes
Tried to adjust it as per needed options.
2 parents 1a3bfac + 0ae1919 commit f5f0fc9

File tree

9 files changed

+143
-16
lines changed

9 files changed

+143
-16
lines changed

src/Config/ConfigFinder.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace AssetCompress\Config;
33

4+
use Cake\Cache\Cache;
5+
use Cake\Core\Configure;
46
use Cake\Core\Plugin;
57
use MiniAsset\AssetConfig;
68

@@ -26,28 +28,55 @@ class ConfigFinder
2628
* In addition for each file found the `asset_compress.local.ini`
2729
* will be loaded if it is present.
2830
*
29-
* @param string $path The configuration file path to start loading from.
31+
* @param string|null $path The configuration file path to start loading from.
3032
* @param bool $skipPlugins Whether to skip config files from plugins. Default `false`.
33+
* @param bool $skipLocal Whether to skip config *local* files. Default `false`.
34+
* @param string|bool|null $cache Whether to cache the loaded config. Defaults to debug mode level.
3135
* @return \MiniAsset\AssetConfig The completed configuration object.
3236
*/
33-
public function loadAll(?string $path = null, bool $skipPlugins = false): AssetConfig
34-
{
37+
public function loadAll(
38+
?string $path = null,
39+
bool $skipPlugins = false,
40+
bool $skipLocal = false,
41+
bool|string|null $cache = null,
42+
): AssetConfig {
43+
if ($cache === null) {
44+
$cache = Configure::read('AssetCompress.cache', !Configure::read('debug'));
45+
}
46+
if ($cache === true) {
47+
$cache = 'default';
48+
}
49+
if ($cache) {
50+
$cachedConfig = Cache::read('asset_compress_config', $cache);
51+
if ($cachedConfig) {
52+
return $cachedConfig;
53+
}
54+
}
55+
3556
if (!$path) {
3657
$path = CONFIG . 'asset_compress.ini';
3758
}
3859
$config = new AssetConfig([], [
3960
'WEBROOT' => WWW_ROOT,
4061
]);
41-
$this->_load($config, $path);
62+
$this->_load($config, $path, '', $skipLocal);
4263

4364
if ($skipPlugins) {
65+
if ($cache) {
66+
Cache::write('asset_compress_config', $config, $cache);
67+
}
68+
4469
return $config;
4570
}
4671

4772
$plugins = Plugin::loaded();
4873
foreach ($plugins as $plugin) {
4974
$pluginConfig = Plugin::path($plugin) . 'config' . DS . 'asset_compress.ini';
50-
$this->_load($config, $pluginConfig, $plugin . '.');
75+
$this->_load($config, $pluginConfig, $plugin . '.', $skipLocal);
76+
}
77+
78+
if ($cache) {
79+
Cache::write('asset_compress_config', $config, $cache);
5180
}
5281

5382
return $config;
@@ -59,15 +88,20 @@ public function loadAll(?string $path = null, bool $skipPlugins = false): AssetC
5988
* @param \MiniAsset\AssetConfig $config The config object to update.
6089
* @param string $path The config file to load.
6190
* @param string $prefix The prefix to use.
91+
* @param bool $skipLocal Skip *.local.ini file lookup
6292
* @return void
6393
*/
64-
protected function _load(AssetConfig $config, string $path, string $prefix = ''): void
94+
protected function _load(AssetConfig $config, string $path, string $prefix = '', bool $skipLocal = false): void
6595
{
6696
if (file_exists($path)) {
6797
$config->load($path, $prefix);
6898
}
6999

70-
$localConfig = preg_replace('/(.*)\.ini$/', '$1.local.ini', $path);
100+
if ($skipLocal) {
101+
return;
102+
}
103+
104+
$localConfig = (string)preg_replace('/(.*)\.ini$/', '$1.local.ini', $path);
71105
if (file_exists($localConfig)) {
72106
$config->load($localConfig, $prefix);
73107
}

src/Middleware/AssetCompressMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected function mapType(AssetTarget $build): string
127127
*
128128
* @param \MiniAsset\AssetConfig $config The config object to use.
129129
* @param string $url The url to get an asset name from.
130-
* @return string|bool false if no build can be parsed from URL
130+
* @return string|false false if no build can be parsed from URL
131131
* with url path otherwise
132132
*/
133133
protected function getName(AssetConfig $config, string $url): bool|string

src/View/Helper/AssetCompressHelper.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ class AssetCompressHelper extends Helper
3434
*/
3535
public array $helpers = ['Html'];
3636

37+
/**
38+
* @var array<string, mixed>
39+
*/
40+
protected array $_defaultConfig = [
41+
'skipPlugins' => false,
42+
'skipLocal' => false,
43+
'configPath' => CONFIG . 'asset_compress.ini',
44+
'noconfig' => false,
45+
];
46+
3747
/**
3848
* Configuration object
3949
*
@@ -66,15 +76,19 @@ class AssetCompressHelper extends Helper
6676
* Constructor - finds and parses the ini file the plugin uses.
6777
*
6878
* @param \Cake\View\View $view The view instance to use.
69-
* @param array $settings The settings for the helper.
79+
* @param array $config The settings for the helper.
7080
* @return void
7181
*/
72-
public function __construct(View $view, array $settings = [])
82+
public function __construct(View $view, array $config = [])
7383
{
74-
parent::__construct($view, $settings);
75-
if (empty($settings['noconfig'])) {
84+
parent::__construct($view, $config);
85+
if (!$this->getConfig('noconfig')) {
86+
$skipPlugins = $this->getConfig('skipPlugins');
87+
$skipLocal = $this->getConfig('skipLocal');
7688
$configFinder = new ConfigFinder();
77-
$this->assetConfig($configFinder->loadAll());
89+
$this->assetConfig(
90+
$configFinder->loadAll($this->getConfig('configPath'), $skipPlugins, $skipLocal),
91+
);
7892
}
7993
}
8094

tests/TestCase/AssetScannerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
class AssetScannerTest extends TestCase
1010
{
11+
protected string $_testFiles;
12+
protected AssetScanner $Scanner;
13+
1114
public function setUp(): void
1215
{
1316
parent::setUp();

tests/TestCase/Command/AssetCompressCommandsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class AssetCompressCommandsTest extends TestCase
1414
{
1515
use ConsoleIntegrationTestTrait;
1616

17+
protected string $testConfig;
18+
1719
/**
1820
* setup method.
1921
*
@@ -36,7 +38,6 @@ public function setUp(): void
3638
public function tearDown(): void
3739
{
3840
parent::tearDown();
39-
unset($this->Shell);
4041
$fs = new Filesystem();
4142
$fs->deleteDir(WWW_ROOT . 'cache_js');
4243
$fs->deleteDir(WWW_ROOT . 'cache_css');

tests/TestCase/Config/ConfigFinderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
*/
1111
class ConfigFinderTest extends TestCase
1212
{
13+
protected $_testFiles;
14+
protected $testConfig;
15+
1316
/**
1417
* setup method
1518
*

tests/TestCase/Filter/ImportInlineTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class ImportInlineTest extends TestCase
1010
{
11+
protected ImportInline $filter;
12+
1113
public function setUp(): void
1214
{
1315
parent::setUp();

tests/TestCase/Filter/SprocketsTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
class SprocketsTest extends TestCase
1010
{
11+
protected string $_testFiles;
12+
protected string $_jsDir;
13+
protected Sprockets $filter;
14+
1115
public function setUp(): void
1216
{
1317
parent::setUp();
@@ -34,9 +38,9 @@ public function testThemeAndPluginInclusion()
3438
];
3539
$this->filter->settings($settings);
3640

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

39-
$content = file_get_contents($this->_themeDir . 'webroot' . DS . 'theme.js');
43+
$content = file_get_contents($themeDir . 'webroot' . DS . 'theme.js');
4044
$result = $this->filter->input('theme.js', $content);
4145
$expected = <<<TEXT
4246
var Theme = new Class({

tests/TestCase/View/Helper/AssetCompressHelperTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,72 @@ public function tearDown(): void
5656
unset($this->Helper);
5757
}
5858

59+
/**
60+
* Test that plugin scans are enabled
61+
*/
62+
public function testConfigPathWithPlugins(): void
63+
{
64+
$this->loadPlugins(['TestAssetIni']);
65+
$helper = new AssetCompressHelper($this->View, [
66+
'configPath' => APP . 'config/bare.ini',
67+
]);
68+
$config = $helper->assetConfig();
69+
$this->assertTrue($config->hasTarget('pink.css'));
70+
$this->assertTrue($config->hasTarget('TestAssetIni.all.css'));
71+
}
72+
73+
/**
74+
* Test that plugin scans can be disabled.
75+
*/
76+
public function testSkipPluginsOption(): void
77+
{
78+
$this->loadPlugins(['TestAssetIni']);
79+
$helper = new AssetCompressHelper($this->View, [
80+
'skipPlugins' => true,
81+
'configPath' => APP . 'config/bare.ini',
82+
]);
83+
$config = $helper->assetConfig();
84+
$this->assertTrue($config->hasTarget('pink.css'));
85+
$this->assertFalse($config->hasTarget('TestAssetIni.all.css'));
86+
}
87+
88+
/**
89+
* Test that .local file scans can be disabled.
90+
*/
91+
public function testLoadLocalOption(): void
92+
{
93+
$this->loadPlugins(['TestAssetIni']);
94+
$helper = new AssetCompressHelper($this->View, [
95+
'skipPlugins' => true,
96+
'configPath' => APP . 'config/overridable.ini',
97+
]);
98+
$config = $helper->assetConfig();
99+
$this->assertEquals('', $config->get('general.cacheConfig'));
100+
$this->assertEquals(
101+
'/path/to/local/yuicompressor',
102+
$config->filterConfig('YuiJs')['path'],
103+
);
104+
}
105+
106+
/**
107+
* Test that .local file scans can be disabled.
108+
*/
109+
public function testSkipLocalOption(): void
110+
{
111+
$this->loadPlugins(['TestAssetIni']);
112+
$helper = new AssetCompressHelper($this->View, [
113+
'skipPlugins' => true,
114+
'skipLocal' => true,
115+
'configPath' => APP . 'config/overridable.ini',
116+
]);
117+
$config = $helper->assetConfig();
118+
$this->assertEquals('1', $config->get('general.cacheConfig'));
119+
$this->assertEquals(
120+
'/path/to/yuicompressor',
121+
$config->filterConfig('YuiJs')['path'],
122+
);
123+
}
124+
59125
/**
60126
* Test that generated elements can have attributes added.
61127
*

0 commit comments

Comments
 (0)