1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nip\Dispatcher\Resolver\ClassResolver; |
5
|
|
|
|
6
|
|
|
use Nip\Utility\Traits\SingletonTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class NameGenerator |
10
|
|
|
* @package Nip\Dispatcher\Resolver\ClassResolver |
11
|
|
|
*/ |
12
|
|
|
class NameGenerator |
13
|
|
|
{ |
14
|
|
|
use SingletonTrait; |
15
|
|
|
|
16
|
4 |
|
protected array $namespaces = []; |
17
|
|
|
|
18
|
4 |
|
|
19
|
4 |
|
/** |
20
|
|
|
* @param $module |
21
|
4 |
|
* @param $controller |
22
|
4 |
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public static function generateClasses($module, $controller) |
25
|
|
|
{ |
26
|
|
|
$module = NameFormatter::formatModuleName($module); |
27
|
|
|
$controller = NameFormatter::formatControllerName($controller); |
28
|
|
|
|
29
|
|
|
$return = self::instance()->generateControllerNamespacedVariations($module, $controller); |
30
|
|
|
$return[] = self::generateControllerString($module, $controller); |
31
|
4 |
|
return $return; |
32
|
|
|
} |
33
|
4 |
|
|
34
|
4 |
|
/** |
35
|
4 |
|
* @param $module |
36
|
|
|
* @param $controller |
37
|
4 |
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public static function generateControllerNamespaced($module, $controller, $namespace = null) |
40
|
|
|
{ |
41
|
|
|
$name = static::generateModuleNameNamespace($module, $namespace); |
42
|
|
|
$name .= '\Controllers\\'; |
43
|
|
|
$name .= static::formatControllerName($controller); |
44
|
|
|
|
45
|
|
|
return $name; |
46
|
4 |
|
} |
47
|
|
|
|
48
|
4 |
|
|
49
|
|
|
/** |
50
|
|
|
* @param $module |
51
|
|
|
* @param $controller |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public static function generateControllerString($module, $controller) |
55
|
4 |
|
{ |
56
|
|
|
return $module . "_" . $controller . "Controller"; |
57
|
4 |
|
} |
58
|
4 |
|
|
59
|
4 |
|
/** |
60
|
4 |
|
* @param $namespace |
61
|
|
|
*/ |
62
|
|
|
public function addNamespace($namespace) |
63
|
|
|
{ |
64
|
|
|
$this->namespaces[] = $namespace; |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
public function addNamespaces(array $namespaces) |
68
|
|
|
{ |
69
|
4 |
|
foreach ($namespaces as $namespace) { |
70
|
|
|
$this->addNamespace($namespace); |
71
|
4 |
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function __construct($namespaces = ['']) |
75
|
|
|
{ |
76
|
4 |
|
$this->namespaces = $namespaces; |
77
|
|
|
} |
78
|
4 |
|
|
79
|
|
|
/** |
80
|
|
|
* @param $module |
81
|
4 |
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected static function generateModuleNameNamespace($module, $namespace = null) |
84
|
|
|
{ |
85
|
|
|
$namespace = $namespace ?: reset(self::instance()->namespaces); |
86
|
|
|
$name = $namespace . 'Modules\\'; |
87
|
|
|
$module = strtolower($module) == 'default' ? 'Frontend' : $module; |
88
|
|
|
$name .= $module; |
89
|
|
|
return $name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
protected static function formatControllerName($name) |
97
|
|
|
{ |
98
|
|
|
$name = str_replace('_', '\\', $name) . "Controller"; |
99
|
|
|
|
100
|
|
|
return $name; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function generateControllerNamespacedVariations($module,$controller): array |
104
|
|
|
{ |
105
|
|
|
$return = []; |
106
|
|
|
foreach ($this->namespaces as $namespace) { |
107
|
|
|
$return[] = static::generateControllerNamespaced($module, $controller, $namespace); |
108
|
|
|
} |
109
|
|
|
return $return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|