1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains the Concord helper functions |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016 Attila Fulop |
6
|
|
|
* @author Attila Fulop |
7
|
|
|
* @license MIT |
8
|
|
|
* @since 2016-09-25 |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
use Konekt\Concord\Contracts\Concord; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Converts a fully qualified classname to a string (backslashes to dots, parts to snake case) |
15
|
|
|
* |
16
|
|
|
* Eg.: '\App\Services\BamBamService' -> 'app.services.bam_bam_service' |
17
|
|
|
* |
18
|
|
|
* @param string $classname |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
function classpath_to_slug($classname) |
23
|
|
|
{ |
24
|
|
|
$parts = explode('\\', $classname); |
25
|
|
|
// Remove first part if empty ie. begins with \ |
26
|
|
|
if (empty($parts[0])) { |
27
|
|
|
$parts = array_except($parts, 0); |
28
|
|
|
} |
29
|
|
|
// Remove last part if empty ie. ends to \ |
30
|
|
|
if (empty($parts[count($parts) - 1])) { |
31
|
|
|
$parts = array_except($parts, count($parts) - 1); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
array_walk($parts, function (&$part) { |
35
|
|
|
$part = snake_case($part); |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
return implode('.', $parts); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Counterpart of classpath_to_str, that converts the string back to a fully qualified classname |
43
|
|
|
* |
44
|
|
|
* Eg.: 'app.services.bam_bam_service' -> '\App\Services\BamBamService' |
45
|
|
|
* |
46
|
|
|
* @see classpath_to_str() |
47
|
|
|
* |
48
|
|
|
* @param string $str |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
function slug_to_classpath($str) |
53
|
|
|
{ |
54
|
|
|
$parts = explode('.', $str); |
55
|
|
|
|
56
|
|
|
array_walk($parts, function (&$part) { |
57
|
|
|
$part = studly_case($part); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
return implode('\\', $parts); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns a standard module name based on the module provider's classname |
65
|
|
|
* |
66
|
|
|
* Eg.: '\Vendor\Module\Providers\ModuleServiceProvider' -> 'vendor.module' |
67
|
|
|
* 'App\Modules\Billing' -> 'billing' |
68
|
|
|
* |
69
|
|
|
* @param string $classname |
70
|
|
|
* @param null|\Konekt\Concord\Contracts\Convention $convention |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
function concord_module_id($classname, $convention = null) |
75
|
|
|
{ |
76
|
|
|
$convention = $convention ?: concord()->getConvention(); |
77
|
|
|
$modulesFolder = $convention->modulesFolder(); |
78
|
|
|
// Check if '\Modules\' is part of the namespace |
79
|
|
|
$p = strrpos($classname, "\\$modulesFolder\\"); |
80
|
|
|
// if no \Modules\, but starts with 'Modules\' that's also a match |
81
|
|
|
$p = false === $p ? strpos($classname, "$modulesFolder\\") : $p; |
|
|
|
|
82
|
|
|
if (false !== $p) { |
|
|
|
|
83
|
|
|
$parts = explode('\\', substr($classname, $p + strlen($modulesFolder) + 1)); |
84
|
|
|
$vendorAndModule = empty($parts[0]) ? array_only($parts, 1) : array_only($parts, 0); |
85
|
|
|
} else { |
86
|
|
|
$parts = explode('\\', $classname); |
87
|
|
|
$vendorAndModule = empty($parts[0]) ? array_only($parts, [1,2]) : array_only($parts, [0,1]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
array_walk($vendorAndModule, function (&$part) { |
91
|
|
|
$part = snake_case($part); |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
return implode('.', $vendorAndModule); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Shortcut function for returning helper instances by their name |
99
|
|
|
* |
100
|
|
|
* @param string $name The name of the helper |
101
|
|
|
* @param array $arguments Optional arguments to pass to the helper class |
102
|
|
|
* |
103
|
|
|
* @return object|null |
104
|
|
|
*/ |
105
|
|
|
function helper($name, $arguments = []) |
106
|
|
|
{ |
107
|
|
|
return concord()->helper($name, $arguments); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the concord instance |
112
|
|
|
* |
113
|
|
|
* @return Concord |
114
|
|
|
*/ |
115
|
|
|
function concord() |
116
|
|
|
{ |
117
|
|
|
return app('concord'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the classname shortened (no namespace, base class name only, snake_case |
122
|
|
|
* |
123
|
|
|
* @param string $classname |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
function shorten($classname) |
128
|
|
|
{ |
129
|
|
|
return snake_case(class_basename($classname)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Shorthand function for returning an enum object by it's short name |
135
|
|
|
* |
136
|
|
|
* @param string $shortname The short name of the enum |
137
|
|
|
* @param mixed $value The value to create the enum with |
138
|
|
|
* |
139
|
|
|
* @return \Konekt\Enum\Enum |
140
|
|
|
*/ |
141
|
|
|
function enum($shortname, $value = null) |
142
|
|
|
{ |
143
|
|
|
$abstract = concord()->short($shortname); |
144
|
|
|
if ($abstract && $class = concord()->enum($abstract)) { |
|
|
|
|
145
|
|
|
return new $class($value); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|