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