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 int|bool |
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 |
|
return 1; |
50
|
|
|
} |
51
|
|
|
/** |
52
|
|
|
* @return Composer |
53
|
|
|
*/ |
54
|
54 |
|
protected function completeComposerJson() |
55
|
|
|
{ |
56
|
54 |
|
$content = $this->getComposerFileContent(); |
57
|
54 |
|
if (is_array($content) && !empty($content)) { |
58
|
54 |
|
$this->addAutoloadToComposerJson($content)->addComposerSettings($content); |
59
|
27 |
|
} |
60
|
54 |
|
return $this->setComposerFileContent($content); |
61
|
|
|
} |
62
|
|
|
/** |
63
|
|
|
* @return Composer |
64
|
|
|
*/ |
65
|
54 |
|
protected function addAutoloadToComposerJson(array &$content) |
66
|
|
|
{ |
67
|
54 |
|
$content['autoload'] = [ |
68
|
54 |
|
'psr-4' => $this->getPsr4Autoload(), |
69
|
|
|
]; |
70
|
54 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
/** |
73
|
|
|
* @return Composer |
74
|
|
|
*/ |
75
|
54 |
|
protected function addComposerSettings(array &$content) |
76
|
|
|
{ |
77
|
54 |
|
$content = array_merge_recursive($content, $this->getGenerator()->getOptionComposerSettings()); |
78
|
54 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
54 |
|
protected function getPsr4Autoload() |
84
|
|
|
{ |
85
|
54 |
|
$namespace = new EmptyModel($this->getGenerator(), ''); |
86
|
54 |
|
if ($namespace->getNamespace() !== '') { |
87
|
24 |
|
$namespaceKey = sprintf('%s\\', $namespace->getNamespace()); |
88
|
12 |
|
} else { |
89
|
30 |
|
$namespaceKey = ''; |
90
|
3 |
|
} |
91
|
54 |
|
$src = rtrim($this->generator->getOptionSrcDirname(), DIRECTORY_SEPARATOR); |
92
|
3 |
|
return [ |
93
|
54 |
|
$namespaceKey => sprintf('./%s', empty($src) ? '' : $src . DIRECTORY_SEPARATOR), |
94
|
27 |
|
]; |
95
|
|
|
} |
96
|
|
|
/** |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
54 |
|
protected function getComposerFileContent() |
100
|
|
|
{ |
101
|
54 |
|
$content = []; |
102
|
54 |
|
$composerFilePath = $this->getComposerFilePath(); |
103
|
54 |
|
if (!empty($composerFilePath)) { |
104
|
54 |
|
$content = json_decode(file_get_contents($composerFilePath), true); |
105
|
27 |
|
} |
106
|
54 |
|
return $content; |
107
|
|
|
} |
108
|
|
|
/** |
109
|
|
|
* @param array $content |
110
|
|
|
* @return Composer |
111
|
|
|
*/ |
112
|
54 |
|
protected function setComposerFileContent(array $content) |
113
|
|
|
{ |
114
|
54 |
|
$composerFilePath = $this->getComposerFilePath(); |
115
|
54 |
|
if (!empty($composerFilePath)) { |
116
|
54 |
|
file_put_contents($composerFilePath, self::encodeToJson($content)); |
117
|
27 |
|
} |
118
|
54 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
/** |
121
|
|
|
* @param array $content |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
54 |
|
protected static function encodeToJson($content) |
125
|
|
|
{ |
126
|
54 |
|
if (version_compare(PHP_VERSION, '5.4.0') === -1) { |
127
|
|
|
$json = str_replace('\/', '/', json_encode($content)); |
128
|
|
|
} else { |
129
|
54 |
|
$json = json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
130
|
|
|
} |
131
|
54 |
|
return $json; |
132
|
|
|
} |
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
54 |
|
protected function getComposerFilePath() |
137
|
|
|
{ |
138
|
54 |
|
return realpath(sprintf('%s/composer.json', $this->getGenerator()->getOptionDestination())); |
139
|
|
|
} |
140
|
|
|
/** |
141
|
|
|
* @param bool $runComposerUpdate |
142
|
|
|
* @return Composer |
143
|
|
|
*/ |
144
|
30 |
|
public function setRunComposerUpdate($runComposerUpdate) |
145
|
|
|
{ |
146
|
30 |
|
$this->runComposerUpdate = $runComposerUpdate; |
147
|
30 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
/** |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
60 |
|
public function getRunComposerUpdate() |
153
|
|
|
{ |
154
|
60 |
|
return $this->runComposerUpdate; |
155
|
|
|
} |
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
30 |
|
public function getFileExtension() |
160
|
|
|
{ |
161
|
30 |
|
return self::JSON_FILE_EXTENSION; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|