Completed
Push — master ( 2dbb1e...a190f2 )
by Nazar
03:57
created

Base::__wakeup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2013-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Singleton;
9
use
10
	cs\False_class,
11
	cs\Request;
12
13
/**
14
 * Singleton trait
15
 *
16
 * Provides Singleton-like implementation with some advanced capabilities
17
 */
18
trait Base {
19
	private $__request_id;
20 164
	final protected function __construct () {
21 164
	}
22 134
	protected function construct () {
23 134
	}
24
	/**
25
	 * Get instance of class
26
	 *
27
	 * @param bool $check If true - checks, if instance was already created, if not - instance of cs\False_class will be returned
28
	 *
29
	 * @return False_class|static
30
	 */
31 2
	public static function instance ($check = false) {
32 2
		static $instance;
33 2
		return self::instance_prototype($instance, $check);
34
	}
35
	/**
36
	 * Get instance of class
37
	 *
38
	 * @param static $instance
39
	 * @param bool   $check If true - checks, if instance was already created, if not - instance of cs\False_class will be returned
40
	 *
41
	 * @return False_class|static
42
	 */
43 182
	protected static function instance_prototype (&$instance, $check = false) {
44 182
		if (is_object($instance)) {
45 168
			if ($instance->__request_id !== Request::$id) {
46 136
				$instance->__request_id = Request::$id;
47 136
				if (defined('static::INIT_STATE_METHOD')) {
48 96
					$instance->{constant('static::INIT_STATE_METHOD')}();
49
				}
50
			}
51 168
			return $instance;
52
		}
53 150
		$called_class = get_called_class();
54 150
		if ($check || strpos($called_class, 'cs') !== 0) {
55 18
			return False_class::instance();
56
		}
57 150
		list($aliases, $final_class) = static::instance_prototype_get_aliases_final_class($called_class);
58 150
		foreach ($aliases as $alias) {
59
			/**
60
			 * If for whatever reason base class does not exists or file that should be included does not exists
61
			 */
62
			if (
63 2
				!class_exists($alias['original'], false) ||
64 2
				!file_exists($alias['path'])
65
			) {
66 2
				__classes_clean_cache();
67 2
				return static::instance_create($instance, $called_class, $called_class);
68
			}
69 2
			class_alias($alias['original'], $alias['alias']);
70 2
			require_once $alias['path'];
71
		}
72 150
		return static::instance_create($instance, $called_class, $final_class);
73
	}
74
	/**
75
	 * @param static $instance
76
	 * @param string $called_class
77
	 * @param string $final_class
78
	 *
79
	 * @return static
80
	 */
81 150
	protected static function instance_create (&$instance, $called_class, $final_class) {
82 150
		if ($final_class != $called_class) {
83
			/**
84
			 * We can't access protected methods of class if it doesn't extend current class, so let's call its `::instance()` method instead
85
			 */
86 2
			return $final_class::instance();
87
		}
88 150
		$instance               = new $called_class;
89 150
		$instance->__request_id = Request::$id;
90 150
		$instance->construct();
91 146
		if (defined('static::INIT_STATE_METHOD')) {
92 100
			$instance->{constant('static::INIT_STATE_METHOD')}();
93
		}
94 146
		return $instance;
95
	}
96
	/**
97
	 * @param string $class
98
	 *
99
	 * @return array
100
	 */
101 150
	protected static function instance_prototype_get_aliases_final_class ($class) {
102 150
		$modified_classes = modified_classes();
103 150
		if (!isset($modified_classes[$class])) {
104 72
			$custom_class_base = 'cs\\custom'.substr($class, 2);
105 72
			$next_alias        = $class;
106 72
			$aliases           = [];
107 72
			if (class_exists($custom_class_base, false)) {
108 2
				$next_alias = $custom_class_base;
109
			}
110 72
			$custom_classes_paths = defined('CUSTOM') ? glob(CUSTOM.'/classes/'.str_replace('\\', '/', substr($class, 3)).'_*.php') : [];
111 72
			$custom_length        = defined('CUSTOM') ? strlen(CUSTOM.'/classes/') : 0;
112 72
			foreach ($custom_classes_paths as $custom_class_path) {
113 2
				$custom_class = substr($custom_class_path, $custom_length, -4);
114 2
				$custom_class = 'cs\\custom\\'.str_replace('/', '\\', $custom_class);
115
				// Same path with prefixed class name
116 2
				$_custom_class   = explode('\\', $custom_class);
117 2
				$_custom_class[] = '_'.array_pop($_custom_class);
118 2
				$_custom_class   = implode('\\', $_custom_class);
119 2
				$aliases[]       = [
120 2
					'original' => $next_alias,
121 2
					'alias'    => $_custom_class,
122 2
					'path'     => $custom_class_path
123
				];
124 2
				$next_alias      = $custom_class;
125
			}
126 72
			$modified_classes[$class] = [
127 72
				'aliases'     => $aliases,
128 72
				'final_class' => $next_alias
129
			];
130 72
			modified_classes($modified_classes);
131
		}
132
		return [
133 150
			$modified_classes[$class]['aliases'],
134 150
			$modified_classes[$class]['final_class']
135
		];
136
	}
137
	final protected function __clone () {
138
	}
139
	final protected function __wakeup () {
140
	}
141
}
142