| Total Complexity | 4 |
| Total Lines | 32 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 12 | trait SingletonTrait { |
||
| 13 | |||
| 14 | protected static $instance = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Retorna a instância da Classe |
||
| 18 | * Diferentes $alias retornam diferentes instâncias |
||
| 19 | * @param string $alias |
||
| 20 | * @return static |
||
| 21 | */ |
||
| 22 | public static function instance($alias = 'default') { |
||
| 23 | $class = get_called_class(); |
||
| 24 | if (!isset(static::$instance[$class][$alias])) { |
||
| 25 | $classDi = static::getClassDi(); |
||
| 26 | $instance = new $classDi(); |
||
| 27 | static::$instance[$class][$alias] = $instance; |
||
| 28 | } |
||
| 29 | return static::$instance[$class][$alias]; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Retorna o nome a classe que deverá ser usada no $instance |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | protected static function getClassDi() { |
||
| 37 | return DependenceInjector::getClassDi(get_called_class()); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Não se deve usar o construtor de um objeto Singleton |
||
| 42 | */ |
||
| 43 | private final function __construct() { |
||
| 44 | /* its not possible */ |
||
| 45 | } |
||
| 46 | |||
| 48 |