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