1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/zamm |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/zamm/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Zamm; |
14
|
|
|
|
15
|
|
|
use DirectoryIterator; |
16
|
|
|
use Maslosoft\Addendum\Utilities\ClassChecker; |
17
|
|
|
use ReflectionClass; |
18
|
|
|
use ReflectionProperty; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Iterator |
22
|
|
|
* |
23
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
24
|
|
|
*/ |
25
|
|
|
class Iterator |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Iterate over classes in same folder. |
30
|
|
|
* @param string $class |
31
|
|
|
* @return string[] |
32
|
|
|
*/ |
33
|
|
|
public static function ns($class) |
34
|
|
|
{ |
35
|
|
|
$info = new ReflectionClass($class); |
36
|
|
|
$path = dirname($info->getFileName()); |
37
|
|
|
$ns = $info->getNamespaceName(); |
38
|
|
|
$classNames = []; |
39
|
|
|
foreach (new DirectoryIterator($path) as $fileInfo) |
40
|
|
|
{ |
41
|
|
|
$name = $fileInfo->getFilename(); |
42
|
|
|
|
43
|
|
|
// Only files |
44
|
|
|
if (!$fileInfo->isFile()) |
45
|
|
|
{ |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Only php |
50
|
|
|
if (!preg_match('~\.php$~', $name)) |
51
|
|
|
{ |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
$fqn = sprintf('%s\%s', $ns, basename($name, '.php')); |
55
|
|
|
if (!ClassChecker::exists($fqn)) |
56
|
|
|
{ |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
$classNames[] = $fqn; |
60
|
|
|
} |
61
|
|
|
sort($classNames); |
62
|
|
|
return $classNames; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Iterate over *public* class methods |
67
|
|
|
* @param string $class |
68
|
|
|
* @return string[] |
69
|
|
|
*/ |
70
|
|
|
public static function methods($class) |
71
|
|
|
{ |
72
|
|
|
$info = new ReflectionClass($class); |
73
|
|
|
|
74
|
|
|
$methods = []; |
75
|
|
|
foreach ($info->getMethods(ReflectionProperty::IS_PUBLIC) as $method) |
76
|
|
|
{ |
77
|
|
|
$methods[] = $method->name; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Sort methods that: |
82
|
|
|
* * Getters and setters are first |
83
|
|
|
* * Getters and setters are side-by-side to each other counterpart |
84
|
|
|
*/ |
85
|
|
|
$sort = function($a, $b) |
86
|
|
|
{ |
87
|
|
|
// Whether is getter or setter |
88
|
|
|
$isAGS = false; |
89
|
|
|
$isBGS = false; |
90
|
|
|
// Whether is getter |
91
|
|
|
$isAG = false; |
92
|
|
|
$isBG = false; |
93
|
|
|
// Whether is setter |
94
|
|
|
$isAS = false; |
95
|
|
|
$isBS = false; |
96
|
|
|
// Check if it's getter or setter |
97
|
|
|
if (preg_match('~^[gs]et[A-Z0-9]~', $a)) |
98
|
|
|
{ |
99
|
|
|
if(preg_match('~^get[A-Z0-9]~', $a)) |
100
|
|
|
{ |
101
|
|
|
$isAG = true; |
102
|
|
|
} |
103
|
|
|
else |
104
|
|
|
{ |
105
|
|
|
$isAS = true; |
106
|
|
|
} |
107
|
|
|
$a = substr($a, 3); |
108
|
|
|
$isAGS = true; |
109
|
|
|
} |
110
|
|
|
// Check if it's getter or setter |
111
|
|
|
if (preg_match('~^[gs]et[A-Z0-9]~', $b)) |
112
|
|
|
{ |
113
|
|
|
if(preg_match('~^get[A-Z0-9]~', $b)) |
114
|
|
|
{ |
115
|
|
|
$isBG = true; |
116
|
|
|
} |
117
|
|
|
else |
118
|
|
|
{ |
119
|
|
|
$isBS = true; |
120
|
|
|
} |
121
|
|
|
$b = substr($b, 3); |
122
|
|
|
$isBGS = true; |
123
|
|
|
} |
124
|
|
|
if($isAGS && !$isBGS) |
125
|
|
|
{ |
126
|
|
|
return -1; |
127
|
|
|
} |
128
|
|
|
if(!$isAGS && $isBGS) |
129
|
|
|
{ |
130
|
|
|
return 1; |
131
|
|
|
} |
132
|
|
|
if ($a == $b) |
133
|
|
|
{ |
134
|
|
|
if($isAGS && $isBGS) |
135
|
|
|
{ |
136
|
|
|
if($isAG && $isBS) |
137
|
|
|
{ |
138
|
|
|
return -1; |
139
|
|
|
} |
140
|
|
|
if($isBG && $isAS) |
141
|
|
|
{ |
142
|
|
|
return 1; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
return 0; |
146
|
|
|
} |
147
|
|
|
return ($a < $b) ? -1 : 1; |
148
|
|
|
}; |
149
|
|
|
usort($methods, $sort); |
150
|
|
|
|
151
|
|
|
return $methods; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Iterate over *public* class properties |
156
|
|
|
* @param string $class |
157
|
|
|
* @return string[] |
158
|
|
|
*/ |
159
|
|
|
public static function properties($class) |
160
|
|
|
{ |
161
|
|
|
$info = new ReflectionClass($class); |
162
|
|
|
|
163
|
|
|
$properties = []; |
164
|
|
|
foreach ($info->getProperties(ReflectionProperty::IS_PUBLIC) as $property) |
165
|
|
|
{ |
166
|
|
|
$properties[] = $property->name; |
167
|
|
|
} |
168
|
|
|
sort($properties); |
169
|
|
|
return $properties; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|