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\CakeServiceLayer\Generator\Task; |
16
|
|
|
|
17
|
|
|
use Cake\Core\App; |
18
|
|
|
use Cake\Core\Plugin; |
19
|
|
|
use Cake\Filesystem\Folder; |
20
|
|
|
use IdeHelper\Generator\Directive\Override; |
21
|
|
|
use IdeHelper\Generator\Task\TaskInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ServiceTask |
25
|
|
|
*/ |
26
|
|
|
class ServiceTask implements TaskInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Aliases |
30
|
|
|
* |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
protected $aliases = [ |
34
|
|
|
'\Burzum\CakeServiceLayer\Service\ServiceAwareTrait::loadService(0)', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Buffer |
39
|
|
|
* |
40
|
|
|
* @var string[]|null |
41
|
|
|
*/ |
42
|
|
|
protected static $services; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public static function clearBuffer() |
48
|
|
|
{ |
49
|
|
|
static::$services = null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return \IdeHelper\Generator\Directive\BaseDirective[] |
54
|
|
|
*/ |
55
|
1 |
|
public function collect(): array |
56
|
|
|
{ |
57
|
1 |
|
$map = []; |
58
|
|
|
|
59
|
1 |
|
$services = $this->collectServices(); |
60
|
1 |
|
foreach ($services as $service => $className) { |
61
|
1 |
|
$map[$service] = '\\' . $className . '::class'; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$result = []; |
65
|
1 |
|
foreach ($this->aliases as $alias) { |
66
|
1 |
|
$directive = new Override($alias, $map); |
67
|
1 |
|
$result[$directive->key()] = $directive; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string[] |
75
|
|
|
*/ |
76
|
1 |
|
protected function collectServices() |
77
|
|
|
{ |
78
|
1 |
|
if (static::$services !== null) { |
79
|
|
|
return static::$services; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$services = []; |
83
|
|
|
|
84
|
1 |
|
$folders = App::classPath('Service'); |
85
|
1 |
|
foreach ($folders as $folder) { |
86
|
1 |
|
$services = $this->addServices($services, $folder); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$plugins = Plugin::loaded(); |
90
|
1 |
|
foreach ($plugins as $plugin) { |
91
|
|
|
$folders = App::classPath('Service', $plugin); |
92
|
|
|
foreach ($folders as $folder) { |
93
|
|
|
$services = $this->addServices($services, $folder, null, $plugin); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
static::$services = $services; |
98
|
|
|
|
99
|
1 |
|
return $services; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string[] $services Services array |
104
|
|
|
* @param string $path Path |
105
|
|
|
* @param string|null $subFolder Sub folder |
106
|
|
|
* @param string|null $plugin Plugin |
107
|
|
|
* @return string[] |
108
|
|
|
*/ |
109
|
1 |
|
protected function addServices(array $services, $path, $subFolder = null, $plugin = null) |
110
|
|
|
{ |
111
|
1 |
|
$folderContent = (new Folder($path))->read(Folder::SORT_NAME, true); |
112
|
|
|
|
113
|
1 |
|
foreach ($folderContent[1] as $file) { |
114
|
1 |
|
preg_match('/^(.+)Service\.php$/', $file, $matches); |
115
|
1 |
|
if (!$matches) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
1 |
|
$service = $matches[1]; |
119
|
1 |
|
if ($subFolder) { |
120
|
1 |
|
$service = $subFolder . '/' . $service; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
if ($plugin) { |
124
|
|
|
$service = $plugin . '.' . $service; |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
$className = App::className($service, 'Service', 'Service'); |
128
|
1 |
|
if (!$className) { |
129
|
|
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
$services[$service] = $className; |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
foreach ($folderContent[0] as $subDirectory) { |
136
|
1 |
|
$nextSubFolder = $subFolder ? $subFolder . '/' . $subDirectory : $subDirectory; |
137
|
1 |
|
$services = $this->addServices($services, $path . $subDirectory . DS, $nextSubFolder, $plugin); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
return $services; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|