1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\helpers; |
3
|
|
|
|
4
|
|
|
use keeko\tools\services\CommandService; |
5
|
|
|
use keeko\tools\utils\NamespaceResolver; |
6
|
|
|
use Symfony\Component\Process\ExecutableFinder; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
use Symfony\Component\Process\ProcessUtils; |
9
|
|
|
|
10
|
|
|
trait InitCommandHelperTrait { |
11
|
|
|
|
12
|
|
|
private $gitConfig; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @return CommandService |
16
|
|
|
*/ |
17
|
|
|
abstract protected function getService(); |
18
|
|
|
|
19
|
|
|
private function getPackage() { |
20
|
|
|
return $this->getService()->getPackageService()->getPackage(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
private function getPackageKeeko($type) { |
24
|
|
|
$keeko = $this->getPackage()->getKeeko(); |
25
|
|
|
$pkg = $keeko->getKeekoPackage($type); |
26
|
|
|
|
27
|
|
|
if ($pkg == null) { |
28
|
|
|
throw new \Exception(sprintf('Unknown package type <%s>', $type)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $pkg; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function getPackageTitle() { |
35
|
|
|
$input = $this->getService()->getIOService()->getInput(); |
36
|
|
|
$type = $this->getPackageType(); |
37
|
|
|
$keeko = $this->getPackageKeeko($type); |
38
|
|
|
$pkgTitle = $keeko === null ? null : $keeko->getTitle(); |
39
|
|
|
$title = $input->getOption('title'); |
40
|
|
|
$title = $title === null && !empty($pkgTitle) ? $pkgTitle : $title; |
41
|
|
|
|
42
|
|
|
// fallback to default value |
43
|
|
|
if ($title === null) { |
44
|
|
|
$title = ucwords(str_replace('/', ' ', $input->getOption('name'))); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $title; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function getPackageClass() { |
51
|
|
|
$input = $this->getService()->getIOService()->getInput(); |
52
|
|
|
$type = $this->getPackageType(); |
53
|
|
|
$keeko = $this->getPackageKeeko($type); |
54
|
|
|
$pkgClass = $keeko === null ? null : $keeko->getClass(); |
55
|
|
|
$classname = $input->getOption('classname'); |
56
|
|
|
$classname = $classname === null && !empty($pkgClass) ? $pkgClass : $classname; |
57
|
|
|
|
58
|
|
|
// default value |
59
|
|
|
if ($classname === null) { |
60
|
|
|
$pkgName = $this->getPackage()->getFullName(); |
61
|
|
|
$parts = explode('/', $pkgName); |
62
|
|
|
$ns = $input->getOption('namespace'); |
63
|
|
|
$namespace = !empty($ns) ? $ns : str_replace('/', '\\', $pkgName); |
64
|
|
|
$classname = $namespace . '\\' . ucfirst($parts[1]); |
65
|
|
|
|
66
|
|
|
// suffix |
67
|
|
|
if ($type === 'module') { |
68
|
|
|
$classname .= 'Module'; |
69
|
|
|
} else if ($type === 'app') { |
70
|
|
|
$classname .= 'Application'; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $classname; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function getPackageType() { |
78
|
|
|
$input = $this->getService()->getIOService()->getInput(); |
79
|
|
|
$type = $input->getOption('type'); |
80
|
|
|
$pkgType = $this->getPackage()->getType(); |
81
|
|
|
return $type === null && !empty($pkgType) |
82
|
|
|
? str_replace('keeko-', '', $pkgType) |
83
|
|
|
: $type; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function getPackageName() { |
87
|
|
|
$input = $this->getService()->getIOService()->getInput(); |
88
|
|
|
$name = $input->getOption('name'); |
89
|
|
|
$pkgName = $this->getPackage()->getFullName(); |
90
|
|
|
return $name === null && !empty($pkgName) ? $pkgName : $name; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function getPackageDescription() { |
94
|
|
|
$input = $this->getService()->getIOService()->getInput(); |
95
|
|
|
$desc = $input->getOption('description'); |
96
|
|
|
$pkgDesc = $this->getPackage()->getDescription(); |
97
|
|
|
return $desc === null && !empty($pkgDesc) ? $pkgDesc : $desc; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function getPackageLicense() { |
101
|
|
|
$input = $this->getService()->getIOService()->getInput(); |
102
|
|
|
$license = $input->getOption('license'); |
103
|
|
|
$pkgLicense = $this->getPackage()->getLicense(); |
104
|
|
|
return $license === null && !empty($pkgLicense) ? $pkgLicense : $license; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function hasAutoload() { |
108
|
|
|
return NamespaceResolver::getNamespace('src', $this->package); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function validateName($name) { |
112
|
|
|
if (!preg_match('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}', $name)) { |
113
|
|
|
throw new \InvalidArgumentException( |
114
|
|
|
'The package name ' . $name . ' is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+' |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function setAutoload($namespace) { |
120
|
|
|
$autoload = $this->getPackage()->getAutoload(); |
121
|
|
|
|
122
|
|
|
// remove existing src/ entry |
123
|
|
|
$autoload->getPsr0()->removePath('src'); |
124
|
|
|
$autoload->getPsr4()->removePath('src'); |
125
|
|
|
|
126
|
|
|
// add src/ to psr4 |
127
|
|
|
$autoload->getPsr4()->setPath($namespace, 'src/'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function getGitConfig() { |
|
|
|
|
131
|
|
|
if (null !== $this->gitConfig) { |
132
|
|
|
return $this->gitConfig; |
133
|
|
|
} |
134
|
|
|
$finder = new ExecutableFinder(); |
135
|
|
|
$gitBin = $finder->find('git'); |
136
|
|
|
$cmd = new Process(sprintf('%s config -l', ProcessUtils::escapeArgument($gitBin))); |
137
|
|
|
$cmd->run(); |
138
|
|
|
if ($cmd->isSuccessful()) { |
139
|
|
|
$this->gitConfig = []; |
140
|
|
|
$matches = []; |
141
|
|
|
preg_match_all('{^([^=]+)=(.*)$}m', $cmd->getOutput(), $matches, PREG_SET_ORDER); |
142
|
|
|
foreach ($matches as $match) { |
|
|
|
|
143
|
|
|
$this->gitConfig[$match[1]] = $match[2]; |
144
|
|
|
} |
145
|
|
|
return $this->gitConfig; |
146
|
|
|
} |
147
|
|
|
return $this->gitConfig = []; |
148
|
|
|
} |
149
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: