1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) Florian Krämer |
4
|
|
|
* Licensed under The MIT License |
5
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) Florian Krämer |
9
|
|
|
* @link https://github.com/burzum/cakephp-service-layer |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
12
|
|
|
*/ |
13
|
|
|
declare(strict_types = 1); |
14
|
|
|
|
15
|
|
|
namespace Burzum\Cake\Generator\Task; |
16
|
|
|
|
17
|
|
|
use Cake\Core\App; |
18
|
|
|
use Cake\Core\Plugin; |
19
|
|
|
use Cake\Filesystem\Folder; |
20
|
|
|
use IdeHelper\Generator\Task\TaskInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ServiceTask |
24
|
|
|
*/ |
25
|
|
|
class ServiceTask implements TaskInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Aliases |
29
|
|
|
* |
30
|
|
|
* @var string[] |
31
|
|
|
*/ |
32
|
|
|
protected $aliases = [ |
33
|
|
|
'\Burzum\Cake\Service\ServiceAwareTrait::loadService(0)', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Buffer |
38
|
|
|
* |
39
|
|
|
* @var string[]|null |
40
|
|
|
*/ |
41
|
|
|
protected static $services; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public static function clearBuffer() |
47
|
|
|
{ |
48
|
|
|
static::$services = null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
1 |
|
public function collect() |
55
|
|
|
{ |
56
|
1 |
|
$map = []; |
57
|
|
|
|
58
|
1 |
|
$services = $this->collectServices(); |
59
|
1 |
|
foreach ($services as $service => $className) { |
60
|
1 |
|
$map[$service] = '\\' . $className . '::class'; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$result = []; |
64
|
1 |
|
foreach ($this->aliases as $alias) { |
65
|
1 |
|
$result[$alias] = $map; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $result; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string[] |
73
|
|
|
*/ |
74
|
1 |
|
protected function collectServices() |
75
|
|
|
{ |
76
|
1 |
|
if (static::$services !== null) { |
77
|
|
|
return static::$services; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$services = []; |
81
|
|
|
|
82
|
1 |
|
$folders = App::path('Service'); |
83
|
1 |
|
foreach ($folders as $folder) { |
84
|
1 |
|
$services = $this->addServices($services, $folder); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
$plugins = Plugin::loaded(); |
88
|
1 |
|
foreach ($plugins as $plugin) { |
89
|
|
|
$folders = App::path('Service', $plugin); |
90
|
|
|
foreach ($folders as $folder) { |
91
|
|
|
$services = $this->addServices($services, $folder, null, $plugin); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
static::$services = $services; |
96
|
|
|
|
97
|
1 |
|
return $services; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string[] $services Services array |
102
|
|
|
* @param string $path Path |
103
|
|
|
* @param string|null $subFolder Sub folder |
104
|
|
|
* @param string|null $plugin Plugin |
105
|
|
|
* @return string[] |
106
|
|
|
*/ |
107
|
1 |
|
protected function addServices(array $services, $path, $subFolder = null, $plugin = null) |
108
|
|
|
{ |
109
|
1 |
|
$folderContent = (new Folder($path))->read(Folder::SORT_NAME, true); |
110
|
|
|
|
111
|
1 |
|
foreach ($folderContent[1] as $file) { |
112
|
1 |
|
preg_match('/^(.+)Service\.php$/', $file, $matches); |
113
|
1 |
|
if (!$matches) { |
114
|
|
|
continue; |
115
|
|
|
} |
116
|
1 |
|
$service = $matches[1]; |
117
|
1 |
|
if ($subFolder) { |
118
|
1 |
|
$service = $subFolder . '/' . $service; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
if ($plugin) { |
122
|
|
|
$service = $plugin . '.' . $service; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
$className = App::className($service, 'Service', 'Service'); |
126
|
1 |
|
if (!$className) { |
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
$services[$service] = $className; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
foreach ($folderContent[0] as $subDirectory) { |
134
|
1 |
|
$subFolder = $subFolder ? $subFolder . '/' . $subDirectory : $subDirectory; |
135
|
1 |
|
$services += $this->addServices($services, $path . $subDirectory . DS, $subFolder, $plugin); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return $services; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|