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 Symfony\Component\Asset\Context\ContextInterface
;
15 use Symfony\Component\Asset\Package
;
16 use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface
;
18 use Rapsys\PackBundle\Context\NullContext
;
23 class PathPackage
extends Package
{
33 public function __construct(string $basePath, VersionStrategyInterface
$versionStrategy, ContextInterface
$context = null) {
34 //Without context use a null context
35 $context = $context ?? new NullContext();
37 //Call parent constructor
38 parent
::__construct($versionStrategy, $context);
41 if (empty($basePath)) {
43 $this->basePath
= '/';
46 //With relative base path
47 if ('/' != $basePath[0]) {
48 //Set base path as absolute
49 $basePath = '/'.$basePath;
53 $this->basePath
= rtrim($basePath, '/').'/';
57 $this->baseUrl
= $context->getBaseUrl();
61 * Returns an absolute or root-relative public path
63 * Transform @BundleBundle to bundle and remove /Resources/public fragment from path
64 * This bundle name conversion and bundle prefix are the same as in asset:install command
66 * @link https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure
67 * @see vendor/symfony/framework-bundle/Command/AssetsInstallCommand.php +113
68 * @see vendor/symfony/framework-bundle/Command/AssetsInstallCommand.php +141
72 public function getUrl($path): string {
73 //Match url starting with a bundle name
74 if (preg_match('%^@([A-Z][a-zA-Z]*?)(?:Bundle/Resources/public)?/(.*)$%', $path, $matches)) {
75 //Handle empty or without replacement pattern basePath
76 if (empty($this->basePath
) || strpos($this->basePath
, '%s') === false) {
77 //Set path from hardcoded format
78 $path = '/bundles/'.strtolower($matches[1]).'/'.$matches[2];
79 //Proceed with basePath pattern replacement
81 //Set path from basePath pattern
82 //XXX: basePath has a trailing / added by constructor
83 $path = sprintf($this->basePath
, strtolower($matches[1])).$matches[2];
87 //Return parent getUrl result
88 return parent
::getUrl($path);
92 * Returns an absolute public path.
94 * @param string $path A path
95 * @return string The absolute public path
97 public function getAbsoluteUrl(string $path): string {
98 //Return concated base url and url from path
99 return $this->baseUrl
.self
::getUrl($path);