Classes

File io/optimization/CSSCacheManager.class.php

File io/optimization/CSSCacheManager.class.php

 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 
<?php
/**
 * @package     IO
 * @subpackage  Optimization
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Kevin MASSY <reidlos@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 02 14
 * @since       PHPBoost 3.0 - 2011 03 29
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

class CSSCacheManager
{
    private $css_optimizer;
    private $cache_file_location = '';

    public function __construct()
    {
        $this->css_optimizer = new CSSFileOptimizer();
    }

    /* This function return address is stored in the file where the css sent to the function */
    public static function get_css_path($files)
    {
        if (!empty($files))
        {
            $template_folder = new Folder(PATH_TO_ROOT . '/cache/css/' . AppContext::get_current_user()->get_theme());
            if (!$template_folder->exists())
                mkdir(PATH_TO_ROOT . '/cache/css/' . AppContext::get_current_user()->get_theme());

            if (is_array($files))
            {
                $cache_file_location = '/cache/css/' . AppContext::get_current_user()->get_theme() . '/css-cache-'. md5(implode(';', $files)) .'.css';
            }
            else
            {
                $files = str_replace('{THEME}', AppContext::get_current_user()->get_theme(), $files);
                $cache_file_location = '/cache/css/' . AppContext::get_current_user()->get_theme() . '/css-cache-'. md5($files) .'.css';
                $files = explode(';', $files);
            }

            $css_cache = new CSSCacheManager();
            $css_cache->set_files($files);
            $css_cache->set_cache_file_location(PATH_TO_ROOT . $cache_file_location);
            $css_cache->execute(CSSCacheConfig::load()->get_optimization_level());

            return TPL_PATH_TO_ROOT . $cache_file_location;
        }
    }

    public function set_files(Array $files)
    {
        foreach ($files as $file)
        {
            $this->css_optimizer->add_file(PATH_TO_ROOT . $file);
        }
    }

    public function set_cache_file_location($location)
    {
        $this->cache_file_location = $location;
    }

    public function execute($intensity = CSSFileOptimizer::LOW_OPTIMIZATION)
    {
        if (!file_exists($this->cache_file_location))
        {
            $this->force_regenerate_cache($intensity);
        }
        else
        {
            $files = $this->css_optimizer->get_files();
            $cache_file_time = filemtime($this->cache_file_location);
            foreach ($files as $file)
            {
                if (filemtime($file) > $cache_file_time)
                {
                    $this->force_regenerate_cache($intensity);
                    break;
                }
            }
        }
    }

    public function get_cache_file_location()
    {
        return $this->cache_file_location;
    }

    public function force_regenerate_cache($intensity)
    {
        $this->css_optimizer->optimize($intensity);
        $this->css_optimizer->export_to_file($this->cache_file_location);
    }
}
?>