Skip to content

Commit

Permalink
Created Stash repository.
Browse files Browse the repository at this point in the history
Migrated the Stash repository out of it's previous svn repository on
Google Code ( http://code.google.com/p/stash/ ).
  • Loading branch information
tedivm committed Feb 17, 2012
1 parent 6fbbc4c commit e3cc2a2
Show file tree
Hide file tree
Showing 38 changed files with 5,261 additions and 0 deletions.
159 changes: 159 additions & 0 deletions lib/Stash/Autoloader.class.php
@@ -0,0 +1,159 @@
<?php
/**
* Stash
*
* Copyright (c) 2009-2011, Robert Hafner <tedivm@tedivm.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Robert Hafner nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Stash
* @author Robert Hafner <tedivm@tedivm.com>
* @copyright 2009-2011 Robert Hafner <tedivm@tedivm.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://code.google.com/p/stash/
* @since File available since Release 0.9.1
* @version Release: 0.9.3
*/

/**
* The StashAutoloader loads classes from the Stash library.
*
* @package Stash
* @author Robert Hafner <tedivm@tedivm.com>
*/
class StashAutoloader
{
/**
* A class name to path lookup of the classes in the project.
*
* @var array Class Name => Relative Path
*/
static protected $classes = array(
'StashBox' => 'Box.class.php',
'StashHandler' => 'Handler.class.php',
'StashHandlers' => 'Handlers.class.php',
'StashManager' => 'Manager.class.php',
'StashError' => 'Error.class.php',
'StashWarning' => 'Warning.class.php',
'StashUtilities' => 'Utilities.class.php',
'Stash' => 'Stash.class.php',

'StashApc' => 'handlers/Apc.class.php',
'StashArray' => 'handlers/Array.class.php',
'StashExceptionTest' => 'handlers/ExceptionTest.class.php',
'StashXcache' => 'handlers/Xcache.class.php',
'StashSqlite' => 'handlers/Sqlite.class.php',
'StashFileSystem' => 'handlers/FileSystem.class.php',
'StashMemcached' => 'handlers/Memcached.class.php',
'StashMultiHandler' => 'handlers/MultiHandler.class.php',
);
/**
* @var string Base path the autoloader uses when loading classes.
*/
static protected $path;

/**
* Registers the autoloader using the spl_autoload system.
*/
static public function register()
{
ini_set('unserialize_callback_func', 'spl_autoload_call');
spl_autoload_register(array(new self, 'autoload'));
}

/**
* Initializes the autoloader (at this point this just means setting the base path).
*/
static public function init()
{
self::$path = dirname(__file__) . '/';
}

/**
* Takes a class name and attempts to load it into the current environment. If the class is not part of the Stash
* project, or the file is unable to be opened, this function returns false.
*
* @param string $classname
* @return bool Returns true when class is successfully loaded.
*/
static function autoload($classname)
{
if(!isset(self::$classes[$classname]))
return false;

if(class_exists($classname, false) || interface_exists($classname, false))
return true;

if(!file_exists(self::$path . self::$classes[$classname]))
return false;

include(self::$path . self::$classes[$classname]);
return class_exists($classname, false) || interface_exists($classname, false);
}

/**
* Attempts to load every class available to Stash into the current environment.
*/
static function loadAll()
{
$currentDir = dirname(__file__) . '/';

foreach(self::$classes as $classname => $path)
{
if(class_exists($classname, false) || interface_exists($classname, false))
continue;

if(!file_exists($currentDir . $path))
return false;

include($currentDir . $path);

if(!class_exists($classname, false) && !interface_exists($classname, false))
return false;

}


return true;
}
}

/**
* This call makes sure the path is set when the class is first loaded.
*/
StashAutoloader::init();

define('STASH_SP_NONE', 0);
define('STASH_SP_OLD', 1);
define('STASH_SP_VALUE', 2);
define('STASH_SP_SLEEP', 3);
define('STASH_SP_PRECOMPUTE', 4);
?>
133 changes: 133 additions & 0 deletions lib/Stash/Box.class.php
@@ -0,0 +1,133 @@
<?php
/**
* Stash
*
* Copyright (c) 2009-2011, Robert Hafner <tedivm@tedivm.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Robert Hafner nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Stash
* @author Robert Hafner <tedivm@tedivm.com>
* @copyright 2009-2011 Robert Hafner <tedivm@tedivm.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://code.google.com/p/stash/
* @since File available since Release 0.9.1
* @version Release: 0.9.3
*/

/**
* StashBox makes managing a simply cache system easier by encapsulating certain commonly used tasks. StashBox also
* makes it easier to reuse a handler object for each Stash instance. The downside to StashBox is that it only works
* with one handler at a time, so systems with multiple cache pools will want to use the StashManager class instead.
*
* @package Stash
* @author Robert Hafner <tedivm@tedivm.com>
*/
class StashBox
{
static protected $handler;


/**
* Takes the same arguments as the Stash->setupKey() function and returns with a new Stash object. If a handler
* has been set for this class then it is used, otherwise the Stash object will be set to use script memory only.
* Any Stash object set for this class uses the 'stashbox' namespace.
*
* @example $cache = new StashBox::getCache('permissions', 'user', '4', '2');
*
* @param string|array $key, $key, $key...
* @return Stash
*/
static function getCache()
{
$args = func_get_args();

// check to see if a single array was used instead of multiple arguments
if(count($args) == 1 && is_array($args[0]))
$args = $args[0];

$handler = (isset(self::$handler)) ? self::$handler : null;
$stash = new Stash($handler, 'stashbox');

if(count($args) > 0)
$stash->setupKey($args);

return $stash;
}

/**
* Works exactly like the Stash->clear() function, except it can be called as a single function which will build the
* Stash object internally, load the handler, and clear the portion of the cache pool specified all in one call.
*
* @param null|string|array $key, $key, $key...
* @return bool success
*/
static function clearCache()
{
$stash = self::getCache(func_get_args());
return $stash->clear();
}

/**
* Works exactly like the Stash->purge() function, except it can be called as a single function which will build the
* Stash object internally, load the handler, and run the purge function all in one call.
*
* @return bool success
*/
static function purgeCache()
{
$stash = self::getCache();
return $stash->purge();
}

/**
* Returns a list of all available handlers that are registered with the system.
*
* @return array ShortName => Class
*/
static function getCacheHandlers()
{
return StashHandlers::getHandlers();
}

/**
* Sets a handler for each Stash object created by this class. This allows the handlers to be created just once
* and reused, making it much easier to incorporate caching into any code.
*
* @param StashHandler $handler
*/
static function setHandler(StashHandler $handler)
{
self::$handler = $handler;
}
}

?>
47 changes: 47 additions & 0 deletions lib/Stash/Error.class.php
@@ -0,0 +1,47 @@
<?php
/**
* Stash
*
* Copyright (c) 2009-2011, Robert Hafner <tedivm@tedivm.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Robert Hafner nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Stash
* @author Robert Hafner <tedivm@tedivm.com>
* @copyright 2009-2011 Robert Hafner <tedivm@tedivm.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://code.google.com/p/stash/
* @since File available since Release 0.9.1
* @version Release: 0.9.3
*/

class StashError extends Exception {}
?>

0 comments on commit e3cc2a2

Please sign in to comment.