|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Cake\Orm\Shell; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Console\Shell; |
|
6
|
|
|
use Cake\Core\App; |
|
7
|
|
|
use Cake\Core\Configure; |
|
8
|
|
|
use Cake\Core\Plugin; |
|
9
|
|
|
use Roave\BetterReflection\BetterReflection; |
|
10
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
|
11
|
|
|
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator; |
|
12
|
|
|
use WyriHaximus\React\Cake\Orm\AsyncTableGenerator; |
|
13
|
|
|
|
|
14
|
|
|
class GenerateShell extends Shell |
|
15
|
|
|
{ |
|
16
|
|
|
public function all() |
|
17
|
|
|
{ |
|
18
|
|
|
foreach (App::path('Model/Table') as $path) { |
|
19
|
|
|
if (is_dir($path)) { |
|
20
|
|
|
$this->iteratePath($path); |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
foreach (Plugin::loaded() as $plugin) { |
|
|
|
|
|
|
25
|
|
|
foreach (App::path('Model/Table', $plugin) as $path) { |
|
26
|
|
|
if (is_dir($path)) { |
|
27
|
|
|
$this->iteratePath($path); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function iteratePath($path) |
|
34
|
|
|
{ |
|
35
|
|
|
foreach ($this->setupIterator($path) as $item) { |
|
36
|
|
|
$this->iterateClasses($this->getClassByFile(current($item))); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function iterateClasses($classes) |
|
41
|
|
|
{ |
|
42
|
|
|
foreach ($classes as $class) { |
|
43
|
|
|
$className = $class->getName(); |
|
44
|
|
|
(new AsyncTableGenerator( |
|
45
|
|
|
Configure::read('WyriHaximus.React.Cake.Orm.Cache.AsyncTables') |
|
46
|
|
|
))->generate($className, true); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getClassByFile($fileName) |
|
51
|
|
|
{ |
|
52
|
|
|
return (new ClassReflector(new SingleFileSourceLocator($fileName, (new BetterReflection())->astLocator())))->getAllClasses(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set options for this console. |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Cake\Console\ConsoleOptionParser |
|
59
|
|
|
*/ |
|
60
|
|
|
// @codingStandardsIgnoreStart |
|
61
|
|
|
public function getOptionParser() |
|
62
|
|
|
{ |
|
63
|
|
|
// @codingStandardsIgnoreEnd |
|
64
|
|
|
return parent::getOptionParser()->addSubcommand( |
|
|
|
|
|
|
65
|
|
|
'all', |
|
66
|
|
|
[ |
|
67
|
|
|
'short' => 'a', |
|
68
|
|
|
// @codingStandardsIgnoreStart |
|
69
|
|
|
'help' => __('Searches and pregenerates all async tables it finds.'), |
|
70
|
|
|
// @codingStandardsIgnoreEnd |
|
71
|
|
|
] |
|
72
|
|
|
// @codingStandardsIgnoreStart |
|
73
|
|
|
)->description(__('Async table pregenerator')); |
|
74
|
|
|
// @codingStandardsIgnoreEnd |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function setupIterator($path) |
|
78
|
|
|
{ |
|
79
|
|
|
return new \RegexIterator(new \RecursiveIteratorIterator( |
|
80
|
|
|
new \RecursiveDirectoryIterator( |
|
81
|
|
|
$path, |
|
82
|
|
|
\FilesystemIterator::KEY_AS_PATHNAME | |
|
83
|
|
|
\FilesystemIterator::CURRENT_AS_FILEINFO | |
|
84
|
|
|
\FilesystemIterator::SKIP_DOTS |
|
85
|
|
|
), |
|
86
|
|
|
\RecursiveIteratorIterator::CHILD_FIRST, |
|
87
|
|
|
\RecursiveIteratorIterator::CATCH_GET_CHILD |
|
88
|
|
|
), '/.*?.php$/', \RegexIterator::GET_MATCH); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
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.