move over to addon

This commit is contained in:
Ade Attwood 2017-09-15 21:06:31 +01:00
parent f015e60d15
commit 6d2b1b569c
12 changed files with 345 additions and 13 deletions

View file

@ -7,7 +7,7 @@ A collection of uefull helpers for the Yii2 framework
Install with composer Install with composer
~~~php ~~~php
compsoer install adeattwood/yii-helpers compsoer install adeattwood/yii-addon
~~~ ~~~
## Class docs ## Class docs

141
src/filters/PageMin.php Normal file
View file

@ -0,0 +1,141 @@
<?php
namespace yiiaddon\filters;
use Yii;
use yii\di\Instance;
use yii\web\Response;
class PageMin extends \yii\base\ActionFilter
{
public $enabled;
public function beforeAction($action)
{
if (!$this->enabled) {
return true;
}
$response = Yii::$app->getResponse();
$response->on(Response::EVENT_BEFORE_SEND, [$this, 'pageMin']);
return true;
}
public function pageMin($event)
{
Yii::$app->response->data = $this->minifyHtml($event->sender->data);
}
public function minifyHtml( $input )
{
if(trim($input) === "") return $input;
$input = preg_replace_callback('#<([^\/\s<>!]+)(?:\s+([^<>]*?)\s*|\s*)(\/?)>#s', function($matches) {
return '<' . $matches[1] . preg_replace('#([^\s=]+)(\=([\'"]?)(.*?)\3)?(\s+|$)#s', ' $1$2', $matches[2]) . $matches[3] . '>';
}, str_replace("\r", "", $input));
if(strpos($input, ' style=') !== false) {
$input = preg_replace_callback('#<([^<]+?)\s+style=([\'"])(.*?)\2(?=[\/\s>])#s', function($matches) {
return '<' . $matches[1] . ' style=' . $matches[2] . $this->minifyCss($matches[3]) . $matches[2];
}, $input);
}
if(strpos($input, '</style>') !== false) {
$input = preg_replace_callback('#<style(.*?)>(.*?)</style>#is', function($matches) {
return '<style' . $matches[1] .'>'. $this->minifyCss($matches[2]) . '</style>';
}, $input);
}
if(strpos($input, '</script>') !== false) {
$input = preg_replace_callback('#<script(.*?)>(.*?)</script>#is', function($matches) {
return '<script' . $matches[1] .'>'. $this->minifyJs($matches[2]) . '</script>';
}, $input);
}
return preg_replace(
[
'#<(img|input)(>| .*?>)#s',
'#(<!--.*?-->)|(>)(?:\n*|\s{2,})(<)|^\s*|\s*$#s',
'#(<!--.*?-->)|(?<!\>)\s+(<\/.*?>)|(<[^\/]*?>)\s+(?!\<)#s',
'#(<!--.*?-->)|(<[^\/]*?>)\s+(<[^\/]*?>)|(<\/.*?>)\s+(<\/.*?>)#s',
'#(<!--.*?-->)|(<\/.*?>)\s+(\s)(?!\<)|(?<!\>)\s+(\s)(<[^\/]*?\/?>)|(<[^\/]*?\/?>)\s+(\s)(?!\<)#s',
'#(<!--.*?-->)|(<[^\/]*?>)\s+(<\/.*?>)#s',
'#<(img|input)(>| .*?>)<\/\1>#s',
'#(&nbsp;)&nbsp;(?![<\s])#',
'#(?<=\>)(&nbsp;)(?=\<)#',
'#\s*<!--(?!\[if\s).*?-->\s*|(?<!\>)\n+(?=\<[^!])#s'
],
[
'<$1$2</$1>',
'$1$2$3',
'$1$2$3',
'$1$2$3$4$5',
'$1$2$3$4$5$6$7',
'$1$2$3',
'<$1$2',
'$1 ',
'$1',
''
],
$input
);
}
public function minifyCss($input)
{
if (trim($input) === '') {
return $input;
}
return preg_replace(
[
'#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\/\*(?!\!)(?>.*?\*\/)|^\s*|\s*$#s',
'#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/))|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
'#(?<=[\s:])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#si',
'#:(0\s+0|0\s+0\s+0\s+0)(?=[;\}]|\!important)#i',
'#(background-position):0(?=[;\}])#si',
'#(?<=[\s:,\-])0+\.(\d+)#s',
'#(\/\*(?>.*?\*\/))|(?<!content\:)([\'"])([a-z_][a-z0-9\-_]*?)\2(?=[\s\{\}\];,])#si',
'#(\/\*(?>.*?\*\/))|(\burl\()([\'"])([^\s]+?)\3(\))#si',
'#(?<=[\s:,\-]\#)([a-f0-6]+)\1([a-f0-6]+)\2([a-f0-6]+)\3#i',
'#(?<=[\{;])(border|outline):none(?=[;\}\!])#',
'#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#s'
],
[
'$1',
'$1$2$3$4$5$6$7',
'$1',
':0',
'$1:0 0',
'.$1',
'$1$3',
'$1$2$4$5',
'$1$2$3',
'$1:0',
'$1$2'
],
$input
);
}
public function minifyJs($input)
{
if (trim($input) === '') {
return $input;
}
return preg_replace(
[
'#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*(?=[\n\r]|$)|^\s*|\s*$#',
'#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s',
'#;+\}#',
'#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i',
'#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i'
],
[
'$1',
'$1$2',
'}',
'$1$3',
'$1.$3'
],
$input
);
}
}

