1eaa93ce6b27a74a8cd2d775530f936663634ae1

Author: Joel Perras

Date: 2009-03-10 15:08:05 -0400

Adding first draft of cache shell + associated tasks.

diff --git a/tests/cases/vendors/cache.test.php b/tests/cases/vendors/cache.test.php new file mode 100644 index 0000000..5260f86 --- /dev/null +++ b/tests/cases/vendors/cache.test.php @@ -0,0 +1,19 @@ +<?php +Mock::generate('ShellDispatcher'); + +App::import('Vendor', 'Cache.shells/Cache'); + +class CacheTest extends CakeTestCase { + + public function start() { + parent::start(); + $this->Shell = ClassRegistry::init('CacheShell'); + } + + public function testInstance() { + $this->assertIsA($this->Shell, 'CacheShell'); + } + + +} + diff --git a/vendors/shells/cache.php b/vendors/shells/cache.php new file mode 100644 index 0000000..0670332 --- /dev/null +++ b/vendors/shells/cache.php @@ -0,0 +1,78 @@ +<?php +/* SVN FILE: $Id$ */ +/** + * Cache Shell + * + * Long description for file + * + * PHP version 5 + * + * Licensed under The MIT License + * + * @filesource + * @package cache + * @subpackage cache.shells + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ + +/** + * Imports + **/ +App::import('Core', 'Shell'); + +/** + * Cache Shell + * + * @package cache + * @subpackage cache.shells + */ +class CacheShell extends Shell { + +/** + * undocumented variable + * + * @var string + */ + var $tasks = array('Clear', 'Show'); + + +/** + * Main shell execution + * + * @return void + * @access public + */ + function main() { + if (empty($this->args) || count($this->args) > 1) { + return $this->help(); + } + } + + +/** + * Help for Cache shell + * + * @return void + * @access public + */ + function help() { + $this->out(__("Cache Shell", true)); + $this->out(""); + $this->out(__("\tAllows you to perform list and clear operations on your application caches", true)); + $this->out(""); + $this->out(__("\tUsage:", true)); + $this->out(__("\t\tcake cache clear [-type views] [-ext ] all | [cache_name] [cache_name] ... ", true)); + $this->out(__("\t\tcake cache show [-type name]")); + $this->out(""); + $this->out(__("\tArguments:", true)); + $this->out(__("\t\tclear: \tIf called without [cache_name], clears entire view cache.", true)); + $this->out(__("\t\tshow: \tOutputs an alphanumerically ordered list of cache keys.", true)); + $this->out(""); + $this->hr(); + $this->out(""); + } +} +?> \ No newline at end of file diff --git a/vendors/shells/tasks/clear.php b/vendors/shells/tasks/clear.php new file mode 100644 index 0000000..cc1bfaa --- /dev/null +++ b/vendors/shells/tasks/clear.php @@ -0,0 +1,54 @@ +<?php + +class ClearTask extends Shell { + +/** + * undocumented variable + * + * @var string + */ + var $types = array('views', 'models', 'persistent'); + +/** + * Cache clearing + * + * @return void + * @access public + */ + function execute() { + if (count($this->args) == 0) return $this->help(); + + $options = array_merge(array('ext' => '', 'type' => null), $this->params); + $files = ($this->args[0] === 'all') ? null : $this->args; + + if (isset($options['type'])) { + clearCache($files, $options['type'], ''); + } else { + foreach ($this->types as $type) { + clearCache($files, $type, $options['ext']); + } + } + } + + /** + * Help for Cache shell + * + * @return void + * @access public + */ + function help() { + $this->out(__("Cache Shell", true)); + $this->out(""); + $this->out(__("\tAllows you to perform list and clear operations on your application caches", true)); + $this->out(""); + $this->out(__("\tUsage:", true)); + $this->out(__("\t\tcake cache clear [-type views] [-ext ] all | [cache_name] [cache_name] ... ", true)); + $this->out(""); + $this->out(__("\tArguments:", true)); + $this->out(__("\t\tclear: \tIf called without [cache_name], clears entire view cache.", true)); + $this->out(""); + $this->hr(); + $this->out(""); + } +} +?> \ No newline at end of file diff --git a/vendors/shells/tasks/show.php b/vendors/shells/tasks/show.php new file mode 100644 index 0000000..4fa40f5 --- /dev/null +++ b/vendors/shells/tasks/show.php @@ -0,0 +1,113 @@ +<?php +/* SVN FILE: $Id$ */ +/** + * Cache Shell + * + * Long description for file + * + * PHP version 5 + * + * Licensed under The MIT License + * + * @filesource + * @package cache + * @subpackage cache.shells + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ + +/** + * undocumented class + * + * @package default + */ +class ShowTask extends Shell { + +/** + * Files/aliases to be stripped from listing results + * + * @var array + * @access protected + */ + var $_special = array('.', '..'); + + +/** + * Show cached file list + * Actual outputting of file list is delegated to CacheShell::_output + * + * @return void + * @access public + */ + function execute() { + $options = array_merge(array('cacheDir' => CACHE, 'type' => 'views', 'ext' => ''), $this->params); + $directory = $options['cacheDir'] . $options['type'] . DS; + $files = glob($directory. '*'); + + if ($files === false) { + $this->out(__("An error has occurred. Aborting.", true)); + return false; + } + + $files = array_filter($files, array($this, '_filter')); + $this->_output($files); + } + +/** + * + * + * @param string $files + * @return void + * @access protected + */ + function _output($files) { + $fileCount = count($files); + + if ($fileCount === 0) { + $this->out(__('No files exist in specified directory.')); + return true; + } + + $this->out(String::insert(__(':N Cached Files: ', true), array('N' => count($files)))); + foreach ($files as $file) { + $this->out(String::insert("\t:file", compact('file'))); + } + } + + +/** + * Helper callback to remove invalid cache file names + * + * @return boolean True if file is not in $_special array, false otherwise. + * @access protected + */ + function _filter($file) { + return (!in_array($file, $this->_special, true)); + } + +/** + * Help for Cache shell + * + * @return void + * @access public + */ + function help() { + $this->out(__("Cache Shell", true)); + $this->out(""); + $this->out(__("\tAllows you to perform list and clear operations on your application caches", true)); + $this->out(""); + $this->out(__("\tUsage:", true)); + $this->out(__("\t\tcake cache show [-type name]")); + $this->out(""); + $this->out(__("\tArguments:", true)); + $this->out(__("\t\tshow: \tOutputs an alphanumerically ordered list of cache keys.", true)); + $this->out(""); + $this->hr(); + $this->out(""); + } + +} + +?> \ No newline at end of file