Singleton::me()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/***************************************************************************
3
 *   Copyright (C) 2004-2007 by Sveta A. Smirnova                          *
4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   it under the terms of the GNU Lesser General Public License as        *
7
 *   published by the Free Software Foundation; either version 3 of the    *
8
 *   License, or (at your option) any later version.                       *
9
 *                                                                         *
10
 **************************************************************************/
11
12
declare(strict_types=1);
13
14
namespace DBD\Common;
15
16
use DBD\Entity\Common\EntityException;
17
18
abstract class Singleton implements Instantiatable
19
{
20
    /**
21
     * Все вызванные ранее инстансы классов
22
     *
23
     * @var array $instances
24
     */
25
    private static $instances = [];
26
27
    /**
28
     * Singleton constructor. You can't create me
29
     */
30
    private function __construct()
31
    {
32
    }
33
34
    /**
35
     * @return Instantiatable|Singleton|static
36
     */
37
    public static function me(): Instantiatable
38
    {
39
        return self::getInstance(get_called_class());
40
    }
41
42
    /**
43
     * Функция получения инстанса класса
44
     *
45
     * @param string $class
46
     *
47
     * @return Singleton
48
     */
49
    final public static function getInstance(string $class): Singleton
50
    {
51
        if (!isset(self::$instances[$class])) {
52
            $object = new $class;
53
54
            return self::$instances[$class] = $object;
55
        } else {
56
            return self::$instances[$class];
57
        }
58
    }
59
60
    /**
61
     * do not clone me
62
     * @throws EntityException
63
     */
64
    protected function __clone()
65
    {
66
        throw new EntityException("Singleton classes should not be cloned");
67
    }
68
}
69