source: tags/2.1.5/lib/CSS.inc.php

Last change on this file was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

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