1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WsdlToPhp\PackageGenerator\File; |
4
|
|
|
|
5
|
|
|
use Composer\Console\Application; |
6
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
7
|
|
|
use WsdlToPhp\PackageGenerator\Model\EmptyModel; |
8
|
|
|
|
9
|
|
|
class Composer extends AbstractFile |
10
|
|
|
{ |
11
|
|
|
const JSON_FILE_EXTENSION = 'json'; |
12
|
|
|
/** |
13
|
|
|
* Tests purpose: do not run composer update command |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
protected $runComposerUpdate = true; |
17
|
|
|
/** |
18
|
|
|
* @see \WsdlToPhp\PackageGenerator\File\AbstractFile::writeFile() |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
20 |
|
protected function writeFile() |
22
|
|
|
{ |
23
|
20 |
|
$composer = new Application(); |
24
|
20 |
|
$composer->setAutoExit(false); |
25
|
20 |
|
$composer->run(new ArrayInput([ |
26
|
20 |
|
'command' => 'init', |
27
|
|
|
'--verbose' => true, |
28
|
|
|
'--no-interaction' => true, |
29
|
20 |
|
'--name' => $this->getGenerator()->getOptionComposerName(), |
30
|
20 |
|
'--description' => sprintf('Package generated from %s using wsdltophp/packagegenerator', $this->getGenerator()->getWsdl()->getName()), |
31
|
|
|
'--require' => [ |
32
|
|
|
'php:>=5.3.3', |
33
|
|
|
'ext-soap:*', |
34
|
|
|
'ext-mbstring:*', |
35
|
|
|
'wsdltophp/packagebase:~2.0', |
36
|
|
|
], |
37
|
20 |
|
'--working-dir' => $this->getGenerator()->getOptionDestination(), |
38
|
|
|
])); |
39
|
20 |
|
$this->completeComposerJson(); |
40
|
20 |
|
if ($this->getRunComposerUpdate() === true) { |
41
|
10 |
|
return $composer->run(new ArrayInput([ |
42
|
10 |
|
'command' => 'update', |
43
|
|
|
'--verbose' => true, |
44
|
|
|
'--optimize-autoloader' => true, |
45
|
|
|
'--no-dev' => true, |
46
|
10 |
|
'--working-dir' => $this->getGenerator()->getOptionDestination(), |
47
|
|
|
])); |
48
|
|
|
} |
49
|
10 |
|
} |
50
|
|
|
/** |
51
|
|
|
* @return Composer |
52
|
|
|
*/ |
53
|
20 |
|
protected function completeComposerJson() |
54
|
|
|
{ |
55
|
20 |
|
$content = $this->getComposerFileContent(); |
56
|
20 |
|
if (is_array($content) && !empty($content)) { |
57
|
20 |
|
$this->addAutoloadToComposerJson($content)->addComposerSettings($content); |
58
|
|
|
} |
59
|
20 |
|
return $this->setComposerFileContent($content); |
60
|
|
|
} |
61
|
|
|
/** |
62
|
|
|
* @return Composer |
63
|
|
|
*/ |
64
|
20 |
|
protected function addAutoloadToComposerJson(array &$content) |
65
|
|
|
{ |
66
|
20 |
|
$content['autoload'] = [ |
67
|
20 |
|
'psr-4' => $this->getPsr4Autoload(), |
68
|
|
|
]; |
69
|
20 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
/** |
72
|
|
|
* @return Composer |
73
|
|
|
*/ |
74
|
20 |
|
protected function addComposerSettings(array &$content) |
75
|
|
|
{ |
76
|
20 |
|
$content = array_merge_recursive($content, $this->getGenerator()->getOptionComposerSettings()); |
77
|
20 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
20 |
|
protected function getPsr4Autoload() |
83
|
|
|
{ |
84
|
20 |
|
$namespace = new EmptyModel($this->getGenerator(), ''); |
85
|
20 |
|
if ($namespace->getNamespace() !== '') { |
86
|
|
|
$namespaceKey = sprintf('%s\\', $namespace->getNamespace()); |
87
|
|
|
} else { |
88
|
20 |
|
$namespaceKey = ''; |
89
|
|
|
} |
90
|
20 |
|
$src = rtrim($this->generator->getOptionSrcDirname(), DIRECTORY_SEPARATOR); |
91
|
|
|
return [ |
92
|
20 |
|
$namespaceKey => sprintf('./%s', empty($src) ? '' : $src . DIRECTORY_SEPARATOR), |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
/** |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
20 |
|
protected function getComposerFileContent() |
99
|
|
|
{ |
100
|
20 |
|
$content = []; |
101
|
20 |
|
$composerFilePath = $this->getComposerFilePath(); |
102
|
20 |
|
if (!empty($composerFilePath)) { |
103
|
20 |
|
$content = json_decode(file_get_contents($composerFilePath), true); |
104
|
|
|
} |
105
|
20 |
|
return $content; |
106
|
|
|
} |
107
|
|
|
/** |
108
|
|
|
* @param array $content |
109
|
|
|
* @return Composer |
110
|
|
|
*/ |
111
|
20 |
|
protected function setComposerFileContent(array $content) |
112
|
|
|
{ |
113
|
20 |
|
$composerFilePath = $this->getComposerFilePath(); |
114
|
20 |
|
if (!empty($composerFilePath)) { |
115
|
20 |
|
file_put_contents($composerFilePath, self::encodeToJson($content)); |
116
|
|
|
} |
117
|
20 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
/** |
120
|
|
|
* @param array $content |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
20 |
|
protected static function encodeToJson($content) |
124
|
|
|
{ |
125
|
20 |
|
return json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
126
|
|
|
} |
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
20 |
|
protected function getComposerFilePath() |
131
|
|
|
{ |
132
|
20 |
|
$path = realpath(sprintf('%s/composer.json', $this->getGenerator()->getOptionDestination())); |
133
|
20 |
|
return false === $path ? '' : $path; |
134
|
|
|
} |
135
|
|
|
/** |
136
|
|
|
* @param bool $runComposerUpdate |
137
|
|
|
* @return Composer |
138
|
|
|
*/ |
139
|
12 |
|
public function setRunComposerUpdate($runComposerUpdate) |
140
|
|
|
{ |
141
|
12 |
|
$this->runComposerUpdate = $runComposerUpdate; |
142
|
12 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
/** |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
22 |
|
public function getRunComposerUpdate() |
148
|
|
|
{ |
149
|
22 |
|
return $this->runComposerUpdate; |
150
|
|
|
} |
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
12 |
|
public function getFileExtension() |
155
|
|
|
{ |
156
|
12 |
|
return self::JSON_FILE_EXTENSION; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|