1 | <?php |
||
2 | |||
3 | namespace ByTIC\Assets\Assets\Asset; |
||
4 | |||
5 | use ByTIC\Assets\Assets\Asset; |
||
6 | use Nip\Utility\Str; |
||
7 | use Nip\Utility\Traits\SingletonTrait; |
||
8 | |||
9 | /** |
||
10 | * Class AssetSource |
||
11 | * @package ByTIC\Assets\Assets\Asset |
||
12 | */ |
||
13 | class AssetSource |
||
14 | { |
||
15 | use SingletonTrait; |
||
16 | |||
17 | /** |
||
18 | * @param $source |
||
19 | * @param $type |
||
20 | * @return string |
||
21 | 9 | */ |
|
22 | public static function check($source, $type) |
||
23 | { |
||
24 | 4 | return function () use ($source, $type) { |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
25 | 9 | return static::instance()->buildSource($source, $type); |
|
26 | }; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @param $source |
||
31 | * @param $type |
||
32 | * @return string |
||
33 | 7 | */ |
|
34 | protected function buildSource($source, $type) |
||
35 | 7 | { |
|
36 | 1 | if (empty($source)) { |
|
37 | return $source; |
||
38 | 6 | } |
|
39 | 4 | if ($this->isFullPath($source)) { |
|
40 | return $source; |
||
41 | 2 | } |
|
42 | 1 | if ($type === Asset::TYPE_STYLES) { |
|
43 | return $this->transformSourceWithAsset('/stylesheets/'.$source.'.css'); |
||
44 | 1 | } |
|
45 | 1 | if ($type === Asset::TYPE_SCRIPTS) { |
|
46 | return $this->transformSourceWithAsset('/scripts/'.$source.'.js'); |
||
47 | } |
||
48 | |||
49 | return $source; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param $source |
||
54 | * @return bool |
||
55 | 6 | */ |
|
56 | protected function isFullPath($source) |
||
57 | 6 | { |
|
58 | 3 | if (Str::startsWith($source, 'http')) { |
|
59 | return true; |
||
60 | 3 | } |
|
61 | 1 | if (strpos($source, '.css') !== false) { |
|
62 | return true; |
||
63 | } |
||
64 | 2 | if (strpos($source, '.js') !== false) { |
|
65 | return true; |
||
66 | } |
||
67 | |||
68 | return false; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param $source |
||
73 | * @return string |
||
74 | */ |
||
75 | protected function transformSourceWithAsset($source) |
||
76 | { |
||
77 | return \asset($source); |
||
78 | } |
||
79 | } |
||
80 |