Completed
Push — master ( d2fe44...54d794 )
by Mikaël
659:50 queued 657:21
created

Composer::getPsr4Autoload()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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