]> Raphaël G. Git Repositories - packbundle/blob - Package/PathPackage.php
Rename routes config to match new bundle alias
[packbundle] / Package / PathPackage.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Package;
13
14 use Symfony\Component\Asset\Context\ContextInterface;
15 use Symfony\Component\Asset\Package;
16 use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
17
18 use Rapsys\PackBundle\Context\NullContext;
19
20 /**
21 * {@inheritdoc}
22 */
23 class PathPackage extends Package {
24 /**
25 * The base url
26 */
27 protected string $baseUrl;
28
29 /**
30 * {@inheritdoc}
31 */
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();
35
36 //Call parent constructor
37 parent::__construct($this->versionStrategy, $this->context);
38
39 //Without base path
40 if (empty($basePath)) {
41 //Set base path
42 $this->basePath = '/';
43 //With base path
44 } else {
45 //With relative base path
46 if ('/' != $basePath[0]) {
47 //Set base path as absolute
48 $basePath = '/'.$basePath;
49 }
50
51 //Set base path
52 $this->basePath = rtrim($basePath, '/').'/';
53 }
54
55 //Set base url
56 $this->baseUrl = $this->context->getBaseUrl();
57 }
58
59 /**
60 * Returns an absolute or root-relative public path
61 *
62 * Transform @BundleBundle to bundle and remove /Resources/public fragment from path
63 * This bundle name conversion and bundle prefix are the same as in asset:install command
64 *
65 * @link https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure
66 * @see vendor/symfony/framework-bundle/Command/AssetsInstallCommand.php +113
67 * @see vendor/symfony/framework-bundle/Command/AssetsInstallCommand.php +141
68 *
69 * {@inheritdoc}
70 */
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
79 } else {
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];
83 }
84 }
85
86 //Return parent getUrl result
87 return parent::getUrl($path);
88 }
89
90 /**
91 * Returns an absolute public path.
92 *
93 * @param string $path A path
94 * @return string The absolute public path
95 */
96 public function getAbsoluteUrl(string $path): string {
97 //Return concated base url and url from path
98 return $this->baseUrl.self::getUrl($path);
99 }
100 }