Overview

Namespaces

  • Jyxo
    • Beholder
      • TestCase
    • Gettext
      • Parser
    • Input
      • Chain
      • Filter
      • Validator
    • Mail
      • Email
        • Attachment
      • Parser
      • Sender
    • Rpc
      • Json
      • Xml
    • Shell
    • Spl
    • Svn
    • Time
    • Webdav
  • PHP

Classes

  • ArrayUtil
  • CountableLimitIterator
  • FilterIterator
  • MapIterator
  • Object
  • ObjectCache

Interfaces

  • ArrayCopy
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * Jyxo PHP Library
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file license.txt.
 10:  * It is also available through the world-wide-web at this URL:
 11:  * https://github.com/jyxo/php/blob/master/license.txt
 12:  */
 13: 
 14: namespace Jyxo\Spl;
 15: 
 16: /**
 17:  * Simple object cache so we don't have to create them or write caching over again.
 18:  *
 19:  * Example:
 20:  * <code>
 21:  * $key = 'User_Friends/' . $user->username();
 22:  * return \Jyxo\Spl\ObjectCache::get($key) ?: \Jyxo\Spl\ObjectCache::set($key, new \User\Friends($this->context, $user));
 23:  * </code>
 24:  *
 25:  * @category Jyxo
 26:  * @package Jyxo\Spl
 27:  * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
 28:  * @license https://github.com/jyxo/php/blob/master/license.txt
 29:  * @author Jakub Tománek
 30:  */
 31: class ObjectCache implements \IteratorAggregate
 32: {
 33:     /**
 34:      * Object storage.
 35:      *
 36:      * @var array
 37:      */
 38:     private $storage = array();
 39: 
 40:     /**
 41:      * Returns default storage for static access.
 42:      *
 43:      * @return \Jyxo\Spl\ObjectCache
 44:      */
 45:     public static function getInstance()
 46:     {
 47:         static $instance = null;
 48:         if (null === $instance) {
 49:             $instance = new self();
 50:         }
 51:         return $instance;
 52:     }
 53: 
 54:     /**
 55:      * Returns an object from the default storage.
 56:      *
 57:      * @param string $key Object key
 58:      * @return object
 59:      */
 60:     public static function get($key)
 61:     {
 62:         return self::getInstance()->$key;
 63:     }
 64: 
 65:     /**
 66:      * Clear the whole storage.
 67:      *
 68:      * @return \Jyxo\Spl\ObjectCache
 69:      */
 70:     public function clear()
 71:     {
 72:         $this->storage = array();
 73:         return $this;
 74:     }
 75: 
 76:     /**
 77:      * Saves an object into the default storage.
 78:      *
 79:      * @param string $key Object key
 80:      * @param object $value Object
 81:      * @return object saved object
 82:      */
 83:     public static function set($key, $value)
 84:     {
 85:         self::getInstance()->$key = $value;
 86:         return $value;
 87:     }
 88: 
 89:     /**
 90:      * Returns an object from an own storage.
 91:      *
 92:      * @param string $key Object key
 93:      * @return object
 94:      */
 95:     public function __get($key)
 96:     {
 97:         return isset($this->storage[$key]) ? $this->storage[$key] : null;
 98:     }
 99: 
100:     /**
101:      * Saves an object into an own storage.
102:      *
103:      * @param string $key Object key
104:      * @param object $value Object
105:      */
106:     public function __set($key, $value)
107:     {
108:         $this->storage[$key] = $value;
109:     }
110: 
111:     /**
112:      * Returns if there's an object with key $key in the storage.
113:      *
114:      * @param string $key Object key
115:      * @return boolean
116:      */
117:     public function __isset($key)
118:     {
119:         return isset($this->storage[$key]);
120:     }
121: 
122:     /**
123:      * Deletes an object with key $key from the storage.
124:      *
125:      * @param mixed $key Object key
126:      */
127:     public function __unset($key)
128:     {
129:         unset ($this->storage[$key]);
130:     }
131: 
132:     /**
133:      * Returns an iterator.
134:      *
135:      * @return \ArrayIterator
136:      */
137:     public function getIterator()
138:     {
139:         return new \ArrayIterator($this->storage);
140:     }
141: }
142: 
Jyxo PHP Library API documentation generated by ApiGen 2.3.0