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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/gazebo
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
*
*/
namespace Maslosoft\Gazebo;
use Maslosoft\EmbeDi\EmbeDi;
use Maslosoft\Gazebo\Exceptions\ConfigurationException;
use Maslosoft\Gazebo\Storage\PluginsStorage;
use ReflectionClass;
/**
* PluginFactory
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class PluginFactory
{
/**
* Configured plugins instances
* @var PluginsStorage
*/
private $instances = null;
/**
* Single plugins instances
* @var Storage\PluginStorage
*/
private $plugins = null;
/**
* EmbeDi instance
* @var EmbeDi
*/
private $di = null;
/**
* Static instances of plugin factories
* @var PluginFactory[]
*/
private static $_pf = [];
/**
* Class constructor with optional instanceid which is passed to EmbeDi
* @param string $instanceId
*/
public function __construct($instanceId = Gazebo::DefaultInstanceId)
{
$this->instances = new PluginsStorage($this, $instanceId);
$this->plugins = new Storage\PluginStorage($this, $instanceId);
$this->di = new EmbeDi($instanceId);
}
/**
* Flyweight accessor for `PluginFactory` with optional instanceid.
* This will create only one runtime wide instance of `PluginFactory` for each `$instanceId`.
* @param string $instanceId
* @return PluginFactory Flyweight instance of PluginFactory
*/
public static function fly($instanceId = Gazebo::DefaultInstanceId)
{
if (empty(self::$_pf[$instanceId]))
{
self::$_pf[$instanceId] = new static($instanceId);
}
return self::$_pf[$instanceId];
}
/**
* Create plugin set from `$configuration` for `$object`
* optionally implementing one or more `$interfaces`.
*
* @param mixed[][] $configuration Configuration arrays
* @param string|object $object Object or class name
* @param null|string|string[] $interfaces Array or string of interface names or class names
* @return object[] Array of plugin instances
*/
public function create($configuration, $object, $interfaces = null)
{
$plugins = [];
foreach ($this->_getConfigs($configuration, $object, $interfaces) as $config)
{
$plugins[] = $this->_instantiate($config);
}
return $plugins;
}
/**
* Get instance of plugin set from `$config` for `$object`
* optionally implementing one or more `$interfaces`.
*
* This will create instances unique for each object and interfaces set.
* This will create only **one instance** of each plugin per config.
*
* @param mixed[][] $config
* @param string|object $object
* @param null|string|string[] $interfaces
* @return object[] Array of plugin instances
*/
public function instance($config, $object, $interfaces = null)
{
if (is_string($object))
{
$key = $object;
}
else
{
$key = get_class($object);
}
if (null !== $interfaces)
{
if (!is_array($interfaces))
{
$interfaces = [
(string) $interfaces
];
}
$key .= '.' . implode('.', $interfaces);
}
$key .= $this->getKey($config);
if (!isset($this->instances[$key]))
{
$plugins = [];
foreach ($this->_getConfigs($config, $object, $interfaces) as $config)
{
$plugins[] = $this->_instantiate($config, true);
}
$this->instances[$key] = $plugins;
}
return $this->instances[$key];
}
/**
* Check if object or class implements one or more interfaces
* @param object|string $object
* @param null|string|string[] $interfaces Interfaces to check
* @return boolean
*/
private function _implements($object, $interfaces = null)
{
if (null === $interfaces)
{
return true;
}
if (!is_array($interfaces))
{
$interfaces = [
(string) $interfaces
];
}
foreach ($interfaces as $interface)
{
$objectInfo = new ReflectionClass($object);
$interfaceInfo = new ReflectionClass($interface);
if ($objectInfo->name === $interfaceInfo->name)
{
return true;
}
if ($objectInfo->isSubclassOf($interface))
{
return true;
}
if ($interfaceInfo->isInterface() && $objectInfo->implementsInterface($interface))
{
return true;
}
}
return false;
}
/**
* Get class name from configuration
* @param string|mixed[] $config
* @return string
*/
private function _getClassName($config)
{
if (is_string($config))
{
return $config;
}
return $config[$this->di->classField];
}
/**
* Config generator
*
* @param mixed[][] $configuration Configuration arrays
* @param string|object $object Object or class name
* @param null|string|string[] $interfaces Array or string of interface names or class names
* @return mixed[] Array of plugin configs
* @yield mixed[] Array of plugin configs
*/
private function _getConfigs($configuration, $object, $interfaces = null)
{
foreach ($configuration as $interface => $configs)
{
if (!is_string($interface))
{
throw new ConfigurationException(sprintf('Wrong configuration for key `%s` - key must be class name', $interface));
}
if (!is_array($configs))
{
throw new ConfigurationException(sprintf('Wrong configuration for key `%s`, configuration should be array, `%s` given', $interface, gettype($configs)));
}
if (!$this->_implements($object, $interface))
{
continue;
}
foreach ($configs as $config)
{
$pluginClass = $this->_getClassName($config);
if (!$this->_implements($pluginClass, $interfaces))
{
continue;
}
yield $config;
}
}
}
/**
* Instantiate object based on configuration
* @param string|mixed[] $config
* @return object
*/
private function _instantiate($config, $fly = false)
{
$className = $this->_getClassName($config);
if ($fly)
{
$key = $this->getKey($config);
if (isset($this->plugins[$key]))
{
$plugin = $this->plugins[$key];
}
else
{
$plugin = $this->plugins[$key] = new $className;
}
}
else
{
$plugin = new $className;
}
if (is_array($config))
{
$plugin = $this->di->apply($config, $plugin);
}
return $plugin;
}
private function getKey($config)
{
if (is_array($config))
{
// Only class field, use class name as a key
if (!empty($config['class']) && count($config) === 1)
{
return $config['class'];
}
// Complex config
return json_encode($config);
}
else
{
return $config;
}
}
}
API documentation generated by ApiGen