]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/MinifyPlugin.php
Merge branch 'inblob' of git@gitorious.org:~evan/statusnet/evans-mainline into inblob
[quix0rs-gnu-social.git] / plugins / Minify / MinifyPlugin.php
1 <?php
2 /*
3 StatusNet Plugin: 0.9
4 Plugin Name: Minify
5 Description: Minifies resources (Javascript and CSS)
6 Version: 0.1
7 Author: Craig Andrews <candrews@integralblue.com>
8 Author URI: http://candrews.integralblue.com/
9 */
10
11 /*
12  * StatusNet - the distributed open-source microblogging tool
13  * Copyright (C) 2009, StatusNet, Inc.
14  *
15  * This program is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU Affero General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU Affero General Public License for more details.
24  *
25  * You should have received a copy of the GNU Affero General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  */
28
29 /**
30  * @package MinifyPlugin
31  * @maintainer Craig Andrews <candrews@integralblue.com>
32  */
33
34 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
35
36 // We bundle the minify library...
37 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/minify/min/lib');
38
39 class MinifyPlugin extends Plugin
40 {
41     private $minifyInlineJs = true;
42     private $minifyInlineCss = true;
43
44     const cacheKey = 'minify';
45
46     /**
47      * Add Minification related paths to the router table
48      *
49      * Hook for RouterInitialized event.
50      *
51      * @return boolean hook return
52      */
53
54     function onStartInitializeRouter($m)
55     {
56         $m->connect('main/min',
57                     array('action' => 'minify'));
58         return true;
59     }
60
61     function onAutoload($cls)
62     {
63         switch ($cls)
64         {
65          case 'MinifyAction':
66             require_once(INSTALLDIR.'/plugins/Minify/' . strtolower(mb_substr($cls, 0, -6)) . '.php');
67             return false;
68          default:
69             return true;
70         }
71     }
72
73     function onLoginAction($action, &$login)
74     {
75         switch ($action)
76         {
77          case 'minify':
78             $login = true;
79             return false;
80          default:
81             return true;
82         }
83     }
84
85     function onStartScriptElement($action,&$src,&$type) {
86         $url = parse_url($src);
87         if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
88         {
89             $src = $this->minifyUrl($src);
90         }
91     }
92
93     function onStartCssLinkElement($action,&$src,&$theme,&$media) {
94         $allowThemeMinification =
95             is_null(common_config('theme', 'dir'))
96             && is_null(common_config('theme', 'path'))
97             && is_null(common_config('theme', 'server'));
98         $url = parse_url($src);
99         if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment))
100         {
101             if(!isset($theme)) {
102                 $theme = common_config('site', 'theme');
103             }
104             if($allowThemeMinification && file_exists(INSTALLDIR.'/local/theme/'.$theme.'/'.$src)) {
105                 $src = $this->minifyUrl('local/theme/'.$theme.'/'.$src);
106             } else if($allowThemeMinification && file_exists(INSTALLDIR.'/theme/'.$theme.'/'.$src)) {
107                 $src = $this->minifyUrl('theme/'.$theme.'/'.$src);
108             }else if(file_exists(INSTALLDIR.'/'.$src)){
109                 $src = $this->minifyUrl($src);
110             }
111         }
112     }
113
114     function onStartInlineScriptElement($action,&$code,&$type)
115     {
116         if($this->minifyInlineJs && $type=='text/javascript'){
117             $c = common_memcache();
118             if (!empty($c)) {
119                 $cacheKey = common_cache_key(self::cacheKey . ':' . crc32($code));
120                 $out = $c->get($cacheKey);
121             }
122             if(empty($out)) {
123                 $out = $this->minifyJs($code);
124             }
125             if (!empty($c)) {
126                 $c->set($cacheKey, $out);
127             }
128             if(!empty($out)) {
129                 $code = $out;
130             }
131         }
132     }
133
134     function onStartStyleElement($action,&$code,&$type,&$media)
135     {
136         if($this->minifyInlineCss && $type=='text/css'){
137             $c = common_memcache();
138             if (!empty($c)) {
139                 $cacheKey = common_cache_key(self::cacheKey . ':' . crc32($code));
140                 $out = $c->get($cacheKey);
141             }
142             if(empty($out)) {
143                 $out = $this->minifyCss($code);
144             }
145             if (!empty($c)) {
146                 $c->set($cacheKey, $out);
147             }
148             if(!empty($out)) {
149                 $code = $out;
150             }
151         }
152     }
153
154     function minifyUrl($src) {
155         return common_local_url('minify',null,array('f' => $src ,v => STATUSNET_VERSION));
156     }
157
158     static function minifyJs($code) {
159         require_once('JSMin.php');
160         return JSMin::minify($code);
161     }
162
163     static function minifyCss($code, $options = array()) {
164         require_once('Minify/CSS.php');
165         return Minify_CSS::minify($code,$options);
166     }
167 }
168