]> Raphaël G. Git Repositories - packbundle/blob - Package/PathPackage.php
Add public path and url
[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 //The base path
25 protected $basePath;
26
27 //The base url
28 protected $baseUrl;
29
30 /**
31 * {@inheritdoc}
32 */
33 public function __construct(string $basePath, VersionStrategyInterface $versionStrategy, ContextInterface $context = null) {
34 //Without context use a null context
35 $context = $context ?? new NullContext();
36
37 //Call parent constructor
38 parent::__construct($versionStrategy, $context);
39
40 //Without base path
41 if (empty($basePath)) {
42 //Set base path
43 $this->basePath = '/';
44 //With base path
45 } else {
46 //With relative base path
47 if ('/' != $basePath[0]) {
48 //Set base path as absolute
49 $basePath = '/'.$basePath;
50 }
51
52 //Set base path
53 $this->basePath = rtrim($basePath, '/').'/';
54 }
55
56 //Set base url
57 $this->baseUrl = $context->getBaseUrl();
58 }
59
60 /**
61 * Returns an absolute or root-relative public path
62 *
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
65 *
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
69 *
70 * {@inheritdoc}
71 */
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
80 } else {
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];
84 }
85 }
86
87 //Return parent getUrl result
88 return parent::getUrl($path);
89 }
90
91 /**
92 * Returns an absolute public path.
93 *
94 * @param string $path A path
95 * @return string The absolute public path
96 */
97 public function getAbsoluteUrl(string $path): string {
98 //Return concated base url and url from path
99 return $this->baseUrl.self::getUrl($path);
100 }
101 }