|
2 | 2 |
|
3 | 3 | namespace Docplanner\AssetsBundle\Service;
|
4 | 4 |
|
| 5 | +use Docplanner\AssetsBundle\IO\Asset; |
| 6 | + |
5 | 7 | class AssetsLoader
|
6 | 8 | {
|
| 9 | + |
| 10 | + /** @var AssetsPicker $assetsPicker */ |
| 11 | + private $asstsPicker; |
| 12 | + |
| 13 | + /** |
| 14 | + * @param AssetsPicker $assetsPicker |
| 15 | + */ |
| 16 | + public function __construct(AssetsPicker $assetsPicker) |
| 17 | + { |
| 18 | + $this->asstsPicker = $assetsPicker; |
| 19 | + } |
| 20 | + |
7 | 21 | /**
|
8 | 22 | * @param null $type
|
9 | 23 | *
|
10 | 24 | * @return string
|
11 | 25 | */
|
12 |
| - public function renderScript($type = 'file') |
| 26 | + public function renderScript($type = 'src') |
13 | 27 | {
|
14 |
| - $files = ['platform/js/rwd-common.js']; |
| 28 | + $assets = $this->asstsPicker->pickScriptAssets(); |
| 29 | + $mask = $type == 'inline' ? '<script>%s</script>' : '<script src="%s?%s"></script>'; |
15 | 30 |
|
16 |
| - $mask = $type == 'inline' ? '<script>%s</script>' : '<script src="%s?%s"></script>'; |
17 |
| - |
18 |
| - return $this->render($mask, $files, $type); |
| 31 | + return $this->render($mask, $assets, $type); |
19 | 32 | }
|
20 | 33 |
|
21 | 34 | /**
|
22 | 35 | * @param null $type
|
23 | 36 | *
|
24 | 37 | * @return string
|
25 | 38 | */
|
26 |
| - public function renderStyle($type = 'file') |
| 39 | + public function renderStyle($type = 'src') |
27 | 40 | {
|
28 |
| - $files = ['platform/css/rwd-common.css']; |
29 |
| - |
30 |
| - $mask = $type == 'inline' ? '<style>%s</style>' : '<link rel="stylesheet" type="text/css" href="%s?%s">'; |
| 41 | + $assets = $this->asstsPicker->pickStyleAssets(); |
| 42 | + $mask = $type == 'inline' ? '<style>%s</style>' : '<link rel="stylesheet" type="text/css" href="%s?%s">'; |
31 | 43 |
|
32 |
| - return $this->render($mask, $files, $type); |
| 44 | + return $this->render($mask, $assets, $type); |
33 | 45 | }
|
34 | 46 |
|
35 | 47 | /**
|
36 |
| - * @param $mask |
37 |
| - * @param $files |
38 |
| - * @param $type |
| 48 | + * @param $mask |
| 49 | + * @param Asset[] $assets |
| 50 | + * @param $type |
39 | 51 | *
|
40 | 52 | * @return string
|
41 | 53 | */
|
42 |
| - private function render($mask, $files, $type) |
| 54 | + private function render($mask, $assets, $type) |
43 | 55 | {
|
44 | 56 | $ret = '';
|
45 | 57 |
|
46 |
| - if ($type == 'file') |
| 58 | + if ($type == 'src') |
47 | 59 | {
|
48 |
| - foreach ($files as $file) |
| 60 | + foreach ($assets as $asset) |
49 | 61 | {
|
50 |
| - if (!file_exists($file)) |
| 62 | + if (!file_exists($asset->getSrc())) |
51 | 63 | {
|
52 |
| - throw new \LogicException(sprintf('File %s not found', $file)); |
| 64 | + throw new \LogicException(sprintf('File %s not found', $asset->getSrc())); |
53 | 65 | }
|
54 | 66 |
|
55 |
| - $ret .= sprintf($mask, $file, crc32(file_get_contents($file))); |
| 67 | + if ($asset->getSrc()) |
| 68 | + { |
| 69 | + $ret .= sprintf($mask, $asset->getSrc(), crc32(file_get_contents($asset->getSrc()))); |
| 70 | + } |
56 | 71 | }
|
57 | 72 | }
|
58 | 73 | else
|
59 | 74 | {
|
60 |
| - foreach ($files as $file) |
| 75 | + foreach ($assets as $asset) |
61 | 76 | {
|
62 |
| - if (!file_exists($file)) |
| 77 | + if (!file_exists($asset->getInline())) |
63 | 78 | {
|
64 |
| - throw new \LogicException(sprintf('File %s not found', $file)); |
| 79 | + throw new \LogicException(sprintf('File %s not found', $asset->getInline())); |
65 | 80 | }
|
66 | 81 |
|
67 |
| - $source = file_get_contents($file); |
68 |
| - $ret .= $source; |
| 82 | + if ($asset->getInline()) |
| 83 | + { |
| 84 | + $ret .= file_get_contents($asset->getInline()); |
| 85 | + } |
69 | 86 | }
|
70 | 87 |
|
71 |
| - $ret = sprintf($mask, $ret); |
| 88 | + if ($ret) |
| 89 | + { |
| 90 | + $ret = sprintf($mask, $ret); |
| 91 | + } |
72 | 92 | }
|
73 | 93 |
|
74 | 94 | return $ret;
|
|
0 commit comments