1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys PackBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\PackBundle\Package
; 
  14 use Rapsys\PackBundle\Context\NullContext
; 
  16 use Symfony\Component\Asset\Context\ContextInterface
; 
  17 use Symfony\Component\Asset\Package
; 
  18 use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface
; 
  23 class PathPackage 
extends Package 
{ 
  27         protected string $baseUrl; 
  32         public function __construct(protected string $basePath, protected VersionStrategyInterface 
$versionStrategy, protected ?ContextInterface 
$context = null) { 
  33                 //Without context use a null context 
  34                 $this->context 
= $this->context 
?? new NullContext(); 
  36                 //Call parent constructor 
  37                 parent
::__construct($this->versionStrategy
, $this->context
); 
  40                 if (empty($basePath)) { 
  42                         $this->basePath 
= '/'; 
  45                         //With relative base path 
  46                         if ('/' != $basePath[0]) { 
  47                                 //Set base path as absolute 
  48                                 $basePath = '/'.$basePath; 
  52                         $this->basePath 
= rtrim($basePath, '/').'/'; 
  56                 $this->baseUrl 
= $this->context
->getBaseUrl(); 
  62          * Returns an absolute or root-relative public path 
  64          * Transform @BundleBundle to bundle and remove /Resources/public fragment from path 
  65          * This bundle name conversion and bundle prefix are the same as in asset:install command 
  67          * @link https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure 
  68          * @see vendor/symfony/framework-bundle/Command/AssetsInstallCommand.php +113 
  69          * @see vendor/symfony/framework-bundle/Command/AssetsInstallCommand.php +141 
  71         public function getUrl(string $path): string { 
  72                 //Match url starting with a bundle name 
  73                 if (preg_match('%^@([A-Z][a-zA-Z]*?)(?:Bundle/Resources/public)?/(.*)$%', $path, $matches)) { 
  74                         //Handle empty or without replacement pattern basePath 
  75                         if (empty($this->basePath
) || strpos($this->basePath
, '%s') === false) { 
  76                                 //Set path from hardcoded format 
  77                                 $path = '/bundles/'.strtolower($matches[1]).'/'.$matches[2]; 
  78                         //Proceed with basePath pattern replacement 
  80                                 //Set path from basePath pattern 
  81                                 //XXX: basePath has a trailing / added by constructor 
  82                                 $path = sprintf($this->basePath
, strtolower($matches[1])).$matches[2]; 
  86                 //Return parent getUrl result 
  87                 return parent
::getUrl($path); 
  91          * Returns an absolute public path. 
  93          * @param string $path A path 
  94          * @return string The absolute public path 
  96         public function getAbsoluteUrl(string $path): string { 
  97                 //Return concated base url and url from path 
  98                 return $this->baseUrl
.self
::getUrl($path);