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