|
7 | 7 | use c;
|
8 | 8 | use html;
|
9 | 9 |
|
10 |
| -class CSS extends \Kirby\Component\CSS { |
| 10 | +class CSS extends \Kirby\Component\CSS |
| 11 | +{ |
11 | 12 |
|
12 |
| - /** |
13 |
| - * Builds the html link tag for the given css file |
14 |
| - * |
15 |
| - * @param string $url |
16 |
| - * @param string|array $media Either a media string or an array of attributes |
17 |
| - * @return string |
18 |
| - */ |
| 13 | + /** |
| 14 | + * Builds the html link tag for the given css file |
| 15 | + * |
| 16 | + * @param string $url |
| 17 | + * @param string|array $media Either a media string or an array of attributes |
| 18 | + * @return string |
| 19 | + */ |
19 | 20 |
|
20 |
| - public function tag($url, $media = null) { |
| 21 | + public function tag($url, $media = null) |
| 22 | + { |
| 23 | + if (is_array($url)) { |
| 24 | + $css = array(); |
| 25 | + foreach ($url as $u) { |
| 26 | + $css[] = $this->tag($u, $media); |
| 27 | + } |
| 28 | + return implode(PHP_EOL, $css) . PHP_EOL; |
| 29 | + } |
21 | 30 |
|
22 |
| - if(is_array($url)) { |
23 |
| - $css = array(); |
24 |
| - foreach($url as $u) $css[] = $this->tag($u, $media); |
25 |
| - return implode(PHP_EOL, $css) . PHP_EOL; |
26 |
| - } |
| 31 | + // auto template css files |
| 32 | + if ($url == '@auto') { |
| 33 | + $file = $this->kirby->site()->page()->template() . '.css'; |
| 34 | + $root = $this->kirby->roots()->autocss() . DS . $file; |
| 35 | + $url = $this->kirby->urls()->autocss() . '/' . $file; |
| 36 | + if (file_exists($root)) { |
| 37 | + $url = preg_replace('#^' . $this->kirby->urls()->index() . '/#', null, $url); |
| 38 | + } |
| 39 | + } |
27 | 40 |
|
28 |
| - // auto template css files |
29 |
| - if($url == '@auto') { |
30 |
| - $file = $this->kirby->site()->page()->template() . '.css'; |
31 |
| - $root = $this->kirby->roots()->autocss() . DS . $file; |
32 |
| - $url = $this->kirby->urls()->autocss() . '/' . $file; |
33 |
| - if(!file_exists($root)) return false; |
34 |
| - $url = preg_replace('#^' . $this->kirby->urls()->index() . '/#', null, $url); |
35 |
| - } |
| 41 | + $url = ltrim($url, '/'); |
36 | 42 |
|
37 |
| - $url = ltrim($url, '/'); |
| 43 | + if (file_exists($url)) { |
| 44 | + // generate sri hash for css files |
| 45 | + $cssInput = (new Asset($url))->content(); |
| 46 | + $cssIntegrity = sri_checksum($cssInput); |
38 | 47 |
|
39 |
| - if(file_exists($url)) { |
40 |
| - // generate sri hash for css files |
41 |
| - $cssInput = (new Asset($url))->content(); |
42 |
| - $cssIntegrity = sri_checksum($cssInput); |
| 48 | + if (c::get('fingerprinting')) { |
| 49 | + // add timestamp for cache-busting |
| 50 | + $modified = filemtime($url); |
| 51 | + $filename = f::name($url) . '.' . $modified . '.' . f::extension($url); |
| 52 | + $dirname = f::dirname($url); |
| 53 | + $url = ($dirname === '.') ? $filename : $dirname . '/' . $filename; |
| 54 | + } |
43 | 55 |
|
44 |
| - if(c::get('fingerprinting')) { |
45 |
| - // add timestamp for cache-busting |
46 |
| - $modified = filemtime($url); |
47 |
| - $filename = f::name($url) . '.' . $modified . '.' . f::extension($url); |
48 |
| - $dirname = f::dirname($url); |
49 |
| - $url = ($dirname === '.') ? $filename : $dirname . '/' . $filename; |
50 |
| - } |
| 56 | + // build an array of SRI-related attributes |
| 57 | + $cssOptions = array( |
| 58 | + 'integrity' => $cssIntegrity, // generated SRI hash |
| 59 | + 'crossorigin' => c::get('plugin.kirby-sri.crossorigin', 'anonymous'), // user-defined 'crossorigin' |
| 60 | + ); |
| 61 | + } |
51 | 62 |
|
52 |
| - // build an array of SRI-related attributes |
53 |
| - $cssOptions = array( |
54 |
| - 'integrity' => $cssIntegrity, // generated SRI hash |
55 |
| - 'crossorigin' => c::get('plugin.kirby-sri.crossorigin', 'anonymous'), // user-defined 'crossorigin' attribute |
56 |
| - ); |
57 |
| - } |
| 63 | + // build the array of HTML attributes |
| 64 | + $attr = array( |
| 65 | + 'rel' => 'stylesheet', |
| 66 | + 'href' => url($url) |
| 67 | + ); |
58 | 68 |
|
59 |
| - // build the array of HTML attributes |
60 |
| - $attr = array( |
61 |
| - 'rel' => 'stylesheet', |
62 |
| - 'href' => url($url) |
63 |
| - ); |
| 69 | + if (isset($cssOptions)) { |
| 70 | + $attr = array_merge($attr, $cssOptions); |
| 71 | + } |
64 | 72 |
|
65 |
| - if(isset($cssOptions)) { |
66 |
| - $attr = array_merge($attr, $cssOptions); |
67 |
| - } |
| 73 | + if (is_array($media)) { |
| 74 | + // merge array with custom options |
| 75 | + $attr = array_merge($attr, $media); |
| 76 | + } elseif (is_string($media)) { |
| 77 | + // if there's no such array, just set media key |
| 78 | + $attr['media'] = $media; |
| 79 | + } |
68 | 80 |
|
69 |
| - if(is_array($media)) { |
70 |
| - // merge array with custom options |
71 |
| - $attr = array_merge($attr, $media); |
72 |
| - } else if(is_string($media)) { |
73 |
| - // if there's no such array, just set media key |
74 |
| - $attr['media'] = $media; |
| 81 | + // return the proper 'link' tag |
| 82 | + return html::tag('link', null, $attr); |
75 | 83 | }
|
76 |
| - |
77 |
| - // return the proper 'link' tag |
78 |
| - return html::tag('link', null, $attr); |
79 |
| - } |
80 | 84 | }
|
0 commit comments