1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\services; |
3
|
|
|
|
4
|
|
|
use keeko\framework\schema\PackageSchema; |
5
|
|
|
use phootwork\file\exception\FileException; |
6
|
|
|
use phootwork\file\File; |
7
|
|
|
use keeko\tools\utils\NamespaceResolver; |
8
|
|
|
use keeko\framework\schema\ActionSchema; |
9
|
|
|
use keeko\framework\schema\ModuleSchema; |
10
|
|
|
|
11
|
|
|
class PackageService extends AbstractService { |
12
|
|
|
|
13
|
|
|
/** @var PackageSchema */ |
14
|
|
|
private $package = null; |
15
|
|
|
private $keeko = null; |
16
|
|
|
private $module = null; |
17
|
|
|
private $app = null; |
18
|
|
|
private $namespace = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @throws FileException |
23
|
|
|
* @return PackageSchema |
24
|
|
|
*/ |
25
|
|
|
public function getPackage() { |
26
|
20 |
|
if ($this->package === null) { |
27
|
20 |
|
$file = new File($this->service->getProject()->getComposerFileName()); |
28
|
20 |
|
if ($file->exists()) { |
29
|
20 |
|
$this->package = PackageSchema::fromFile($file->getPathname()); |
30
|
17 |
|
} else { |
31
|
17 |
|
$this->package = new PackageSchema(); |
32
|
3 |
|
} |
33
|
|
|
} |
34
|
20 |
|
|
35
|
|
|
return $this->package; |
36
|
20 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the root namespace for this package |
40
|
|
|
* |
41
|
|
|
* @return string the namespace |
42
|
|
|
*/ |
43
|
|
|
public function getNamespace() { |
44
|
20 |
|
if ($this->namespace === null) { |
45
|
20 |
|
$input = $this->io->getInput(); |
46
|
20 |
|
$ns = $input->hasOption('namespace') |
47
|
20 |
|
? $input->getOption('namespace') |
48
|
20 |
|
: null; |
49
|
|
|
if ($ns === null) { |
50
|
20 |
|
$package = $this->service->getPackageService()->getPackage(); |
51
|
|
|
$ns = NamespaceResolver::getNamespace('src', $package); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->namespace = trim($ns, '\\'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->namespace; |
58
|
17 |
|
} |
59
|
17 |
|
|
60
|
17 |
|
/** |
61
|
17 |
|
* Returns the keeko node from the composer.json extra |
62
|
17 |
|
* |
63
|
|
|
* @return KeekoSchema |
64
|
17 |
|
*/ |
65
|
|
|
public function getKeeko() { |
66
|
|
|
if ($this->keeko === null) { |
67
|
|
|
$package = $this->getPackage(); |
68
|
|
|
$this->keeko = $package->getKeeko(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->keeko; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the keeko module schema |
76
|
|
|
* |
77
|
|
|
* @return ModuleSchema |
78
|
|
|
*/ |
79
|
|
|
public function getModule() { |
80
|
|
|
if ($this->module === null) { |
81
|
|
|
$keeko = $this->getKeeko(); |
82
|
|
|
$this->module = $keeko->getModule(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->module; |
86
|
|
|
} |
87
|
8 |
|
|
88
|
8 |
|
/** |
89
|
8 |
|
* Returns the keeko app schema |
90
|
5 |
|
* |
91
|
|
|
* @return AppSchema |
92
|
|
|
*/ |
93
|
7 |
|
public function getApp() { |
94
|
|
|
if ($this->app === null) { |
95
|
|
|
$keeko = $this->getKeeko(); |
96
|
5 |
|
$this->app = $keeko->getApp(); |
97
|
5 |
|
} |
98
|
5 |
|
|
99
|
5 |
|
return $this->app; |
100
|
5 |
|
} |
101
|
5 |
|
|
102
|
5 |
|
/** |
103
|
|
|
* Returns an action |
104
|
|
|
* |
105
|
5 |
|
* @param string $name |
106
|
5 |
|
* @return ActionSchema |
107
|
|
|
*/ |
108
|
|
|
public function getAction($name) { |
109
|
|
|
$module = $this->getModule(); |
110
|
|
|
if ($module !== null && $module->hasAction($name)) { |
111
|
|
|
return $module->getAction($name); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getActionType($actionName, $modelName) { |
118
|
|
|
$input = $this->io->getInput(); |
119
|
|
|
$type = $input->hasOption('type') ? $input->getOption('type') : null; |
120
|
|
|
if ($type === null) { |
121
|
16 |
|
if (($pos = strrpos($actionName, '-')) !== false) { |
122
|
16 |
|
$type = substr($actionName, $pos + 1); |
123
|
13 |
|
} else if ($modelName == $actionName) { |
124
|
13 |
|
$type = 'read'; |
125
|
16 |
|
} |
126
|
16 |
|
} |
127
|
|
|
return $type; |
128
|
|
|
} |
129
|
|
|
|
130
|
16 |
|
public function savePackage(PackageSchema $package = null) { |
131
|
16 |
|
if ($package === null) { |
132
|
|
|
$package = $this->getPackage(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$filename = $this->service->getProject()->getComposerFileName(); |
136
|
|
|
$this->service->getJsonService()->write($filename, $package->toArray()); |
137
|
|
|
$this->io->writeln(sprintf('Package <info>%s</info> written at <info>%s</info>', $package->getFullName(), $filename)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// --- from model service ---- |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the operation (verb) of the action (if existent) |
144
|
|
|
* |
145
|
|
|
* @param ActionSchema $action |
146
|
|
|
* @return string|null |
147
|
|
|
*/ |
148
|
|
|
public function getOperationByAction(ActionSchema $action) { |
149
|
|
|
$actionName = $action->getName(); |
150
|
|
|
$operation = null; |
151
|
|
|
if (($pos = strpos($actionName, '-')) !== false) { |
152
|
|
|
$operation = substr($actionName, $pos + 1); |
153
|
|
|
} |
154
|
|
|
return $operation; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns whether this is a crud operation action |
159
|
|
|
* (create, read, update, delete, list) |
160
|
|
|
* |
161
|
|
|
* @param ActionSchema $action |
162
|
|
|
* @return boolean |
163
|
|
|
*/ |
164
|
|
|
public function isCrudAction(ActionSchema $action) { |
165
|
|
|
$operation = $this->getOperationByAction($action); |
166
|
|
|
|
167
|
|
|
return in_array($operation, ['create', 'read', 'update', 'delete', 'list']); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|