Browse Source

Add support for {plugin} placeholder in asset base URLs.

This allows generating correct URLs for plugin/theme assets when using a CDN.

Refs #10172
ADmad 6 years ago
parent
commit
9da7de273a
2 changed files with 13 additions and 5 deletions
  1. 9 1
      src/Routing/Asset.php
  2. 4 4
      tests/TestCase/Routing/AssetTest.php

+ 9 - 1
src/Routing/Asset.php

@@ -157,7 +157,15 @@ class Asset
             [$plugin, $path] = static::pluginSplit($path);
         }
         if (!empty($options['pathPrefix']) && $path[0] !== '/') {
-            $path = $options['pathPrefix'] . $path;
+            $pathPrefix = $options['pathPrefix'];
+            $placeHolderVal = '';
+            if (!empty($options['theme'])) {
+                $placeHolderVal = static::inflectString($options['theme']) . '/';
+            } elseif (isset($plugin)) {
+                $placeHolderVal = static::inflectString($plugin) . '/';
+            }
+
+            $path = str_replace('{plugin}', $placeHolderVal, $pathPrefix) . $path;
         }
         if (!empty($options['ext']) &&
             strpos($path, '?') === false &&

+ 4 - 4
tests/TestCase/Routing/AssetTest.php

@@ -447,14 +447,14 @@ class AssetTest extends TestCase
         $imageBaseUrl = Configure::read('App.imageBaseUrl');
         $jsBaseUrl = Configure::read('App.jsBaseUrl');
         $cssBaseUrl = Configure::read('App.cssBaseUrl');
-        Configure::write('App.imageBaseUrl', $cdnPrefix);
+        Configure::write('App.imageBaseUrl', $cdnPrefix . '{plugin}img/');
         $result = Asset::imageUrl('TestTheme.text.jpg');
-        $expected = $cdnPrefix . 'text.jpg';
+        $expected = $cdnPrefix . 'test_theme/img/text.jpg';
         $this->assertSame($expected, $result);
 
-        Configure::write('App.jsBaseUrl', $cdnPrefix);
+        Configure::write('App.jsBaseUrl', $cdnPrefix . '{plugin}js/');
         $result = Asset::scriptUrl('TestTheme.app.js');
-        $expected = $cdnPrefix . 'app.js';
+        $expected = $cdnPrefix . 'test_theme/js/app.js';
         $this->assertSame($expected, $result);
 
         Configure::write('App.cssBaseUrl', $cdnPrefix);