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

Last change on this file since 395 was 395, checked in by anonymous, 12 years ago
File size: 5.7 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-2010 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    // Include these style sheets.
34    var $_css_files = array('default' => array());
35
36    // CSS object parameters.
37    var $_params = array(
38        'character_set' => 'utf-8',
39        'cache_css' => false,
40        'strip_whitespace' => false,
41        'output_compression' => false,
42    );
43
44    /**
45     * Set (or overwrite existing) parameters by passing an array of new parameters.
46     *
47     * @access public
48     * @param  array    $params     Array of parameters (key => val pairs).
49     */
50    function setParam($params)
51    {
52        $app =& App::getInstance();
53
54        if (isset($params) && is_array($params)) {
55            // Merge new parameters with old overriding only those passed.
56            $this->_params = array_merge($this->_params, $params);
57        } else {
58            $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
59        }
60    }
61
62    /**
63     * Return the value of a parameter, if it exists.
64     *
65     * @access public
66     * @param string $param        Which parameter to return.
67     * @return mixed               Configured parameter value.
68     */
69    function getParam($param)
70    {
71        $app =& App::getInstance();
72
73        if (isset($this->_params[$param])) {
74            return $this->_params[$param];
75        } else {
76            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
77            return null;
78        }
79    }
80
81    /**
82     * Add a file-path to the array of files to include as CSS.
83     *
84     * @access  public
85     * @param   string  $file   Include path to css files.
86     * @param   mixed   $realms   Realm name string or array of realm names.
87     * @return  bool    True on success, false on failure.
88     */
89    function setFile($file, $realms='')
90    {
91        $app =& App::getInstance();
92
93        if (!is_array($realms)) {
94            $realms = array($realms);
95        }
96
97        if ($fp = fopen($file, 'r', true)) {
98            foreach ($realms as $realm) {
99                $realm = '' == $realm ? 'default' : $realm;
100                $this->_css_files[$realm][] = $file;
101            }
102            fclose($fp);
103            return true;
104        } else {
105            $app->logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__);
106            return false;
107        }
108    }
109
110    /**
111     * Output headers for CSS.
112     *
113     * @access  public
114     *
115     * @return  bool    False if no files have been set.
116     */
117    function headers($realm='')
118    {
119        $app =& App::getInstance();
120
121        $realm = '' == $realm ? 'default' : $realm;
122
123        if (empty($this->_css_files[$realm])) {
124            $app->logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__);
125            return false;
126        }
127
128        // Get time of latest modified file, including this class file.
129        $files_mtime = array();
130        foreach (array_merge($this->_css_files[$realm], array(__FILE__)) as $file) {
131            $files_mtime[] = statIncludePath($file, 'mtime');
132        }
133        sort($files_mtime, SORT_NUMERIC);
134        $latest_mtime = array_pop($files_mtime);
135
136        if ($this->getParam('output_compression') && extension_loaded('zlib')) {
137            ob_start('ob_gzhandler');
138        }
139
140        if ($this->getParam('cache_css')) {
141            header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $latest_mtime) . ' GMT');
142            header('Cache-Control: public, max-age=86400');
143            header('Pragma: public');
144        } else {
145            header('Expires: -1');
146            header('Pragma: no-cache');
147            header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
148        }
149        header('Content-Type: text/css; charset=' . $this->getParam('character_set'));
150    }
151
152    /**
153     * Include CSS files specified by setFile().
154     *
155     * @access  public
156     *
157     * @return  bool    False if no files have been set.
158     */
159    function output($realm='')
160    {
161        $realm = '' == $realm ? 'default' : $realm;
162
163        if (empty($this->_css_files[$realm])) {
164            $app->logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__);
165            return false;
166        }
167
168        foreach ($this->_css_files[$realm] as $file) {
169            if ($this->getParam('strip_whitespace')) {
170                // Strip whitespace and print file.
171                echo preg_replace('/[ \t\n\r]+/', ' ', file_get_contents($file, true));
172            } else {
173                // Include file as is.
174                include $file;
175            }
176        }
177
178        if ($this->getParam('output_compression') && extension_loaded('zlib')) {
179            ob_end_flush();
180        }
181    }
182}
183?>
Note: See TracBrowser for help on using the repository browser.