Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.51% covered (success)
98.51%
66 / 67
91.67% covered (success)
91.67%
11 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Composer
98.51% covered (success)
98.51%
66 / 67
91.67% covered (success)
91.67%
11 / 12
20
0.00% covered (danger)
0.00%
0 / 1
 setRunComposerUpdate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getRunComposerUpdate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileExtension
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 writeFile
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
2
 completeComposerJson
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 addAutoloadToComposerJson
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 addComposerSettings
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPsr4Autoload
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
3.01
 getComposerFileContent
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 setComposerFileContent
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 encodeToJson
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getComposerFilePath
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\File;
6
7use Composer\Console\Application;
8use Symfony\Component\Console\Input\ArrayInput;
9use WsdlToPhp\PackageGenerator\Model\EmptyModel;
10
11final class Composer extends AbstractFile
12{
13    public const JSON_FILE_EXTENSION = 'json';
14
15    /**
16     * Tests purpose: do not run composer update command.
17     */
18    protected bool $runComposerUpdate = true;
19
20    public function setRunComposerUpdate(bool $runComposerUpdate): Composer
21    {
22        $this->runComposerUpdate = $runComposerUpdate;
23
24        return $this;
25    }
26
27    public function getRunComposerUpdate(): bool
28    {
29        return $this->runComposerUpdate;
30    }
31
32    public function getFileExtension(): string
33    {
34        return self::JSON_FILE_EXTENSION;
35    }
36
37    protected function writeFile(): void
38    {
39        $composer = new Application();
40        $composer->setAutoExit(false);
41        $composer->run(new ArrayInput([
42            'command' => 'init',
43            '--verbose' => true,
44            '--no-interaction' => true,
45            '--name' => $this->getGenerator()->getOptionComposerName(),
46            '--description' => sprintf('Package generated from %s using wsdltophp/packagegenerator', $this->getGenerator()->getWsdl()->getName()),
47            '--require' => [
48                'php:>=7.4',
49                'ext-dom:*',
50                'ext-mbstring:*',
51                'ext-soap:*',
52                'wsdltophp/packagebase:~5.0',
53            ],
54            '--working-dir' => $this->getGenerator()->getOptionDestination(),
55        ]));
56
57        $this->completeComposerJson();
58
59        if ($this->getRunComposerUpdate()) {
60            $composer->run(new ArrayInput([
61                'command' => 'update',
62                '--verbose' => true,
63                '--optimize-autoloader' => true,
64                '--no-dev' => true,
65                '--working-dir' => $this->getGenerator()->getOptionDestination(),
66            ]));
67        }
68    }
69
70    protected function completeComposerJson(): Composer
71    {
72        $content = $this->getComposerFileContent();
73        if (is_array($content) && !empty($content)) {
74            $this
75                ->addAutoloadToComposerJson($content)
76                ->addComposerSettings($content)
77            ;
78        }
79
80        return $this->setComposerFileContent($content);
81    }
82
83    protected function addAutoloadToComposerJson(array &$content): Composer
84    {
85        $content['autoload'] = [
86            'psr-4' => $this->getPsr4Autoload(),
87        ];
88
89        return $this;
90    }
91
92    protected function addComposerSettings(array &$content): Composer
93    {
94        $content = array_replace_recursive($content, $this->getGenerator()->getOptionComposerSettings());
95
96        return $this;
97    }
98
99    protected function getPsr4Autoload(): array
100    {
101        $namespace = new EmptyModel($this->getGenerator(), '');
102        if (!empty($namespace->getNamespace())) {
103            $namespaceKey = sprintf('%s\\', $namespace->getNamespace());
104        } else {
105            $namespaceKey = '';
106        }
107        $src = rtrim($this->generator->getOptionSrcDirname(), DIRECTORY_SEPARATOR);
108
109        return [
110            $namespaceKey => sprintf(
111                './%s%s',
112                empty($src) ? '' : $src.DIRECTORY_SEPARATOR,
113                str_replace('\\', DIRECTORY_SEPARATOR, $namespace->getNamespace())
114            ),
115        ];
116    }
117
118    protected function getComposerFileContent(): array
119    {
120        $content = [];
121        $composerFilePath = $this->getComposerFilePath();
122        if (!empty($composerFilePath)) {
123            $content = json_decode(file_get_contents($composerFilePath), true);
124        }
125
126        return $content;
127    }
128
129    protected function setComposerFileContent(array $content): Composer
130    {
131        $composerFilePath = $this->getComposerFilePath();
132        if (!empty($composerFilePath)) {
133            file_put_contents($composerFilePath, self::encodeToJson($content));
134        }
135
136        return $this;
137    }
138
139    protected static function encodeToJson(array $content): string
140    {
141        return json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
142    }
143
144    protected function getComposerFilePath(): string
145    {
146        $path = realpath(sprintf('%s/composer.json', $this->getGenerator()->getOptionDestination()));
147
148        return false === $path ? '' : $path;
149    }
150}