/** * This method allow to generate the cache file with all data for SVG file * * @access public * @static * @throws Exception If the cache can't be write, an exception is throwed * @todo When PHP 5.3 is ok in most hosting, use GlobIterator and not DirectoryIterator and don't do 2 foreach and ksort */ public static function setCache() { self::init(); $directory = new DirectoryIterator(Config::getStripFolder()); $file = array(); // get all svg file foreach ($directory as $file) { if ($file->isDot() === true || $file->isFile() === false) { continue; } $extension = pathinfo($file->getPathname(), PATHINFO_EXTENSION); if (strtolower($extension) !== 'svg') { continue; } $files[$file->getBasename()] = $file->getMTime(); } // We must use ksort for sort the array by filename (the DirectoryIterator have his self sort :-)), in PHP 5.3, we can use GlobIterator directly ksort($files); $cache = '<?php $cache = array('; foreach ($files as $name => $time) { $cache .= '"' . str_replace('"', '\\"', $name) . '" => ' . $time . ', '; } $cache .= ');'; // if file wrinting fail, throw an Exception if (file_put_contents(self::$filename, $cache) === false) { throw new Exception('Impossible to write in the cache file : "' . self::$filename . '"'); } }
/** * Setter for the filename * @param string $file The filename * @throws Exception If the file doesn't exist an exception is throwed * @access public */ protected function setFilename($file) { if (file_exists(Config::getStripFolder() . '/' . $file) === false) { throw new Exception('The filename "' . $file . '" isn\'t a valid file!'); } $this->filename = $file; $this->setSourceSize(); }