1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSoellinger\SilverStripe\ModelAnnotations\Util; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionFunction; |
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
8
|
|
|
use SilverStripe\Core\ClassInfo; |
9
|
|
|
use SilverStripe\Core\Config\Configurable; |
10
|
|
|
use SilverStripe\Core\Injector\Injectable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Helper class. |
14
|
|
|
*/ |
15
|
|
|
class Util |
16
|
|
|
{ |
17
|
|
|
use Injectable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Transform silver stripe db type to a php type. |
21
|
|
|
*/ |
22
|
1 |
|
public function silverStripeToPhpType(string $dataType): string |
23
|
|
|
{ |
24
|
1 |
|
$dataType = strtolower($dataType); |
25
|
1 |
|
$dataType = preg_replace( |
26
|
|
|
'/^(varchar|htmltext|text|htmlfragment|time|datetime|date|htmlvarchar|enum).*/m', |
27
|
|
|
'string', |
28
|
|
|
$dataType |
29
|
|
|
); |
30
|
1 |
|
$dataType = (string) preg_replace('/^(percentage|decimal|currency).*/m', 'float', (string) $dataType); |
31
|
|
|
|
32
|
1 |
|
return str_replace('boolean', 'bool', $dataType); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the file path by the full qualified class name. |
37
|
|
|
* |
38
|
|
|
* @param string $fqn Full qualified class name |
39
|
|
|
*/ |
40
|
3 |
|
public function fileByFqn(string $fqn): string |
41
|
|
|
{ |
42
|
3 |
|
if (function_exists($fqn)) { |
43
|
1 |
|
$reflector = new ReflectionFunction($fqn); |
44
|
2 |
|
} elseif (class_exists($fqn)) { |
45
|
1 |
|
$reflector = new ReflectionClass($fqn); |
46
|
|
|
} else { |
47
|
1 |
|
return ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
return (string) $reflector->getFileName(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get namespace from class full qualified name. |
55
|
|
|
* |
56
|
|
|
* @param string $fqn Full qualified name |
57
|
|
|
*/ |
58
|
2 |
|
public function getNamespaceFromFqn(string $fqn): string |
59
|
|
|
{ |
60
|
2 |
|
$pos = strrpos($fqn, '\\'); |
61
|
|
|
|
62
|
2 |
|
if ($pos !== false) { |
63
|
2 |
|
return substr($fqn, 0, $pos); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return '\\'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get all classes from a namespace. |
71
|
|
|
* |
72
|
|
|
* @param string $namespace The namespace in which to search |
73
|
|
|
* |
74
|
|
|
* @return string[] |
75
|
|
|
*/ |
76
|
1 |
|
public function getClassesFromNamespace(string $namespace) |
77
|
|
|
{ |
78
|
1 |
|
$namespace .= '\\'; |
79
|
|
|
|
80
|
1 |
|
$classes = array_filter(ClassInfo::allClasses(), function (string $item) use ($namespace) { |
81
|
1 |
|
return substr($item, 0, strlen($namespace)) === $namespace; |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
/** @var string[] */ |
85
|
1 |
|
return array_values($classes); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|