|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of Scout Extended. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Algolia Team <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Algolia\ScoutExtended\Helpers; |
|
15
|
|
|
|
|
16
|
|
|
use Illuminate\Console\Command; |
|
17
|
|
|
use Illuminate\Support\Str; |
|
18
|
|
|
use function in_array; |
|
19
|
|
|
use Laravel\Scout\Searchable; |
|
20
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
|
21
|
|
|
use Symfony\Component\Finder\Finder; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @internal |
|
25
|
|
|
*/ |
|
26
|
|
|
final class SearchableFinder |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $declaredClasses; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
|
35
|
|
|
*/ |
|
36
|
|
|
private $app; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* SearchableModelsFinder constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
17 |
|
public function __construct($app) |
|
45
|
|
|
{ |
|
46
|
17 |
|
$this->app = $app; |
|
47
|
17 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get a list of searchable models from the given command. |
|
51
|
|
|
* |
|
52
|
|
|
* @param \Illuminate\Console\Command $command |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
17 |
|
public function fromCommand(Command $command): array |
|
57
|
|
|
{ |
|
58
|
17 |
|
$searchables = (array) $command->argument('searchable'); |
|
59
|
|
|
|
|
60
|
17 |
|
if (empty($searchables) && empty($searchables = $this->find())) { |
|
61
|
|
|
throw new InvalidArgumentException('No searchable classes found.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
17 |
|
return $searchables; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get a list of searchable models. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string[] |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function find(): array |
|
73
|
|
|
{ |
|
74
|
2 |
|
$appNamespace = $this->app->getNamespace(); |
|
75
|
|
|
|
|
76
|
|
|
return array_values(array_filter($this->getProjectClasses(), function (string $class) use ($appNamespace) { |
|
77
|
2 |
|
return Str::startsWith($class, $appNamespace) && $this->isSearchableModel($class); |
|
78
|
2 |
|
})); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $class |
|
83
|
|
|
* |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
2 |
|
private function isSearchableModel($class): bool |
|
87
|
|
|
{ |
|
88
|
2 |
|
return in_array(Searchable::class, class_uses_recursive($class), true); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
2 |
|
private function getProjectClasses(): array |
|
95
|
|
|
{ |
|
96
|
2 |
|
if (self::$declaredClasses === null) { |
|
|
|
|
|
|
97
|
1 |
|
$configFiles = Finder::create()->files()->name('*.php')->in($this->app->path()); |
|
98
|
|
|
|
|
99
|
1 |
|
foreach ($configFiles->files() as $file) { |
|
100
|
1 |
|
require_once $file; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
self::$declaredClasses = get_declared_classes(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
return self::$declaredClasses; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|