Completed
Push — 1.x ( d5da2a...9204c0 )
by Mikaël
76:43 queued 56:16
created

Composer::addComposerSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 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 18
            '--verbose' => true,
28 18
            '--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 18
                'php:>=5.3.3',
33 18
                'ext-soap:*',
34 18
                'wsdltophp/packagebase:~1.0',
35 18
            ),
36 24
            '--working-dir' => $this->getGenerator()->getOptionDestination(),
37 18
        )));
38 24
        $this->completeComposerJson();
39 24
        if ($this->getRunComposerUpdate() === true) {
40 16
            return $composer->run(new ArrayInput(array(
41 16
                'command' => 'update',
42 12
                '--verbose' => true,
43 12
                '--optimize-autoloader' => true,
44 12
                '--no-dev' => true,
45 16
                '--working-dir' => $this->getGenerator()->getOptionDestination(),
46 12
            )));
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 18
        }
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 6
        } else {
88 16
            $namespaceKey = '';
89
        }
90
        return array(
91 24
            $namespaceKey => sprintf('./%s', AbstractModelFile::SRC_FOLDER),
92 18
        );
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 18
        }
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 18
        }
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 6
            $json = str_replace('\/', '/', json_encode($content));
126 6
        } else {
127 18
            $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