1 <?php
2
3 /*
4 * This file is part of the Icybee package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Icybee\Modules\Cache;
13
14 use ICanBoogie\OffsetNotWritable;
15 use ICanBoogie\OffsetNotReadable;
16
17 /**
18 * Cache manager collection.
19 */
20 class Collection implements \IteratorAggregate, \ArrayAccess
21 {
22 static private $instance;
23
24 static public function get()
25 {
26 if (self::$instance)
27 {
28 return self::$instance;
29 }
30
31 return self::$instance = new static();
32 }
33
34 protected $collection = array();
35
36 protected function __construct()
37 {
38 $collection = array
39 (
40 'core.catalogs' => new CatalogsCacheManager,
41 'core.configs' => new ConfigsCacheManager,
42 'core.modules' => new ModulesCacheManager
43 );
44
45 new Collection\CollectEvent($this, $collection);
46
47 foreach ($collection as $id => $cache)
48 {
49 $cache->id = $id;
50 }
51
52 $this->collection = $collection;
53 }
54
55 function getIterator()
56 {
57 return new \ArrayIterator($this->collection);
58 }
59
60 public function offsetExists($offset)
61 {
62 return isset($this->collection[$offset]);
63 }
64
65 /**
66 * Returns a cache.
67 *
68 * @throws CacheNotDefined in attempt to get a cache manager that is not defined.
69 */
70 public function offsetGet($id)
71 {
72 if (!$this->offsetExists($id))
73 {
74 throw new CacheNotDefined($id);
75 }
76
77 return $this->collection[$id];
78 }
79
80 /**
81 * Adds a cache to the collection.
82 *
83 * @throws InvalidArgumentException if the cache manage doesn't implement {@link CacheManagerInterface}.
84 */
85 public function offsetSet($id, $cache)
86 {
87 if (!($cache instanceof CacheManagerInterface))
88 {
89 throw new \InvalidArgumentException('Cache must implements ' . __NAMESPACE__ . '\CacheManagerInterface.');
90 }
91
92 $this->collection[$id] = $cache;
93 }
94
95 /**
96 * @throws OffsetNotWritable in attempt to unset an offset.
97 */
98 public function offsetUnset($offset)
99 {
100 throw new OffsetNotWritable(array($offset, $this));
101 }
102 }
103
104 /**
105 * Exception thrown when a cache is not defined.
106 */
107 class CacheNotDefined extends OffsetNotWritable
108 {
109
110 }
111
112 namespace Icybee\Modules\Cache\Collection;
113
114 /**
115 * Event class for the `Icybee\Modules\Cache\Collection::collect` event.
116 */
117 class CollectEvent extends \ICanBoogie\Event
118 {
119 /**
120 * Reference to the cache manager collection.
121 *
122 * @var array
123 */
124 public $collection;
125
126 /**
127 * The event is constructed with the type `collect`.
128 *
129 * @param \Icybee\Modules\Cache\Collection $target
130 * @param array $collection Cache manager collection.
131 */
132 public function __construct(\Icybee\Modules\Cache\Collection $target, array &$collection)
133 {
134 $this->collection = &$collection;
135
136 parent::__construct($target, 'collect');
137 }
138 }