1 | <?php |
||
20 | class AssetList extends AbstractViewHelper |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * Render a asset list by type |
||
25 | * |
||
26 | * @param ViewController $view The current view |
||
27 | * @param string $type The asset type |
||
28 | * @param bool $inHead If all assets should be concatenated |
||
29 | * within style tag in head |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | public function __invoke(ViewController $view, $type, $inHead = false) |
||
34 | { |
||
35 | $result = ''; |
||
36 | $pattern = ''; |
||
37 | |||
38 | switch ($type) { |
||
39 | |||
40 | case 'js': |
||
41 | $pattern = '<script src="%s"></script>'; |
||
42 | break; |
||
43 | |||
44 | case 'css': |
||
45 | $pattern = '<link rel="stylesheet" type="text/css" href="%s">'; |
||
46 | break; |
||
47 | |||
48 | } |
||
49 | |||
50 | /** @var array $files */ |
||
51 | $files = $view->getVariable('assets' . ucfirst($type)); |
||
52 | |||
53 | if (empty($files)) { |
||
54 | return ''; |
||
55 | } |
||
56 | |||
57 | if (defined('APPLICATION_ENV') && APPLICATION_ENV === 'production' && $inHead) { |
||
58 | |||
59 | $result = '<style type="text/css">'; |
||
60 | $result .= $this->_collectAssetsContent($files); |
||
61 | $result .= '</style>'; |
||
62 | return $result; |
||
63 | |||
64 | } |
||
65 | |||
66 | foreach ($files AS $file) { |
||
67 | $result .= sprintf($pattern, $file) . "\n"; |
||
68 | } |
||
69 | |||
70 | return $result; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Collect all assets content for concatenation |
||
75 | * |
||
76 | * @param array $files The asset files |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | private function _collectAssetsContent(array $files) |
||
104 | |||
105 | } |