. */ namespace SP\Core; use SP\Core\Events\EventDispatcher; use SP\Core\Events\EventDispatcherInterface; use SP\Core\Exceptions\InvalidClassException; use SP\Core\UI\Theme; use SP\Core\UI\ThemeInterface; use SP\Mgmt\ItemBaseInterface; use SP\Storage\DBStorageInterface; use SP\Storage\XmlFileStorageInterface; use SP\Storage\MySQLHandler; use SP\Storage\XmlHandler; /** * Class SingleFactory * * @package SP\Core */ class DiFactory { /** * @var XmlFileStorageInterface */ private static $ConfigFactory; /** * @var DBStorageInterface */ private static $DBFactory; /** * @var ItemBaseInterface[] */ private static $ItemFactory = []; /** * @var ThemeInterface */ private static $ThemeFactory; /** * @var EventDispatcherInterface */ private static $EventDispatcher; /** * Devuelve el almacenamiento para la configuración * * @return XmlFileStorageInterface */ final public static function getConfigStorage() { if (!self::$ConfigFactory instanceof XmlFileStorageInterface) { self::$ConfigFactory = new XmlHandler(CONFIG_FILE); } return self::$ConfigFactory; } /** * Devuelve el manejador para la BD * * @return DBStorageInterface */ final public static function getDBStorage() { if (!self::$DBFactory instanceof DBStorageInterface) { self::$DBFactory = new MySQLHandler(); } return self::$DBFactory; } /** * Devuelve la instancia de la clase del elemento solicitado * * @param string $caller La clase del objeto * @param mixed $itemData Los datos del elemento * @return ItemBaseInterface */ final public static function getItem($caller, $itemData = null) { // error_log(count(self::$ItemFactory) . '-' . (memory_get_usage() / 1000)); try { if (!isset(self::$ItemFactory[$caller])) { self::$ItemFactory[$caller] = new $caller($itemData); return self::$ItemFactory[$caller]; } return (null !== $itemData) ? self::$ItemFactory[$caller]->setItemData($itemData) : self::$ItemFactory[$caller]; } catch (InvalidClassException $e) { debugLog('Invalid class for item data: ' . $e->getMessage(), true); } return self::$ItemFactory[$caller]; } /** * Devuelve el manejador de eventos * * @return EventDispatcherInterface */ final public static function getEventDispatcher() { if (!self::$EventDispatcher instanceof EventDispatcher) { self::$EventDispatcher = new EventDispatcher(); } return self::$EventDispatcher; } }