]> Raphaël G. Git Repositories - packbundle/commitdiff
Rename pack_package into path_package
authorRaphaël Gertz <git@rapsys.eu>
Wed, 14 Oct 2020 01:42:55 +0000 (03:42 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Wed, 14 Oct 2020 01:42:55 +0000 (03:42 +0200)
Asset/PathPackage.php [new file with mode: 0644]
Resources/config/packages/rapsys_pack.yaml

diff --git a/Asset/PathPackage.php b/Asset/PathPackage.php
new file mode 100644 (file)
index 0000000..50c6172
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+namespace Rapsys\PackBundle\Asset;
+
+use Symfony\Component\Asset\Context\ContextInterface;
+use Symfony\Component\Asset\Package;
+use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
+
+/**
+ * (@inheritdoc)
+ */
+class PathPackage extends Package {
+       //The base path
+       protected $basePath;
+
+       /**
+        * {@inheritdoc}
+        */
+       public function __construct(string $basePath, VersionStrategyInterface $versionStrategy, ContextInterface $context = null) {
+               parent::__construct($versionStrategy, $context);
+
+               if (!$basePath) {
+                       $this->basePath = '/';
+               } else {
+                       if ('/' != $basePath[0]) {
+                               $basePath = '/'.$basePath;
+                       }
+
+                       $this->basePath = rtrim($basePath, '/').'/';
+               }
+       }
+
+       /**
+        * @todo Try retrive public dir from the member function BundleNameBundle::getPublicDir() return value ?
+        * @xxx see https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure
+        * {@inheritdoc}
+        */
+       public function getUrl($path) {
+               //Match url starting with a bundle name
+               if (preg_match('%^@([A-Z][a-zA-Z]*?)(?:Bundle/Resources/public)?/(.*)$%', $path, $matches)) {
+                       //Handle empty or without replacement pattern basePath
+                       if (empty($this->basePath) || strpos($this->basePath, '%s') === false) {
+                               //Set path from hardcoded format
+                               $path = '/bundles/'.strtolower($matches[1]).'/'.$matches[2];
+                       //Proceed with basePath pattern replacement
+                       } else {
+                               //Set path from basePath pattern
+                               //XXX: basePath has a trailing / added by constructor
+                               $path = sprintf($this->basePath, strtolower($matches[1])).$matches[2];
+                       }
+               }
+
+               //Return parent getUrl result
+               return parent::getUrl($path);
+       }
+}
index cdae053fa54964e31d12b28090b65b8e164e5cbe..0fe0346939baeca1bd9b96677f027ad761642a42 100644 (file)
@@ -1,11 +1,11 @@
 #Services configuration
 services:
     #Register assets pack package
-    rapsys.pack_package:
-        class: 'Rapsys\PackBundle\Asset\PackPackage'
+    rapsys.path_package:
+        class: 'Rapsys\PackBundle\Asset\PathPackage'
         arguments: [ '/', '@assets.empty_version_strategy', '@assets.context' ]
     #Register twig pack extension
     rapsys.pack_extension:
         class: 'Rapsys\PackBundle\Twig\PackExtension'
-        arguments: [ '@file_locator', '@service_container', '@rapsys.pack_package' ]
+        arguments: [ '@file_locator', '@service_container', '@rapsys.path_package' ]
         tags: [ 'twig.extension' ]