|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle CMS |
|
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) 2013-2015, Nazar Mokrynskyi |
|
6
|
|
|
* @license MIT License, see license.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace cs\Singleton; |
|
9
|
|
|
use |
|
10
|
|
|
cs\False_class; |
|
11
|
|
|
/** |
|
12
|
|
|
* Singleton trait |
|
13
|
|
|
* |
|
14
|
|
|
* Provides Singleton pattern implementation |
|
15
|
|
|
*/ |
|
16
|
|
|
trait Base { |
|
17
|
|
|
final protected function __construct () { |
|
18
|
|
|
} |
|
19
|
|
|
protected function construct () { |
|
20
|
|
|
} |
|
21
|
|
|
/** |
|
22
|
|
|
* Get instance of class |
|
23
|
|
|
* |
|
24
|
|
|
* @param bool $check If true - checks, if instance was already created, if not - instance of cs\False_class will be returned |
|
25
|
|
|
* |
|
26
|
|
|
* @return False_class|static |
|
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
static function instance ($check = false) { |
|
29
|
|
|
static $instance; |
|
30
|
|
|
return self::instance_prototype($instance, $check); |
|
31
|
|
|
} |
|
32
|
|
|
/** |
|
33
|
|
|
* Get instance of class |
|
34
|
|
|
* |
|
35
|
|
|
* @param object $instance |
|
36
|
|
|
* @param bool $check If true - checks, if instance was already created, if not - instance of cs\False_class will be returned |
|
37
|
|
|
* |
|
38
|
|
|
* @return False_class|static |
|
|
|
|
|
|
39
|
|
|
*/ |
|
40
|
|
|
protected static function instance_prototype (&$instance, $check = false) { |
|
41
|
|
|
if ($check) { |
|
42
|
|
|
return $instance ?: False_class::instance(); |
|
43
|
|
|
} |
|
44
|
|
|
if ($instance) { |
|
45
|
|
|
return $instance; |
|
46
|
|
|
} |
|
47
|
|
|
$class = get_called_class(); |
|
48
|
|
|
if (substr($class, 0, 2) != 'cs') { |
|
49
|
|
|
return False_class::instance(); |
|
50
|
|
|
} |
|
51
|
|
|
$custom_class_base = 'cs\\custom'.substr($class, 2); |
|
52
|
|
|
$next_alias = $class; |
|
53
|
|
|
if (class_exists($custom_class_base, false)) { |
|
54
|
|
|
$next_alias = $custom_class_base; |
|
55
|
|
|
} |
|
56
|
|
|
$modified_classes = modified_classes(); |
|
57
|
|
|
if (!isset($modified_classes[$class])) { |
|
58
|
|
|
$aliases = []; |
|
59
|
|
|
$modified_classes[$class] = [ |
|
60
|
|
|
'aliases' => &$aliases, |
|
61
|
|
|
'final_class' => &$next_alias |
|
62
|
|
|
]; |
|
63
|
|
|
$classes = glob(CUSTOM.'/classes/'.substr($class, 2).'_*.php'); |
|
64
|
|
|
foreach ($classes as $custom_class) { |
|
65
|
|
|
// Path to file with customized class |
|
66
|
|
|
$custom_class = str_replace(CUSTOM.'/classes/', '', substr($custom_class, 0, -4)); |
|
67
|
|
|
// Same path with prefixed class name |
|
68
|
|
|
$_custom_class = explode('/', $custom_class); |
|
69
|
|
|
$_custom_class[] = '_'.array_pop($_custom_class); |
|
70
|
|
|
$_custom_class = implode('/', $_custom_class); |
|
71
|
|
|
$aliases[] = [ |
|
72
|
|
|
'original' => $next_alias, |
|
73
|
|
|
'alias' => "cs\\custom\\$_custom_class", |
|
74
|
|
|
'path' => $custom_class |
|
75
|
|
|
]; |
|
76
|
|
|
$next_alias = "cs\\custom\\$custom_class"; |
|
77
|
|
|
} |
|
78
|
|
|
if (!is_dir(CACHE.'/classes')) { |
|
79
|
|
|
@mkdir(CACHE.'/classes', 0770, true); |
|
80
|
|
|
} |
|
81
|
|
|
modified_classes($modified_classes); |
|
82
|
|
|
} |
|
83
|
|
|
foreach ($modified_classes[$class]['aliases'] as $alias) { |
|
84
|
|
|
/** |
|
85
|
|
|
* If for whatever reason base class does or file that should be included does not exists |
|
86
|
|
|
*/ |
|
87
|
|
|
if ( |
|
88
|
|
|
!class_exists($alias['original'], false) || |
|
89
|
|
|
!file_exists(CUSTOM."/classes/$alias[path].php") |
|
90
|
|
|
) { |
|
91
|
|
|
clean_classes_cache(); |
|
92
|
|
|
$instance = new $class; |
|
93
|
|
|
$instance->construct(); |
|
94
|
|
|
return $instance; |
|
95
|
|
|
} |
|
96
|
|
|
class_alias($alias['original'], $alias['alias']); |
|
97
|
|
|
require_once CUSTOM."/classes/$alias[path].php"; |
|
98
|
|
|
} |
|
99
|
|
|
$instance = new $modified_classes[$class]['final_class']; |
|
100
|
|
|
$instance->construct(); |
|
101
|
|
|
return $instance; |
|
102
|
|
|
} |
|
103
|
|
|
final protected function __clone () { |
|
104
|
|
|
} |
|
105
|
|
|
final protected function __wakeup () { |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.