-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathModelFinder.php
More file actions
57 lines (47 loc) · 1.9 KB
/
ModelFinder.php
File metadata and controls
57 lines (47 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace System\Helpers;
use Winter\Storm\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Finder\Finder;
class ModelFinder
{
/**
* Find all models in core and active plugins.
*
* @return Collection
*/
public static function findModels(): array
{
$models = [];
$models[] = static::findModuleModels();
$models[] = static::findActivePluginsModels();
return collect($models)->flatten()->all();
}
public static function findModuleModels(): array
{
$modulesPath = base_path() . '/modules';
$models = collect(Finder::create()->in($modulesPath)->path('/models/')->notPath('/tests/')->files()->name('/^[A-Z]{1}.+\.php$/'))
->map(function ($model) use ($modulesPath) {
$modelPath = str_replace(['/', '.php'], ['\\', ''], Str::after($model->getRealPath(), realpath($modulesPath).DIRECTORY_SEPARATOR));
return ucwords($modelPath, '\\');
});
return $models->values()->all();
}
public static function findActivePluginsModels(): array
{
$models = [];
$pm = \System\Classes\PluginManager::instance();
$pluginsPaths = collect($pm->getPlugins())->map(function ($plugin) use ($pm) {
return $pm->getPluginPath($plugin);
})->filter(function ($path) {
return File::exists($path . '/models');
})->each(function ($path) use (&$models) {
$modelPaths = Finder::create()->in($path . '/models')->files()->name('/^[A-Z]{1}.+\.php$/');
$models[] = collect($modelPaths)->map(function ($model) {
$modelPath = str_replace(['/', '.php'], ['\\', ''], Str::after($model->getRealPath(), plugins_path().DIRECTORY_SEPARATOR));
return ucwords($modelPath, '\\');
})->all();
});
return collect($models)->flatten()->all();
}
}