|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_modules; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_paths; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Support |
|
11
|
|
|
* @package kalanis\kw_modules |
|
12
|
|
|
* Basic class with supporting methods |
|
13
|
|
|
*/ |
|
14
|
|
|
class Support |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Get module from some preset path |
|
18
|
|
|
* @param string[] $modulePath |
|
19
|
|
|
* @return string[] |
|
20
|
|
|
*/ |
|
21
|
1 |
|
public static function modulePathFromDirPath(array $modulePath): array |
|
22
|
|
|
{ |
|
23
|
1 |
|
return array_values(array_map('ucfirst', |
|
24
|
1 |
|
array_map([self::class, 'normalizeModuleName'], |
|
25
|
1 |
|
array_filter( |
|
26
|
1 |
|
array_filter( |
|
27
|
1 |
|
$modulePath |
|
28
|
1 |
|
), [kw_paths\Stuff::class, 'notDots'] |
|
29
|
|
|
) |
|
30
|
|
|
) |
|
31
|
|
|
)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get module from template format |
|
36
|
|
|
* {SOMETHING_NAMED__UNDER__PATH} |
|
37
|
|
|
* @param string $name |
|
38
|
|
|
* @return string[] |
|
39
|
|
|
*/ |
|
40
|
9 |
|
public static function modulePathFromTemplate(string $name): array |
|
41
|
|
|
{ |
|
42
|
9 |
|
return array_values(array_map( |
|
43
|
9 |
|
'ucfirst', |
|
44
|
9 |
|
array_map( |
|
45
|
9 |
|
[self::class, 'normalizeTemplateModuleName'], |
|
46
|
9 |
|
array_map( |
|
47
|
9 |
|
'strtolower', |
|
48
|
9 |
|
explode('__', $name) |
|
49
|
|
|
) |
|
50
|
|
|
) |
|
51
|
|
|
)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
9 |
|
public static function clearModuleName(string $name): string |
|
55
|
|
|
{ |
|
56
|
9 |
|
return strtr($name, ['/' => '', '!' => '']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
public static function normalizeModuleName(string $moduleName): string |
|
60
|
|
|
{ |
|
61
|
2 |
|
return implode('', array_map([self::class, 'moduleNamePart'], explode('-', $moduleName))); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
9 |
|
public static function normalizeTemplateModuleName(string $moduleName): string |
|
65
|
|
|
{ |
|
66
|
9 |
|
return implode('', array_map([self::class, 'moduleNamePart'], explode('_', $moduleName))); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
11 |
|
public static function moduleNamePart(string $moduleName): string |
|
70
|
|
|
{ |
|
71
|
11 |
|
return ucfirst(strtolower($moduleName)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string[] $path |
|
76
|
|
|
* @return string |
|
77
|
|
|
* Reverse for |
|
78
|
|
|
* @see \kalanis\kw_modules\Support::modulePathFromTemplate |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public static function templatePathForModule(array $path): string |
|
81
|
|
|
{ |
|
82
|
2 |
|
return strval(implode( |
|
83
|
2 |
|
'__', |
|
84
|
2 |
|
array_map([self::class, 'templateModuleName'], $path) |
|
85
|
|
|
)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
5 |
|
public static function templateModuleName(string $moduleName): string |
|
89
|
|
|
{ |
|
90
|
5 |
|
if (!empty(preg_match_all('#([A-Z][a-z0-9]*)#u', $moduleName, $matches))) { |
|
91
|
4 |
|
return implode('_', array_map('mb_strtoupper', $matches[1])); |
|
92
|
|
|
} else { |
|
93
|
1 |
|
return mb_strtoupper($moduleName); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|