]> Raphaël G. Git Repositories - packbundle/commitdiff
Add asset path package class
authorRaphaël Gertz <git@rapsys.eu>
Tue, 26 Nov 2019 15:26:47 +0000 (16:26 +0100)
committerRaphaël Gertz <git@rapsys.eu>
Tue, 26 Nov 2019 15:26:47 +0000 (16:26 +0100)
Asset/PathPackage.php [new file with mode: 0644]

diff --git a/Asset/PathPackage.php b/Asset/PathPackage.php
new file mode 100644 (file)
index 0000000..371ffcc
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+namespace Rapsys\PackBundle\Asset;
+
+use Symfony\Component\Asset\Context\ContextInterface;
+use Symfony\Component\Asset\PathPackage as BasePackage;
+use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
+
+/**
+ * (@inheritdoc)
+ */
+class PathPackage extends BasePackage {
+       //The base path
+       protected $basePath;
+
+       /**
+        * {@inheritdoc}
+        */
+       public function __construct(string $basePath, VersionStrategyInterface $versionStrategy, ContextInterface $context = null) {
+               parent::__construct($basePath, $versionStrategy, $context);
+
+               if (!$basePath) {
+                       $this->basePath = '/';
+               } else {
+                       if ('/' != $basePath[0]) {
+                               $basePath = '/'.$basePath;
+                       }
+
+                       $this->basePath = rtrim($basePath, '/').'/';
+               }
+       }
+
+       /**
+        * {@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
+                               $path = sprintf($this->basePath, strtolower($matches[1])).$matches[2];
+                       }
+               }
+
+               //Return parent getUrl result
+               return parent::getUrl($path);
+       }
+}