|
1
|
|
|
<?php |
|
2
|
|
|
namespace Burzum\Cake\Generator\Task; |
|
3
|
|
|
|
|
4
|
|
|
use Cake\Core\App; |
|
5
|
|
|
use Cake\Core\Plugin; |
|
6
|
|
|
use Cake\Filesystem\Folder; |
|
7
|
|
|
use IdeHelper\Generator\Task\TaskInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ServiceTask implements TaskInterface { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $aliases = [ |
|
15
|
|
|
'\Burzum\Cake\Service\ServiceAwareTrait::loadService(0)', |
|
16
|
|
|
'\ServiceAwareTrait::loadService(0)', |
|
17
|
|
|
]; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Buffer |
|
21
|
|
|
* |
|
22
|
|
|
* @var array|null |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static $services; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function clearBuffer() { |
|
30
|
|
|
static::$services = null; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function collect() { |
|
37
|
|
|
$map = []; |
|
38
|
|
|
|
|
39
|
|
|
$services = $this->collectServices(); |
|
40
|
|
|
foreach ($services as $service => $className) { |
|
41
|
|
|
$map[$service] = '\\' . $className . '::class'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$result = []; |
|
45
|
|
|
foreach ($this->aliases as $alias) { |
|
46
|
|
|
$result[$alias] = $map; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $result; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string[] |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function collectServices() { |
|
56
|
|
|
if (static::$services !== null) { |
|
57
|
|
|
return static::$services; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$services = []; |
|
61
|
|
|
|
|
62
|
|
|
$folders = App::path('Service'); |
|
63
|
|
|
foreach ($folders as $folder) { |
|
64
|
|
|
$services = $this->addServices($services, $folder); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$plugins = Plugin::loaded(); |
|
68
|
|
|
foreach ($plugins as $plugin) { |
|
|
|
|
|
|
69
|
|
|
$folders = App::path('Service', $plugin); |
|
70
|
|
|
foreach ($folders as $folder) { |
|
71
|
|
|
$services = $this->addServices($services, $folder, $plugin); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
static::$services = $services; |
|
76
|
|
|
|
|
77
|
|
|
return $services; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array $services |
|
82
|
|
|
* @param string $folder |
|
83
|
|
|
* @param string|null $plugin |
|
84
|
|
|
* |
|
85
|
|
|
* @return string[] |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function addServices(array $services, $folder, $plugin = null) { |
|
88
|
|
|
$folderContent = (new Folder($folder))->read(Folder::SORT_NAME, true); |
|
89
|
|
|
|
|
90
|
|
|
foreach ($folderContent[1] as $file) { |
|
91
|
|
|
preg_match('/^(.+)Service\.php$/', $file, $matches); |
|
92
|
|
|
if (!$matches) { |
|
|
|
|
|
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
$service = $matches[1]; |
|
96
|
|
|
if ($plugin) { |
|
|
|
|
|
|
97
|
|
|
$service = $plugin . '.' . $service; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$className = App::className($service, 'Service', 'Service'); |
|
101
|
|
|
if (!$className) { |
|
|
|
|
|
|
102
|
|
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$services[$service] = $className; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $services; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
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.