Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

Configuration/CommandlineOptionsMiddleware.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2016 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Application\Configuration;
14
15
use phpDocumentor\DomainModel\Dsn;
16
use phpDocumentor\DomainModel\Path;
17
18
final class CommandlineOptionsMiddleware
19
{
20
    /** @var string[] */
21
    private $options = [];
22
23 7
    public function __construct(array $options = [])
24
    {
25 7
        $this->options = $options;
26 7
    }
27
28 6
    public function provideOptions(array $options)
29
    {
30 6
        $this->options = $options;
31 6
    }
32
33 7
    public function __invoke(array $configuration)
34
    {
35 7
        $configuration = $this->overwriteDestinationFolder($configuration);
36 7
        $configuration = $this->disableCache($configuration);
37 7
        $configuration = $this->overwriteCacheFolder($configuration);
38 7
        $configuration = $this->overwriteTitle($configuration);
39 7
        $configuration = $this->overwriteTemplates($configuration);
40
41 7
        if (!isset($configuration['phpdocumentor']['versions'])) {
42 5
            return $configuration;
43
        }
44
45 2
        foreach ($configuration['phpdocumentor']['versions'] as &$version) {
0 ignored issues
show
The expression $configuration['phpdocumentor']['versions'] of type string is not traversable.
Loading history...
46 2
            $version = $this->setFilesInPath($version);
47 2
            $version = $this->setDirectoriesInPath($version);
48 2
            $version = $this->registerExtensions($version);
49 2
            $version = $this->overwriteIgnoredPaths($version);
50 2
            $version = $this->overwriteMarkers($version);
51 2
            $version = $this->overwriteVisibility($version);
52 2
            $version = $this->overwriteDefaultPackageName($version);
53
        }
54
55 2
        return $configuration;
56
    }
57
58
    public function createDefaultApiSettings(): array
59
    {
60
        return [[
61
            'format' => 'php',
62
            'source' => [
63
                'dsn' => 'file://.',
64
                'paths' => [new Path('.')],
65
            ],
66
            'ignore' => [
67
                'hidden' => true,
68
                'symlinks' => true,
69
                'paths' => [],
70
            ],
71
            'extensions' => ['php', 'php3', 'phtml'],
72
            'visibility' => 'public',
73
            'default-package-name' => 'phpDocumentor',
74
            'markers' => ['TODO', 'FIXME'],
75
        ]];
76
    }
77
78 2
    private function setFilesInPath(array $version): array
79
    {
80 2
        if (! isset($this->options['filename']) || ! $this->options['filename']) {
81 2
            return $version;
82
        }
83
84
        if (! isset($version['api'])) {
85
            $version['api'] = $this->createDefaultApiSettings();
86
        }
87
88
        $version['api'][0]['source']['paths'] = array_map(
89
            function ($path) {
90
                return new Path($path);
91
            },
92
            $this->options['filename']
93
        );
94
95
        return $version;
96
    }
97
98 2
    private function setDirectoriesInPath(array $version): array
99
    {
100 2
        if (! isset($this->options['directory']) || ! $this->options['directory']) {
101 1
            return $version;
102
        }
103
104 1
        if (! isset($version['api'])) {
105
            $version['api'] = $this->createDefaultApiSettings();
106
        }
107
108 1
        $version['api'][0]['source']['paths'] = array_map(
109 1
            function ($path) {
110 1
                return new Path($path);
111 1
            },
112 1
            $this->options['directory']
113
        );
114
115 1
        return $version;
116
    }
117
118 2
    private function overwriteIgnoredPaths(array $version): array
119
    {
120 2
        if (! isset($this->options['ignore']) || ! $this->options['ignore']) {
121 2
            return $version;
122
        }
123
124
        if (! isset($version['api'])) {
125
            $version['api'] = $this->createDefaultApiSettings();
126
        }
127
128
        $version['api'][0]['ignore']['paths'] = array_map(
129
            function ($path) {
130
                return new Path($path);
131
            },
132
            $this->options['ignore']
133
        );
134
135
        return $version;
136
    }
137
138 2
    private function registerExtensions(array $version): array
139
    {
140 2
        if (!isset($this->options['extensions']) || ! $this->options['extensions']) {
141 2
            return $version;
142
        }
143
144
        if (! isset($version['api'])) {
145
            $version['api'] = $this->createDefaultApiSettings();
146
        }
147
148
        $version['api'][0]['extensions'] = $this->options['extensions'];
149
150
        return $version;
151
    }
152
153
    /**
154
     * @return array
155
     */
156 7
    private function overwriteDestinationFolder(array $configuration)
157
    {
158 7
        if (isset($this->options['target']) && $this->options['target']) {
159 1
            $configuration['phpdocumentor']['paths']['output'] = new Dsn($this->options['target']);
160
        }
161
162 7
        return $configuration;
163
    }
164
165 7
    private function overwriteCacheFolder(array $configuration): array
166
    {
167 7
        if (isset($this->options['cache-folder']) && $this->options['cache-folder']) {
168 1
            $configuration['phpdocumentor']['paths']['cache'] = new Path($this->options['cache-folder']);
169
        }
170
171 7
        return $configuration;
172
    }
173
174 2
    private function overwriteMarkers(array $version): array
175
    {
176 2
        if (! isset($this->options['markers']) || ! $this->options['markers']) {
177 2
            return $version;
178
        }
179
180
        if (! isset($version['api'])) {
181
            $version['api'] = $this->createDefaultApiSettings();
182
        }
183
184
        $version['api'][0]['markers'] = $this->options['markers'];
185
186
        return $version;
187
    }
188
189 2
    private function overwriteVisibility(array $version): array
190
    {
191 2
        if (! isset($this->options['visibility']) || ! $this->options['visibility']) {
192 2
            return $version;
193
        }
194
195
        if (! isset($version['api'])) {
196
            $version['api'] = $this->createDefaultApiSettings();
197
        }
198
199
        $version['api'][0]['visibility'] = $this->options['visibility'];
200
201
        return $version;
202
    }
203
204 2
    private function overwriteDefaultPackageName(array $version): array
205
    {
206 2
        if (! isset($this->options['defaultpackagename']) || ! $this->options['defaultpackagename']) {
207 2
            return $version;
208
        }
209
210
        if (! isset($version['api'])) {
211
            $version['api'] = $this->createDefaultApiSettings();
212
        }
213
214
        $version['api'][0]['default-package-name'] = $this->options['defaultpackagename'];
215
216
        return $version;
217
    }
218
219 7
    private function overwriteTitle(array $configuration): array
220
    {
221 7
        if (isset($this->options['title']) && $this->options['title']) {
222 1
            $configuration['phpdocumentor']['title'] = $this->options['title'];
223
        }
224
225 7
        return $configuration;
226
    }
227
228
    /**
229
     * Changes the given configuration array to feature the templates from the options.
230
     *
231
     * @param string[] $configuration
232
     *
233
     * @return string[]
234
     */
235 7
    private function overwriteTemplates(array $configuration)
236
    {
237 7
        if (isset($this->options['template']) && $this->options['template']) {
238 1
            $configuration['phpdocumentor']['templates'] = (array) $this->options['template'];
239
        }
240
241 7
        return $configuration;
242
    }
243
244
    /**
245
     * Changes the given configuration array so that the cache handling is disabled.
246
     *
247
     * @param string[] $configuration
248
     *
249
     * @return string[]
250
     */
251 7
    private function disableCache($configuration): array
252
    {
253 7
        if (isset($this->options['force']) && $this->options['force']) {
254 2
            $configuration['phpdocumentor']['use-cache'] = false;
255
        }
256
257 7
        return $configuration;
258
    }
259
}
260