View file

@ -1,12 +1,13 @@
<?php <?php
namespace adeattwood\helpers; namespace yiiaddon\helpers;
use Yii; use Yii;
use adeattwood\helpers\ArrayObject;
use adeattwood\helpers\Enum;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yiiaddon\helpers\ArrayObject;
use yiiaddon\helpers\Enum;
/** /**
* @category PHP * @category PHP
* @package adeattwood\yii-helpers * @package adeattwood\yii-helpers

View file

@ -1,10 +1,11 @@
<?php <?php
namespace adeattwood\helpers; namespace yiiaddon\helpers;
use Yii; use Yii;
use yii\helpers\Html; use yii\helpers\Html;
use adeattwood\helpers\Enum;
use yiiaddon\helpers\Enum;
/** /**
* @category PHP * @category PHP

View file

@ -1,6 +1,6 @@
<?php <?php
namespace adeattwood\helpers; namespace yiiaddon\helpers;
/** /**
* @category PHP * @category PHP

60
src/web/RedirectRule.php Normal file
View file

@ -0,0 +1,60 @@
<?php
namespace yiiaddon\web;
use Yii;
use yii\helpers\Url;
/**
* [
* 'class' => 'yiiaddon\web\RedirectRule',
*
* 'route' => '/about',
*
* 'permanents' => [
* 'deprecated-about',
* 'an-older-deprecated-about'
* ],
*
* 'temporaries' => [
* 'under-construction-about',
* ]
* ]
*/
class RedirectRule extends \yii\web\UrlRule
{
public $permanents = [];
public $temporaries = [];
public function init()
{
if ($this->pattern === null) {
$this->pattern = false;
}
if ($this->name === null) {
$this->name = $this->route;
}
parent::init();
}
public function parseRequest($manager, $request)
{
$pathInfo = $request->getPathInfo();
if(in_array($pathInfo, $this->permanents)) {
$request->setPathInfo($this->name);
Yii::$app->response->redirect($this->name, 301);
} else if(in_array($pathInfo, $this->temporaries)) {
$request->setPathInfo($this->name);
Yii::$app->response->redirect($this->name, 302);
}
parent::parseRequest($manager, $request);
Yii::$app->end();
}
}

129
src/web/View.php Normal file
View file

