Completed
Push — feature/issue-157 ( 8c1a44 )
by Mikaël
20:07
created

Composer::getRunComposerUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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