Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit ff0478f

Browse files
committed
Applying PSR2 coding style
1 parent 7344ae6 commit ff0478f

File tree

3 files changed

+139
-129
lines changed

3 files changed

+139
-129
lines changed

composer.json

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
{
2-
"name": "s1syphos/kirby-sri",
3-
"description": "Subresource integrity hashing & cache-busting static assets for Kirby",
4-
"type": "kirby-plugin",
5-
"license": "MIT",
6-
"authors": [
7-
{
8-
"name": "Martin Folkers",
9-
"homepage": "https://twobrain.io",
10-
"role": "Project Author"
11-
}
12-
],
13-
"require": {
14-
"composer/installers": "~1.0"
15-
}
16-
}
1+
{
2+
"name": "s1syphos/kirby-sri",
3+
"description": "Subresource integrity hashing & cache-busting static assets for Kirby",
4+
"type": "kirby-plugin",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Martin Folkers",
9+
"homepage": "https://twobrain.io",
10+
"role": "Project Author"
11+
}
12+
],
13+
"require": {
14+
"composer/installers": "~1.0"
15+
}
16+
}

core/css.php

+62-58
Original file line numberDiff line numberDiff line change
@@ -7,74 +7,78 @@
77
use c;
88
use html;
99

10-
class CSS extends \Kirby\Component\CSS {
10+
class CSS extends \Kirby\Component\CSS
11+
{
1112

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+
*/
1920

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+
}
2130

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+
}
2740

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, '/');
3642

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);
3847

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+
}
4355

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+
}
5162

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+
);
5868

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+
}
6472

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+
}
6880

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);
7583
}
76-
77-
// return the proper 'link' tag
78-
return html::tag('link', null, $attr);
79-
}
8084
}

core/js.php

+61-55
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,77 @@
77
use c;
88
use html;
99

10-
class JS extends \Kirby\Component\JS {
10+
class JS extends \Kirby\Component\JS
11+
{
1112

12-
/**
13-
* Builds the html script tag for the given javascript file
14-
*
15-
* @param string $src
16-
* @param boolean|array $async Either true for the async attribute or an array of attributes
17-
* @return string
18-
*/
13+
/**
14+
* Builds the html script tag for the given javascript file
15+
*
16+
* @param string $src
17+
* @param boolean|array $async Either true for the async attribute or an array of attributes
18+
* @return string
19+
*/
1920

20-
public function tag($src, $async = false) {
21+
public function tag($src, $async = false)
22+
{
23+
if (is_array($src)) {
24+
$js = array();
25+
foreach ($src as $s) {
26+
$js[] = $this->tag($s, $async);
27+
}
28+
return implode(PHP_EOL, $js) . PHP_EOL;
29+
}
2130

22-
if(is_array($src)) {
23-
$js = array();
24-
foreach($src as $s) $js[] = $this->tag($s, $async);
25-
return implode(PHP_EOL, $js) . PHP_EOL;
26-
}
31+
// auto template css files
32+
if ($src == '@auto') {
33+
$file = $this->kirby->site()->page()->template() . '.js';
34+
$root = $this->kirby->roots()->autojs() . DS . $file;
35+
$src = $this->kirby->urls()->autojs() . '/' . $file;
36+
if (file_exists($root)) {
37+
$src = preg_replace('#^' . $this->kirby->urls()->index() . '/#', null, $url);
38+
}
39+
}
2740

28-
// auto template css files
29-
if($src == '@auto') {
30-
$file = $this->kirby->site()->page()->template() . '.js';
31-
$root = $this->kirby->roots()->autojs() . DS . $file;
32-
$src = $this->kirby->urls()->autojs() . '/' . $file;
33-
if(!file_exists($root)) return false;
34-
$src = preg_replace('#^' . $this->kirby->urls()->index() . '/#', null, $src);
35-
}
41+
$src = ltrim($src, '/');
3642

37-
$src = ltrim($src, '/');
43+
if (file_exists($src)) {
44+
// generate sri hash for css files
45+
$jsInput = (new Asset($src))->content();
46+
$jsIntegrity = sri_checksum($jsInput);
3847

39-
if(file_exists($src)) {
40-
// generate sri hash for css files
41-
$jsInput = (new Asset($src))->content();
42-
$jsIntegrity = sri_checksum($jsInput);
48+
if (c::get('fingerprinting')) {
49+
// add timestamp for cache-busting
50+
$modified = filemtime($src);
51+
$filename = f::name($src) . '.' . $modified . '.' . f::extension($src);
52+
$dirname = f::dirname($src);
53+
$src = ($dirname === '.') ? $filename : $dirname . '/' . $filename;
54+
}
4355

44-
if(c::get('fingerprinting')) {
45-
// add timestamp for cache-busting
46-
$modified = filemtime($src);
47-
$filename = f::name($src) . '.' . $modified . '.' . f::extension($src);
48-
$dirname = f::dirname($src);
49-
$src = ($dirname === '.') ? $filename : $dirname . '/' . $filename;
50-
}
56+
// build an array of SRI-related attributes
57+
$jsOptions = array(
58+
'integrity' => $jsIntegrity, // generated SRI hash
59+
'crossorigin' => c::get('plugin.kirby-sri.crossorigin', 'anonymous'), // user-defined 'crossorigin'
60+
);
61+
}
5162

52-
// build an array of SRI-related attributes
53-
$jsOptions = array(
54-
'integrity' => $jsIntegrity, // 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+
'src' => url($src)
66+
);
5867

59-
// build the array of HTML attributes
60-
$attr = array('src' => url($src));
68+
if (isset($jsOptions)) {
69+
$attr = array_merge($attr, $jsOptions);
70+
}
6171

62-
if(isset($jsOptions)) {
63-
$attr = array_merge($attr, $jsOptions);
64-
}
72+
if (is_array($async)) {
73+
// merge array with custom options
74+
$attr = array_merge($attr, $async);
75+
} elseif ($async === true) {
76+
// if there's no such array, just set async key
77+
$attr['async'] = true;
78+
}
6579

66-
if(is_array($async)) {
67-
// merge array with custom options
68-
$attr = array_merge($attr, $async);
69-
} else if($async === true) {
70-
// if there's no such array, just set async key
71-
$attr['async'] = true;
80+
// return the proper 'script' tag
81+
return html::tag('script', '', $attr);
7282
}
73-
74-
// return the proper 'script' tag
75-
return html::tag('script', '', $attr);
76-
}
7783
}

0 commit comments

Comments
 (0)