@ -0,0 +1,129 @@
<?php
namespace yiiaddon\web;
use Yii;
use yii\helpers\Url;
class View extends \yii\web\View
{
public $facebook_og = true;
public $twitter_card = true;
public $description;
public $url;
public $keywords;
public $image;
public $twitterTag;
public $authorName;
public $authorTwitterTag;
public $viewportContent = 'width=device-width, initial-scale=1';
public $googleAnalyticsCode;
public $googleAnalyticsPosition;
public $googleAnalyticsFile = 'https://www.google-analytics.com/analytics.js';
public $googleAnalyticsVar = 'ga';
protected $googleAnalyticsSet = [];
public function init()
{
if (!$this->googleAnalyticsPosition) {
$this->googleAnalyticsPosition = self::POS_END;
}
return parent::init();
}
public function googleAnalyticsSet($key, $value)
{
$googleAnalyticsSet[$key] = $value;
}
public function renderHeadHtml()
{
$metas = [
'charset' => ['charset' => Yii::$app->charset],
'viewport' => ['name' => 'viewport', 'content' => $this->viewportContent],
'http-equiv' => ['http-equiv' => 'X-UA-Compatible', 'content' => 'IE=edge'],
'description' => ['name' => 'description', 'content' => $this->description],
'keywords' => $this->keywords ? ['name' => 'keywords', 'content' => $this->keywords] : false,
'author' => $this->authorName ? ['name' => 'author', 'content' => $this->authorName] : false
];
$links = [
'canonical' => ['rel' => 'canonical', 'href' => Url::to($this->url, true)]
];
if ($this->facebook_og) {
$metas = array_merge($metas, $this->registerFacebookOg());
}
if ($this->twitter_card) {
$metas = array_merge($metas, $this->registerTwitterCard());
}
foreach ($metas as $meta => $content) {
if (!isset($this->metaTags[$meta]) && $content) {
$this->registerMetaTag($content, $meta);
}
}
foreach ($links as $link => $content) {
if (!isset($this->metaTags[$link]) && $content) {
$this->registerLinkTag($content, $link);
}
}
if ($this->googleAnalyticsCode) {
$this->registerGoogleAnalytics();
}
return '<title>' . $this->title . '</title>' . parent::renderHeadHtml();
}
public function registerGoogleAnalytics()
{
$js = "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','$this->googleAnalyticsFile','$this->googleAnalyticsVar');";
$js .= "$this->googleAnalyticsVar('create', '$this->googleAnalyticsCode', 'auto');";
$js .= "$this->googleAnalyticsVar('send', 'pageview');";
foreach ($this->googleAnalyticsSet as $key => $value) {
$js .= "$this->googleAnalyticsVar('set', '$key', '$value');";
}
$this->registerJS($js, $this->googleAnalyticsPosition);
}
public function registerFacebookOg()
{
return [
'og:locale' => ['property' => 'og:locale', 'content' => 'en_GB'],
'og:type' => ['property' => 'og:type', 'content' => 'website'],
'og:title' => ['property' => 'og:title', 'content' => $this->title],
'og:description' => ['property' => 'og:description', 'content' => $this->description],
'og:url' => ['property' => 'og:url', 'content' => Url::to($this->url, true)],
'og:site_name' => ['property' => 'og:site_name', 'content' => Yii::$app->name],
'og:image' => $this->image ? ['property' => 'og:image', 'content' => Url::to($this->image, true)] : false,
];
}
public function registerTwitterCard()
{
return [
'twitter:card' => ['name' => 'twitter:card', 'content' => 'summary'],
'twitter:description' => ['name' => 'twitter:description', 'content' => $this->description],
'twitter:title' => ['name' => 'twitter:title', 'content' => $this->title],
'twitter:url' => ['name' => 'twitter:url', 'content' => Url::to($this->url, true)],
'twitter:site' => $this->twitterTag ? ['name' => 'twitter:site', 'content' => $this->twitterTag] : false,
'twitter:image' => $this->image ? ['name' => 'twitter:image', 'content' => Url::to($this->image, true)] : false,
'twitter:creator' => $this->authorTwitterTag ? ['name' => 'twitter:creator', 'content' => $this->authorTwitterTag] : false,
];
}
}

View file

@ -1,6 +1,6 @@
<?php <?php
namespace adeattwood\helpers\tests\_data; namespace yiiaddon\tests\_data;
/** /**
* @category PHP * @category PHP

View file

@ -1,7 +1,7 @@
<?php <?php
use adeattwood\helpers\ArrayHelper; use yiiaddon\ArrayHelper;
use adeattwood\helpers\tests\_data\ModelOne; use yiiaddon\tests\_data\ModelOne;
/** /**
* @category PHP * @category PHP

View file

@ -1,6 +1,6 @@
<?php <?php
use adeattwood\helpers\ArrayObject; use yiiaddon\ArrayObject;
/** /**
* @category PHP * @category PHP

View file

@ -1,6 +1,6 @@
<?php <?php
use adeattwood\helpers\Enum; use yiiaddon\Enum;
/** /**
* @category PHP * @category PHP