1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of twLib |
4
|
|
|
* http://www.thomaswhiston.com |
5
|
|
|
* [email protected] |
6
|
|
|
* Created by PhpStorm. |
7
|
|
|
* User: Thomas Whiston |
8
|
|
|
* Date: 27/01/2016 |
9
|
|
|
* Time: 14:14 |
10
|
|
|
*/ |
11
|
|
|
namespace twhiston\twLib\Object; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Instantiate |
15
|
|
|
* Create a class |
16
|
|
|
* @package twhiston\twLib\Object |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* Class Instantiate |
20
|
|
|
* @package twhiston\twLib\Object |
21
|
|
|
*/ |
22
|
|
|
class Instantiate |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param $class |
28
|
|
|
* @param $args |
29
|
|
|
* @param $namespace string psr-4 style 'whatever\\thing\\another\\' |
30
|
|
|
* @param null $interface test that class implements this interface |
31
|
|
|
* @return object |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
9 |
|
public static function make($class, $args, $namespace = null, $interface = null) |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
// $count = substr_count($class, '\\'); |
|
|
|
|
38
|
|
|
// if ($count = 0 || !(0 === strpos($class, 'Drupal'))) { |
39
|
|
|
// //if there is no '/' we assume this is not a fully qualified namespace and make it our root Constraint namespace |
40
|
|
|
// //If there is no drupal at the start then we assume its our class further down the tree |
41
|
|
|
// $class = $namespace.$class; |
42
|
|
|
// } |
43
|
|
|
|
44
|
9 |
|
if ($namespace !== null) { |
45
|
7 |
|
$class = $namespace . $class; |
46
|
7 |
|
} |
47
|
|
|
|
48
|
|
|
try { |
49
|
|
|
|
50
|
9 |
|
if (!class_exists($class, true)) { |
51
|
5 |
|
throw new \Exception('Class does not exist'); |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
if ($interface !== null) { |
55
|
|
|
|
56
|
4 |
|
$imp = class_implements($class, true); |
57
|
4 |
|
if (!is_array($imp)) { |
58
|
|
|
throw new \Exception('Does not implement an interface'); |
59
|
|
|
} |
60
|
4 |
|
if (!in_array($interface, $imp)) { |
61
|
2 |
|
throw new \Exception('Does not implement requested interface'); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
} |
65
|
|
|
//Make sure args are an array |
66
|
2 |
|
if ($args !== null && $args !== false) { |
67
|
2 |
|
$args = (!is_array($args)) ? array($args) : $args; |
68
|
2 |
|
if (is_array($args)) { |
69
|
2 |
|
$c = count(array_filter(array_keys($args), 'is_string')); |
|
|
|
|
70
|
2 |
|
if (count(array_filter(array_keys($args), 'is_string')) > 0) { |
71
|
|
|
$args = [$args]; |
72
|
|
|
} |
73
|
2 |
|
} |
74
|
2 |
|
} |
75
|
|
|
|
76
|
2 |
|
$instance = Instantiate::instantiate($class, $args); |
77
|
|
|
|
78
|
9 |
|
} catch (\Exception $e) { |
79
|
7 |
|
throw $e; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return $instance; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $class |
87
|
|
|
* @param $args |
88
|
|
|
* @return object |
89
|
|
|
*/ |
90
|
2 |
|
private static function instantiate($class, $args) |
91
|
|
|
{ |
92
|
|
|
//BOOOOO, this produces a parse error on less than 5.6.0 |
|
|
|
|
93
|
|
|
// if (version_compare(phpversion(), '5.6.0') !== -1) { |
94
|
|
|
// $instance = new $class(...$args); |
95
|
|
|
// } else { |
96
|
2 |
|
$reflect = new \ReflectionClass($class); |
97
|
2 |
|
$instance = ($args === null || $args === false) ? $reflect->newInstanceArgs() : $reflect->newInstanceArgs($args); |
98
|
|
|
// } |
99
|
|
|
|
100
|
2 |
|
return $instance; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.