Maslosoft Gazebo API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Maslosoft\Gazebo;
use ArrayAccess;
use Countable;
use Iterator;
use Maslosoft\Addendum\Utilities\ClassChecker;
use Maslosoft\EmbeDi\EmbeDi;
use Maslosoft\Gazebo\Exceptions\GazeboException;
/**
* PluginsContainer
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class PluginsContainer implements ArrayAccess, Countable, Iterator
{
/**
* Values holder
* @var mixed[]
*/
private $_values = [];
/**
* DI container
* @var EmbeDi
*/
private $_di = null;
/**
* Create instance with optional configuration.
* This also prevents setting bogus configuration keys.
* This will unset all public properties and pass them thru get/set and allow array access.
* @param mixed[] $config
*/
public function __construct($config = [])
{
$this->_di = new EmbeDi();
$this->apply($config);
}
/**
* Apply configuration
* @param mixed[] $config
*/
public function apply($config)
{
foreach ((array)$config as $name => $value)
{
$this->offsetSet($name, $value);
}
}
public function toArray()
{
return $this->_values;
}
public function has($name)
{
return ClassChecker::exists($name);
}
// <editor-fold defaultstate="collapsed" desc="__* magic implementation">
/**
* This will be called instead of public getting properties.
* @param string $name
* @return mixed
*/
public function __get($name)
{
return $this->offsetGet($name);
}
/**
* This will be called instead of public setting properties.
* @param string $name Configuration key value
* @param mixed $value Configuration value
* @return mixed
*/
public function __set($name, $value)
{
$this->offsetSet($name, $value);
}
/**
* Unset
* @param string $name
*/
public function __unset($name)
{
$this->offsetUnset($name);
}
/**
* Isset
* @param string $name
* @return bool
*/
public function __isset($name)
{
return $this->offsetExists($name);
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="ArrayAccess implementation">
public function offsetExists($name)
{
return array_key_exists($name, $this->_values);
}
public function offsetGet($name)
{
if (!$this->has($name))
{
throw new GazeboException("Class `$name` used as key does not exists. Tried to get value.");
}
return $this->_values[$name];
}
public function offsetSet($name, $value)
{
if (!$this->has($name))
{
throw new GazeboException("Class `$name` used as key does not exists. Tried to set value.");
}
foreach($value as $cfg)
{
if(is_array($cfg))
{
$className = $cfg[$this->_di->classField];
}
else
{
$className = $cfg;
}
if (!$this->has($className))
{
throw new GazeboException("Class `$className` used as value does not exists. Tried to set value.");
}
}
return $this->_values[$name] = $value;
}
public function offsetUnset($name)
{
if (!$this->has($name))
{
throw new GazeboException("Configuration property `$name` does not exists. Tried to unset value.");
}
unset($this->_values[$name]);
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Countable implementation">
public function count($mode = COUNT_NORMAL)
{
return count($this->_values, $mode);
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Iterator implementation">
public function current()
{
return current($this->_values);
}
public function key()
{
return key($this->_values);
}
public function next()
{
return next($this->_values);
}
public function rewind()
{
return reset($this->_values);
}
public function valid()
{
return $this->has($this->key()) && $this->offsetExists($this->key());
}
// </editor-fold>
}
API documentation generated by ApiGen