1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Cake\Orm\Shell; |
4
|
|
|
|
5
|
|
|
use BetterReflection\Reflector\ClassReflector; |
6
|
|
|
use BetterReflection\SourceLocator\Type\SingleFileSourceLocator; |
7
|
|
|
use Cake\Console\Shell; |
8
|
|
|
use Cake\Core\App; |
9
|
|
|
use Cake\Core\Plugin; |
10
|
|
|
use Cake\ORM\TableRegistry; |
11
|
|
|
use WyriHaximus\React\Cake\Orm\AsyncTableRegistry; |
12
|
|
|
|
13
|
|
|
class GenerateShell extends Shell |
14
|
|
|
{ |
15
|
|
|
public function all() |
16
|
|
|
{ |
17
|
|
|
foreach (App::path('Table') as $path) { |
18
|
|
|
if (is_dir($path)) { |
19
|
|
|
$this->iteratePath($path); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
foreach (Plugin::loaded() as $plugin) { |
|
|
|
|
24
|
|
|
foreach (App::path('Table', $plugin) as $path) { |
25
|
|
|
if (is_dir($path)) { |
26
|
|
|
$this->iteratePath($path); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function iteratePath($path) |
34
|
|
|
{ |
35
|
|
|
foreach ($this->setupIterator($path) as $item) { |
36
|
|
|
$this->iterateClasses($this->getClassByFile($item)); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function iterateClasses($classes) |
41
|
|
|
{ |
42
|
|
|
foreach ($classes as $class) { |
43
|
|
|
AsyncTableRegistry::get( |
44
|
|
|
TableRegistry::get( |
45
|
|
|
md5($class), |
46
|
|
|
[ |
47
|
|
|
'className' => $class, |
48
|
|
|
] |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getClassByFile($fileName) |
55
|
|
|
{ |
56
|
|
|
return (new ClassReflector(new SingleFileSourceLocator($fileName)))->getAllClasses(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function setupIterator($path) |
60
|
|
|
{ |
61
|
|
|
return new \RegexIterator(new \RecursiveIteratorIterator( |
62
|
|
|
new \RecursiveDirectoryIterator( |
63
|
|
|
$path, |
64
|
|
|
\FilesystemIterator::KEY_AS_PATHNAME | |
65
|
|
|
\FilesystemIterator::CURRENT_AS_FILEINFO | |
66
|
|
|
\FilesystemIterator::SKIP_DOTS |
67
|
|
|
), |
68
|
|
|
\RecursiveIteratorIterator::CHILD_FIRST, |
69
|
|
|
\RecursiveIteratorIterator::CATCH_GET_CHILD |
70
|
|
|
), '/.*?.php$/', \RegexIterator::GET_MATCH); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set options for this console. |
76
|
|
|
* |
77
|
|
|
* @return \Cake\Console\ConsoleOptionParser |
78
|
|
|
*/ |
79
|
|
|
// @codingStandardsIgnoreStart |
80
|
|
|
public function getOptionParser() |
81
|
|
|
{ |
82
|
|
|
// @codingStandardsIgnoreEnd |
83
|
|
|
return parent::getOptionParser()->addSubcommand( |
|
|
|
|
84
|
|
|
'all', |
85
|
|
|
[ |
86
|
|
|
'short' => 'a', |
87
|
|
|
// @codingStandardsIgnoreStart |
88
|
|
|
'help' => __('Searches and pregenerates all async tables it finds.'), |
89
|
|
|
// @codingStandardsIgnoreEnd |
90
|
|
|
] |
91
|
|
|
// @codingStandardsIgnoreStart |
92
|
|
|
)->description(__('Async table pregenerator')); |
93
|
|
|
// @codingStandardsIgnoreEnd |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.