1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: alex |
5
|
|
|
* Date: 1/06/20 |
6
|
|
|
* Time: 12:36 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace AlgoWeb\PODataLaravel\Models; |
10
|
|
|
|
11
|
|
|
use Cruxinator\ClassFinder\ClassFinder; |
12
|
|
|
use Illuminate\Database\Eloquent\Model; |
13
|
|
|
use Illuminate\Support\Facades\App; |
14
|
|
|
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerTrait; |
15
|
|
|
use Illuminate\Routing\Controller; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ClassReflectionHelper |
19
|
|
|
* @package AlgoWeb\PODataLaravel\Models |
20
|
|
|
*/ |
21
|
|
|
abstract class ClassReflectionHelper |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @throws \Exception |
25
|
|
|
* @return string[] |
26
|
|
|
*/ |
27
|
|
|
public static function getCandidateModels(): array |
28
|
|
|
{ |
29
|
|
|
return ClassFinder::getClasses( |
30
|
|
|
static::getAppNamespace(), |
31
|
|
|
function ($className) { |
32
|
|
|
return in_array(MetadataTrait::class, class_uses($className)) && |
33
|
|
|
is_subclass_of($className, Model::class); |
34
|
|
|
}, |
35
|
|
|
true |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
|
|
public static function getCandidateControllers(): array |
44
|
|
|
{ |
45
|
|
|
$classes = ClassFinder::getClasses( |
46
|
|
|
static::getAppNamespace(), |
47
|
|
|
function ($className) { |
48
|
|
|
return in_array(MetadataControllerTrait::class, class_uses($className)) && |
49
|
|
|
(App::make($className) instanceof Controller); |
50
|
|
|
}, |
51
|
|
|
true |
52
|
|
|
); |
53
|
|
|
$ends = array_reduce($classes, function ($carry, $item) { |
54
|
|
|
$carry[] = App::make($item); |
55
|
|
|
return $carry; |
56
|
|
|
}, []); |
57
|
|
|
return $ends; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public static function getAppNamespace(): string |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
$startName = App::getNamespace(); |
67
|
|
|
} catch (\Exception $e) { |
68
|
|
|
$startName = 'App'; |
69
|
|
|
} |
70
|
|
|
return $startName; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|