Source of file FlushCache.php
Size: 8,291 Bytes - Last Modified: 2021-12-23T10:34:07+00:00
/var/www/docs.ssmods.com/process/src/code/commands/FlushCache.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 | <?php namespace SilverStripe\Sakemore\Commands; use DataExtension; use ReflectionMethod; use SS_TemplateManifest; use SS_ClassManifest; use Object; use SSViewer; use Zend_Translate; use SS_ConfigManifest; use Requirements; /** * This class gives the ability to flush any of Silverstripe's major caches. * * To extend functionality in this class */ class FlushCacheRole extends DataExtension { const CMD_FLUSHCACHE = 'clear'; /** * Tell sake more about this command. */ public function commands(&$list) { $list[self::CMD_FLUSHCACHE] = array($this, 'flushCache'); } /** * Gives sake a brief for the help section. */ public function help_brief(&$details) { $details[self::CMD_FLUSHCACHE] = "Flush's one or more of Silverstripe's caches. Expects type of cache as parameter"; } /** * Gives sake a list of the parameters used. */ public function help_parameters(&$details) { // Get the list of commands. $commands = $this->availableCommands(); $details[self::CMD_FLUSHCACHE] = array( 'Type - A choice of: all, ' . implode(', ', array_keys($commands)), ); } /** * Gives sake a list of examples of how to use this command. */ public function help_examples(&$examples) { // Add the generic 'all' command. $examples[self::CMD_FLUSHCACHE] = array( self::CMD_FLUSHCACHE . ' all - Clears all the caches', ); // Add the rest of the commands. $commands = $this->availableCommands(); foreach ($commands as $command => $specifics) { $examples[self::CMD_FLUSHCACHE][] = sprintf( '%s %s - %s', self::CMD_FLUSHCACHE, $command, $specifics['description'] ); } } /** * Gets a list of the flush commands avilable. * * This can be extended with 'flushCache_commands' which alters the command set. * The data is structed: array( * '[name of cache]' => array( * 'description' => '[a one-line description of the parameter]', * 'reference' => array([object], '[method name]'), * ), * ); */ protected function availableCommands() { // Prepares the list of build-in cache flush commands. $existing = array( 'image' => array( 'description' => 'Finds cached images and deletes them', 'reference' => array($this, 'flushImage'), ), 'template' => array( 'description' => 'Clears cached templates', 'reference' => array($this, 'flushTemplate'), ), 'language' => array( 'description' => 'Finds cached languages from Zend translate and clears them', 'reference' => array($this, 'flushLanguage'), ), 'manifest' => array( 'description' => 'Rebuilds the manifest and finds new files', 'reference' => array($this, 'flushManifest'), ), 'combined' => array( 'description' => 'Rebuilds js and css files which have been combined together', 'reference' => array($this, 'flushCombined'), ), ); // Allow other modules to extend this. $obj = new FlushCache(); $obj->extendExtension($existing); // Done. return $existing; } /** * Flushes internal caches. * * @param string $username * The username to find. * @param string $password * The new password, plain text. */ public function flushCache($type = null) { // Prepare variables. $commands = $this->availableCommands(); // Generically add flush=all to help the scripts. $_GET['flush'] = 'all'; // Validate the input. if (!$type) { return 'Choose a type of cache to clear. See "sake more help" for more details'; } else if ($type == 'all') { // Run all the cache hooks. foreach ($commands as $type => $specifics) { // These are printed here so it is displayed in real-time. $response = $this->flushCache($type); printf("%s", $response); } return null; } else if (!array_key_exists($type, $commands)) { return sprintf('Unexpected parameter "%s". See "sake more help" for more details', $type); } else { // Prepare to run the fucntion. $object = null; $reference = $commands[$type]['reference']; list($class, $function) = $reference; if (is_object($class)) { // Passed an object instead of a static class? Ok. $object = $class; $class = get_class($object); } // Execute the flush command. $method = new ReflectionMethod($class, $function); $response = $method->invokeArgs($object, array()); // Use whatever message is returned. if ($response) { $response .= "\n"; } return sprintf("Clearing %s cache\n%s\n", $type, $response); } } /** * Finds and deletes the _resampled directory contents for cached images. */ public function flushImage() { // Get a list of all the folders. $folders = array(); // Since we're running inside the framework directory. $prefix = '..'; $dirs = $this->getDirList($prefix); // Find the direcotires with '_resampled' in their name. $cachedimages = array(); foreach ($dirs as $dir) { if (preg_match(sprintf( '@%s[^%s]*_resampled%s@', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR ), $dir . DIRECTORY_SEPARATOR)) { // Examine these directories for files which match the image format. $files = $this->getFileList($dir); foreach ($files as $file) { $filename = basename($file); if (preg_match('/\-/', $filename)) { // Temporarily only output the command that would otherwise delete the files. printf("rm %s\n", $file); } } } } } /** * Recursively gets a list of directories. */ protected function getDirList($dir) { // Go through each entry in the directory. for ($dirs = array(), $handle = opendir($dir); ($file = readdir($handle)) !== false;) { $path = $dir . DIRECTORY_SEPARATOR . $file; // Skip certain entries. if ($file == '.' || $file == '..') { continue; } // If it's a directory, add it to the list. else if (is_dir($path)) { // Add it to the list. $dirs[] = $path; // Add all sub folders too. foreach ($this->getDirList($path) as $subdir) { $dirs[] = $subdir; } } } // Clean up. closedir($handle); return $dirs; } /** * Non-recursively gets a list of files from a given directory. */ protected function getFileList($dir) { // Go through each entry in the directory. for ($files = array(), $handle = opendir($dir); ($file = readdir($handle)) !== false;) { $path = $dir . DIRECTORY_SEPARATOR . $file; // Skip certain entries. if ($file == '.' || $file == '..') { continue; } // If it's a file, add it to the list. else if (is_file($path)) { $files[] = $path; } } // Clean up. closedir($handle); return $files; } /** * Clears cached template files. */ public function flushTemplate() { // Rebuild the template manifest. $template = new SS_TemplateManifest(BASE_PATH, false, true); // Clear the template cache. SSViewer::flush_template_cache(); } /** * Clears the cache for multiple languages. * * If using the Zend translation module for multiple languages. */ public function flushLanguage() { // Can only be done if Zend_Translate is loaded. if (class_exists('Zend_Translate')) { $cache = Zend_Translate::getCache(); // ...and there is a cache to clear. if ($cache) { $cache->clean(Zend_Cache::CLEANING_MODE_ALL); return 'Successfully cleared'; } } return 'No cached data found'; } /** * Rebuilds the config and class manifests. */ public function flushManifest() { // Rebuild the config manifest $configManifest = new SS_ConfigManifest(BASE_PATH, false, true); // Rebuild the class manifest. $manifest = new SS_ClassManifest(BASE_PATH, false, true); } /** * Rebuilds the cache for combined CSS and JS files. */ public function flushCombined() { // Split any the combined files. Requirements::process_combined_files(); } } /** * This classes sole purpose is to allow extensions. * * Example usage: * In _config.php put: Object::add_extension('FlushCache', 'MyNewModule'); * In MyNewModule.php put: function flushCache_commands(&$commands) { ... } * Commands must follow the format specified in FlushCacheRole::availableCommands. */ class FlushCache extends Object { public function extendExtension(&$commands) { $this->extend('flushCache_commands', $commands); } } |