1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cerbero\LaravelEnum; |
6
|
|
|
|
7
|
|
|
use Cerbero\Enum\Enums as BaseEnums; |
8
|
|
|
use Cerbero\LaravelEnum\Actions\OnCall; |
9
|
|
|
use Closure; |
10
|
|
|
use Generator; |
11
|
|
|
use Illuminate\Support\Arr; |
12
|
|
|
use Illuminate\Support\Facades\App; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
use UnitEnum; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The enums manager. |
18
|
|
|
*/ |
19
|
|
|
class Enums extends BaseEnums |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The logic to resolve the translation key. |
23
|
|
|
* |
24
|
|
|
* @var ?Closure(UnitEnum $case, string $method): string |
25
|
|
|
*/ |
26
|
|
|
protected static ?Closure $translateFrom = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
1 |
|
* The glob paths to find enums in. |
30
|
|
|
* |
31
|
1 |
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
protected static array $paths = ['app/Enums']; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set the logic to resolve the translation key. |
37
|
4 |
|
* |
38
|
|
|
* @param callable(UnitEnum $case, string $method): string $callback |
39
|
4 |
|
*/ |
40
|
1 |
|
public static function translateFrom(callable $callback): void |
41
|
4 |
|
{ |
42
|
|
|
static::$translateFrom = $callback(...); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Retrieve the translation key for the given case. |
47
|
|
|
*/ |
48
|
|
|
public static function resolveTranslationKey(UnitEnum $case, ?string $method = null): string |
49
|
7 |
|
{ |
50
|
|
|
return static::$translateFrom |
51
|
7 |
|
? (static::$translateFrom)($case, (string) $method) |
52
|
|
|
: sprintf('enums.%s.%s%s', $case::class, $case->name, $method ? ".{$method}" : ''); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the glob paths to find all the application enums. |
57
|
|
|
*/ |
58
|
|
|
public static function paths(string ...$paths): void |
59
|
|
|
{ |
60
|
|
|
static::$paths = $paths; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Yield the namespaces of all the application enums. |
65
|
|
|
* |
66
|
|
|
* @return Generator<int, class-string> |
67
|
|
|
*/ |
68
|
|
|
public static function namespaces(): Generator |
69
|
|
|
{ |
70
|
|
|
$composer = json_decode((string) file_get_contents(App::basePath('composer.json')), true); |
71
|
|
|
/** @var array<string, string> */ |
72
|
|
|
$psr4 = Arr::get((array) $composer, 'autoload.psr-4', []); |
73
|
|
|
|
74
|
|
|
foreach (static::$paths as $relativePath) { |
75
|
|
|
$glob = App::basePath(trim($relativePath, '/')) . '/*.php'; |
76
|
|
|
|
77
|
|
|
foreach (glob($glob) ?: [] as $path) { |
78
|
|
|
foreach ($psr4 as $namespace => $relative) { |
79
|
|
|
$absolute = Str::finish(App::basePath($relative), '/'); |
80
|
|
|
|
81
|
|
|
if (str_starts_with($path, $absolute)) { |
82
|
|
|
$enum = str_replace([$absolute, '/', '.php'], [$namespace, '\\', ''], $path); |
83
|
|
|
|
84
|
|
|
if (enum_exists($enum)) { |
|
|
|
|
85
|
|
|
yield $enum; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Handle the call to an inaccessible case method. |
95
|
|
|
* |
96
|
|
|
* @param array<array-key, mixed> $arguments |
|
|
|
|
97
|
|
|
*/ |
98
|
|
|
public static function handleCall(object $case, string $name, array $arguments): mixed |
99
|
|
|
{ |
100
|
|
|
return (static::$onCall ?: new OnCall())($case, $name, $arguments); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths