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