]> Raphaƫl G. Git Repositories - packbundle/blob - Asset/PathPackage.php
Add asset path package class
[packbundle] / Asset / PathPackage.php
1 <?php
2
3 namespace Rapsys\PackBundle\Asset;
4
5 use Symfony\Component\Asset\Context\ContextInterface;
6 use Symfony\Component\Asset\PathPackage as BasePackage;
7 use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
8
9 /**
10 * (@inheritdoc)
11 */
12 class PathPackage extends BasePackage {
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 * {@inheritdoc}
35 */
36 public function getUrl($path) {
37 //Match url starting with a bundle name
38 if (preg_match('%^@([A-Z][a-zA-Z]*)Bundle/Resources/public(/.*)$%', $path, $matches)) {
39 //Handle empty or without replacement pattern basePath
40 if (empty($this->basePath) || strpos($this->basePath, '%s') === false) {
41 //Set path from hardcoded format
42 $path = '/bundles/'.strtolower($matches[1]).$matches[2];
43 //Proceed with basePath pattern replacement
44 } else {
45 //Set path from basePath pattern
46 $path = sprintf($this->basePath, strtolower($matches[1])).$matches[2];
47 }
48 }
49
50 //Return parent getUrl result
51 return parent::getUrl($path);
52 }
53 }