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

Commit

Permalink
Applying PSR2 coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
S1SYPHOS committed Mar 9, 2018
1 parent 7344ae6 commit ff0478f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 129 deletions.
32 changes: 16 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "s1syphos/kirby-sri",
"description": "Subresource integrity hashing & cache-busting static assets for Kirby",
"type": "kirby-plugin",
"license": "MIT",
"authors": [
{
"name": "Martin Folkers",
"homepage": "https://twobrain.io",
"role": "Project Author"
}
],
"require": {
"composer/installers": "~1.0"
}
}
{
"name": "s1syphos/kirby-sri",
"description": "Subresource integrity hashing & cache-busting static assets for Kirby",
"type": "kirby-plugin",
"license": "MIT",
"authors": [
{
"name": "Martin Folkers",
"homepage": "https://twobrain.io",
"role": "Project Author"
}
],
"require": {
"composer/installers": "~1.0"
}
}
120 changes: 62 additions & 58 deletions core/css.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,78 @@
use c;
use html;

class CSS extends \Kirby\Component\CSS {
class CSS extends \Kirby\Component\CSS
{

/**
* Builds the html link tag for the given css file
*
* @param string $url
* @param string|array $media Either a media string or an array of attributes
* @return string
*/
/**
* Builds the html link tag for the given css file
*
* @param string $url
* @param string|array $media Either a media string or an array of attributes
* @return string
*/

public function tag($url, $media = null) {
public function tag($url, $media = null)
{
if (is_array($url)) {
$css = array();
foreach ($url as $u) {
$css[] = $this->tag($u, $media);
}
return implode(PHP_EOL, $css) . PHP_EOL;
}

if(is_array($url)) {
$css = array();
foreach($url as $u) $css[] = $this->tag($u, $media);
return implode(PHP_EOL, $css) . PHP_EOL;
}
// auto template css files
if ($url == '@auto') {
$file = $this->kirby->site()->page()->template() . '.css';
$root = $this->kirby->roots()->autocss() . DS . $file;
$url = $this->kirby->urls()->autocss() . '/' . $file;
if (file_exists($root)) {
$url = preg_replace('#^' . $this->kirby->urls()->index() . '/#', null, $url);
}
}

// auto template css files
if($url == '@auto') {
$file = $this->kirby->site()->page()->template() . '.css';
$root = $this->kirby->roots()->autocss() . DS . $file;
$url = $this->kirby->urls()->autocss() . '/' . $file;
if(!file_exists($root)) return false;
$url = preg_replace('#^' . $this->kirby->urls()->index() . '/#', null, $url);
}
$url = ltrim($url, '/');

$url = ltrim($url, '/');
if (file_exists($url)) {
// generate sri hash for css files
$cssInput = (new Asset($url))->content();
$cssIntegrity = sri_checksum($cssInput);

if(file_exists($url)) {
// generate sri hash for css files
$cssInput = (new Asset($url))->content();
$cssIntegrity = sri_checksum($cssInput);
if (c::get('fingerprinting')) {
// add timestamp for cache-busting
$modified = filemtime($url);
$filename = f::name($url) . '.' . $modified . '.' . f::extension($url);
$dirname = f::dirname($url);
$url = ($dirname === '.') ? $filename : $dirname . '/' . $filename;
}

if(c::get('fingerprinting')) {
// add timestamp for cache-busting
$modified = filemtime($url);
$filename = f::name($url) . '.' . $modified . '.' . f::extension($url);
$dirname = f::dirname($url);
$url = ($dirname === '.') ? $filename : $dirname . '/' . $filename;
}
// build an array of SRI-related attributes
$cssOptions = array(
'integrity' => $cssIntegrity, // generated SRI hash
'crossorigin' => c::get('plugin.kirby-sri.crossorigin', 'anonymous'), // user-defined 'crossorigin'
);
}

// build an array of SRI-related attributes
$cssOptions = array(
'integrity' => $cssIntegrity, // generated SRI hash
'crossorigin' => c::get('plugin.kirby-sri.crossorigin', 'anonymous'), // user-defined 'crossorigin' attribute
);
}
// build the array of HTML attributes
$attr = array(
'rel' => 'stylesheet',
'href' => url($url)
);

// build the array of HTML attributes
$attr = array(
'rel' => 'stylesheet',
'href' => url($url)
);
if (isset($cssOptions)) {
$attr = array_merge($attr, $cssOptions);
}

if(isset($cssOptions)) {
$attr = array_merge($attr, $cssOptions);
}
if (is_array($media)) {
// merge array with custom options
$attr = array_merge($attr, $media);
} elseif (is_string($media)) {
// if there's no such array, just set media key
$attr['media'] = $media;
}

if(is_array($media)) {
// merge array with custom options
$attr = array_merge($attr, $media);
} else if(is_string($media)) {
// if there's no such array, just set media key
$attr['media'] = $media;
// return the proper 'link' tag
return html::tag('link', null, $attr);
}

// return the proper 'link' tag
return html::tag('link', null, $attr);
}
}
116 changes: 61 additions & 55 deletions core/js.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,77 @@
use c;
use html;

class JS extends \Kirby\Component\JS {
class JS extends \Kirby\Component\JS
{

/**
* Builds the html script tag for the given javascript file
*
* @param string $src
* @param boolean|array $async Either true for the async attribute or an array of attributes
* @return string
*/
/**
* Builds the html script tag for the given javascript file
*
* @param string $src
* @param boolean|array $async Either true for the async attribute or an array of attributes
* @return string
*/

public function tag($src, $async = false) {
public function tag($src, $async = false)
{
if (is_array($src)) {
$js = array();
foreach ($src as $s) {
$js[] = $this->tag($s, $async);
}
return implode(PHP_EOL, $js) . PHP_EOL;
}

if(is_array($src)) {
$js = array();
foreach($src as $s) $js[] = $this->tag($s, $async);
return implode(PHP_EOL, $js) . PHP_EOL;
}
// auto template css files
if ($src == '@auto') {
$file = $this->kirby->site()->page()->template() . '.js';
$root = $this->kirby->roots()->autojs() . DS . $file;
$src = $this->kirby->urls()->autojs() . '/' . $file;
if (file_exists($root)) {
$src = preg_replace('#^' . $this->kirby->urls()->index() . '/#', null, $url);
}
}

// auto template css files
if($src == '@auto') {
$file = $this->kirby->site()->page()->template() . '.js';
$root = $this->kirby->roots()->autojs() . DS . $file;
$src = $this->kirby->urls()->autojs() . '/' . $file;
if(!file_exists($root)) return false;
$src = preg_replace('#^' . $this->kirby->urls()->index() . '/#', null, $src);
}
$src = ltrim($src, '/');

$src = ltrim($src, '/');
if (file_exists($src)) {
// generate sri hash for css files
$jsInput = (new Asset($src))->content();
$jsIntegrity = sri_checksum($jsInput);

if(file_exists($src)) {
// generate sri hash for css files
$jsInput = (new Asset($src))->content();
$jsIntegrity = sri_checksum($jsInput);
if (c::get('fingerprinting')) {
// add timestamp for cache-busting
$modified = filemtime($src);
$filename = f::name($src) . '.' . $modified . '.' . f::extension($src);
$dirname = f::dirname($src);
$src = ($dirname === '.') ? $filename : $dirname . '/' . $filename;
}

if(c::get('fingerprinting')) {
// add timestamp for cache-busting
$modified = filemtime($src);
$filename = f::name($src) . '.' . $modified . '.' . f::extension($src);
$dirname = f::dirname($src);
$src = ($dirname === '.') ? $filename : $dirname . '/' . $filename;
}
// build an array of SRI-related attributes
$jsOptions = array(
'integrity' => $jsIntegrity, // generated SRI hash
'crossorigin' => c::get('plugin.kirby-sri.crossorigin', 'anonymous'), // user-defined 'crossorigin'
);
}

// build an array of SRI-related attributes
$jsOptions = array(
'integrity' => $jsIntegrity, // generated SRI hash
'crossorigin' => c::get('plugin.kirby-sri.crossorigin', 'anonymous'), // user-defined 'crossorigin' attribute
);
}
// build the array of HTML attributes
$attr = array(
'src' => url($src)
);

// build the array of HTML attributes
$attr = array('src' => url($src));
if (isset($jsOptions)) {
$attr = array_merge($attr, $jsOptions);
}

if(isset($jsOptions)) {
$attr = array_merge($attr, $jsOptions);
}
if (is_array($async)) {
// merge array with custom options
$attr = array_merge($attr, $async);
} elseif ($async === true) {
// if there's no such array, just set async key
$attr['async'] = true;
}

if(is_array($async)) {
// merge array with custom options
$attr = array_merge($attr, $async);
} else if($async === true) {
// if there's no such array, just set async key
$attr['async'] = true;
// return the proper 'script' tag
return html::tag('script', '', $attr);
}

// return the proper 'script' tag
return html::tag('script', '', $attr);
}
}

0 comments on commit ff0478f

Please sign in to comment.