]> Raphaƫl G. Git Repositories - packbundle/blob - Asset/PackPackage.php
Rename service assets.pack_package in rapsys.pack_package
[packbundle] / Asset / PackPackage.php
1 <?php
2
3 namespace Rapsys\PackBundle\Asset;
4
5 use Symfony\Component\Asset\Context\ContextInterface;
6 use Symfony\Component\Asset\PathPackage;
7 use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
8
9 /**
10 * (@inheritdoc)
11 */
12 class PackPackage extends PathPackage {
13 //The base path
14 protected $basePath;
15
16 /**
17 * {@inheritdoc}
18 */
19 public function __construct(string $basePath, VersionStrategyInterface $versionStrategy, ContextInterface $context = null) {
20 parent::__construct($basePath, $versionStrategy, $context);
21
22 if (!$basePath) {
23 $this->basePath = '/';
24 } else {
25 if ('/' != $basePath[0]) {
26 $basePath = '/'.$basePath;
27 }
28
29 $this->basePath = rtrim($basePath, '/').'/';
30 }
31 }
32
33 /**
34 * @todo Try retrive public dir from the member function BundleNameBundle::getPublicDir() return value ?
35 * @xxx see https://symfony.com/doc/current/bundles.html#overridding-the-bundle-directory-structure
36 * {@inheritdoc}
37 */
38 public function getUrl($path) {
39 //Match url starting with a bundle name
40 if (preg_match('%^@([A-Z][a-zA-Z]*?)(?:Bundle/Resources/public)?/(.*)$%', $path, $matches)) {
41 //Handle empty or without replacement pattern basePath
42 if (empty($this->basePath) || strpos($this->basePath, '%s') === false) {
43 //Set path from hardcoded format
44 $path = '/bundles/'.strtolower($matches[1]).'/'.$matches[2];
45 //Proceed with basePath pattern replacement
46 } else {
47 //Set path from basePath pattern
48 //XXX: basePath has a trailing / added by constructor
49 $path = sprintf($this->basePath, strtolower($matches[1])).$matches[2];
50 }
51 }
52
53 //Return parent getUrl result
54 return parent::getUrl($path);
55 }
56 }