<?php

namespace common\logic;

abstract class AbstractSingleton
{
    private static $instances = array();

    final private function __construct()
    {
    }

    final public function __clone()
    {
        trigger_error("Le clonage n'est pas autorisé.", E_USER_ERROR);
    }

    final public static function getInstance(): self
    {
        $c = get_called_class();

        if (!isset(self::$instances[$c])) {
            self::$instances[$c] = new $c;

            if(in_array(ManagerInterface::class, class_implements(self::$instances[$c]))) {
                self::$instances[$c]->initContainer();
            }

            if(in_array(ServiceInterface::class, class_implements(self::$instances[$c]))) {
                self::$instances[$c]->loadDependencies();
            }
        }

        return self::$instances[$c];
    }
}
