source: trunk/lib/CSS.inc.php @ 502

Last change on this file since 502 was 502, checked in by anonymous, 9 years ago

Many minor fixes during pulso development

File size: 6.0 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2012 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
24 * CSS.inc.css
25 *
26 * Dynamically outputs cached CSS data.
27 *
28 * @author  Quinn Comendant <quinn@strangecode.com>
29 * @version 1.2
30 */
31class CSS
32{
33
34    // Include these style sheets.
35    protected $_css_files = array('default' => array());
36
37    // CSS object parameters.
38    protected $_params = array(
39        'character_set' => 'utf-8',
40        'cache_css' => false,
41        'strip_whitespace' => false,
42        'output_compression' => false,
43    );
44
45    /**
46     * Set (or overwrite existing) parameters by passing an array of new parameters.
47     *
48     * @access public
49     * @param  array    $params     Array of parameters (key => val pairs).
50     */
51    public function setParam($params)
52    {
53        $app =& App::getInstance();
54
55        if (isset($params) && is_array($params)) {
56            // Merge new parameters with old overriding only those passed.
57            $this->_params = array_merge($this->_params, $params);
58        } else {
59            $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
60        }
61    }
62
63    /**
64     * Return the value of a parameter, if it exists.
65     *
66     * @access public
67     * @param string $param        Which parameter to return.
68     * @return mixed               Configured parameter value.
69     */
70    public function getParam($param)
71    {
72        $app =& App::getInstance();
73
74        if (array_key_exists($param, $this->_params)) {
75            return $this->_params[$param];
76        } else {
77            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
78            return null;
79        }
80    }
81
82    /**
83     * Add a file-path to the array of files to include as CSS.
84     *
85     * @access  public
86     * @param   string  $file   Include path to css files.
87     * @param   mixed   $realms   Realm name string or array of realm names.
88     * @return  bool    True on success, false on failure.
89     */
90    public function setFile($file, $realms='')
91    {
92        $app =& App::getInstance();
93
94        if (!is_array($realms)) {
95            $realms = array($realms);
96        }
97
98        if ($fp = fopen($file, 'r', true)) {
99            foreach ($realms as $realm) {
100                $realm = '' == $realm ? 'default' : $realm;
101                $this->_css_files[$realm][] = $file;
102            }
103            fclose($fp);
104            return true;
105        } else {
106            $app->logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__);
107            return false;
108        }
109    }
110
111    /**
112     * Output headers for CSS.
113     *
114     * @access  public
115     *
116     * @return  bool    False if no files have been set.
117     */
118    public function headers($realm='')
119    {
120        $app =& App::getInstance();
121
122        $realm = '' == $realm ? 'default' : $realm;
123
124        if (empty($this->_css_files[$realm])) {
125            $app->logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__);
126            return false;
127        }
128
129        // Get time of latest modified file, including this class file.
130        $files_mtime = array();
131        foreach (array_merge($this->_css_files[$realm], array(__FILE__)) as $file) {
132            $files_mtime[] = statIncludePath($file, 'mtime');
133        }
134        sort($files_mtime, SORT_NUMERIC);
135        $latest_mtime = array_pop($files_mtime);
136
137        if ($this->getParam('output_compression') && extension_loaded('zlib')) {
138            ob_start('ob_gzhandler');
139        }
140
141        if ($this->getParam('cache_css')) {
142            header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $latest_mtime) . ' GMT');
143            header('Cache-Control: public, max-age=86400');
144            header('Pragma: public');
145        } else {
146            header('Expires: -1');
147            header('Pragma: no-cache');
148            header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
149        }
150        header('Content-Type: text/css; charset=' . $this->getParam('character_set'));
151    }
152
153    /**
154     * Include CSS files specified by setFile().
155     *
156     * @access  public
157     *
158     * @return  bool    False if no files have been set.
159     */
160    public function output($realm='')
161    {
162        $app =& App::getInstance();
163
164        $realm = '' == $realm ? 'default' : $realm;
165
166        if (empty($this->_css_files[$realm])) {
167            $app->logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__);
168            return false;
169        }
170
171        foreach ($this->_css_files[$realm] as $file) {
172            if ($this->getParam('strip_whitespace')) {
173                // Strip whitespace and print file.
174                echo preg_replace(
175                    array('!/\*.*?\*/!s', '/[\n\r]+/', '/([;:])\s+/m', '/\s*}[ \t]*/', '/\s*{\s*/', '/[ \t\n\r]*,[ \t\n\r]*/', '/^\s+/'),
176                    array('', "\n", '$1', '}', '{', ',', ''), file_get_contents($file, true)
177                );
178            } else {
179                // Include file as is.
180                include $file;
181            }
182        }
183
184        if ($this->getParam('output_compression') && extension_loaded('zlib')) {
185            ob_end_flush();
186        }
187    }
188}
Note: See TracBrowser for help on using the repository browser.