diff --git a/readme.md b/readme.md index bda0286..f89f85e 100644 --- a/readme.md +++ b/readme.md @@ -7,7 +7,7 @@ A collection of uefull helpers for the Yii2 framework Install with composer ~~~php -compsoer install adeattwood/yii-helpers +compsoer install adeattwood/yii-addon ~~~ ## Class docs diff --git a/src/filters/PageMin.php b/src/filters/PageMin.php new file mode 100644 index 0000000..935d75d --- /dev/null +++ b/src/filters/PageMin.php @@ -0,0 +1,141 @@ +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, '') !== false) { + $input = preg_replace_callback('##is', function($matches) { + return ''; + }, $input); + } + if(strpos($input, '') !== false) { + $input = preg_replace_callback('##is', function($matches) { + return ''; + }, $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', + '#( ) (?![<\s])#', + '#(?<=\>)( )(?=\<)#', + '#\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', + '#(\/\*(?>.*?\*\/))|(?.*?\*\/))|(\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.,;]|[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 + ); + } + +} diff --git a/src/ArrayHelper.php b/src/helpers/ArrayHelper.php similarity index 96% rename from src/ArrayHelper.php rename to src/helpers/ArrayHelper.php index 559d2ab..6755a7b 100644 --- a/src/ArrayHelper.php +++ b/src/helpers/ArrayHelper.php @@ -1,12 +1,13 @@ '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(); + + } + +} diff --git a/src/web/View.php b/src/web/View.php new file mode 100644 index 0000000..1ec42ea --- /dev/null +++ b/src/web/View.php @@ -0,0 +1,129 @@ +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 '