Completed
Push — develop ( 3bf98d...ad187c )
by Jaap
06:26
created

CommandlineOptionsMiddleware::__invoke()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 1
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
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\Factory;
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
    public function __construct(array $options = [])
24
    {
25
        $this->options = $options;
26
    }
27
28
    public function provideOptions(array $options)
29
    {
30
        $this->options = $options;
31
    }
32
33
    public function __invoke(array $configuration)
34
    {
35
        $configuration = $this->overwriteDestinationFolder($configuration);
36
        $configuration = $this->disableCache($configuration);
37
        $configuration = $this->overwriteCacheFolder($configuration);
38
        $configuration = $this->overwriteTitle($configuration);
39
        $configuration = $this->overwriteTemplates($configuration);
40
41
        if (!isset($configuration['phpdocumentor']['versions'])) {
42
            return $configuration;
43
        }
44
45
        foreach ($configuration['phpdocumentor']['versions'] as &$version) {
0 ignored issues
show
Bug introduced by
The expression $configuration['phpdocumentor']['versions'] of type string is not traversable.
Loading history...
46
            $version = $this->setFilesInPath($version);
47
            $version = $this->setDirectoriesInPath($version);
48
            $version = $this->registerExtensions($version);
49
            $version = $this->overwriteIgnoredPaths($version);
50
            $version = $this->overwriteMarkers($version);
51
            $version = $this->overwriteVisibility($version);
52
            $version = $this->overwriteDefaultPackageName($version);
53
        }
54
55
        return $configuration;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function createDefaultApiSettings()
62
    {
63
        return [
64
            'format'               => 'php',
65
            'source'               => [
66
                'dsn'   => 'file://.',
67
                'paths' => [new Path('.')]
68
            ],
69
            'ignore'               => [
70
                'hidden'   => true,
71
                'symlinks' => true,
72
                'paths'    => [],
73
            ],
74
            'extensions'           => ['php', 'php3', 'phtml'],
75
            'visibility'           => 'public',
76
            'default-package-name' => 'phpDocumentor',
77
            'markers'              => ['TODO', 'FIXME']
78
        ];
79
    }
80
81
    /**
82
     * @param $version
83
     *
84
     * @return mixed
85
     */
86
    private function setFilesInPath($version)
87
    {
88
        if (! isset($this->options['filename']) || ! $this->options['filename']) {
89
            return $version;
90
        }
91
92
        if (! isset($version['api'])) {
93
            $version['api'] = $this->createDefaultApiSettings();
94
        }
95
96
        $version['api']['source']['paths'] = array_map(
97
            function ($path) {
98
                return new Path($path);
99
            },
100
            $this->options['filename']
101
        );
102
103
        return $version;
104
    }
105
106
    /**
107
     * @param $version
108
     *
109
     * @return mixed
110
     */
111
    private function setDirectoriesInPath($version)
112
    {
113
        if (! isset($this->options['directory']) || ! $this->options['directory']) {
114
            return $version;
115
        }
116
117
        if (! isset($version['api'])) {
118
            $version['api'] = $this->createDefaultApiSettings();
119
        }
120
121
        $version['api']['source']['paths'] = array_map(
122
            function ($path) {
123
                return new Path($path);
124
            },
125
            $this->options['directory']
126
        );
127
128
        return $version;
129
    }
130
131
    /**
132
     * @param $version
133
     *
134
     * @return mixed
135
     */
136
    private function overwriteIgnoredPaths($version)
137
    {
138
        if (! isset($this->options['ignore']) || ! $this->options['ignore']) {
139
            return $version;
140
        }
141
142
        if (! isset($version['api'])) {
143
            $version['api'] = $this->createDefaultApiSettings();
144
        }
145
146
        $version['api']['ignore']['paths'] = array_map(
147
            function ($path) {
148
                return new Path($path);
149
            },
150
            $this->options['ignore']
151
        );
152
153
        return $version;
154
    }
155
156
    /**
157
     * @param $version
158
     *
159
     * @return mixed
160
     */
161
    private function registerExtensions($version)
162
    {
163
        if (!isset($this->options['extensions']) || ! $this->options['extensions']) {
164
            return $version;
165
        }
166
167
        if (! isset($version['api'])) {
168
            $version['api'] = $this->createDefaultApiSettings();
169
        }
170
171
        $version['api']['extensions'] = $this->options['extensions'];
172
173
        return $version;
174
    }
175
176
    /**
177
     * @param array $configuration
178
     *
179
     * @return array
180
     */
181
    private function overwriteDestinationFolder(array $configuration)
182
    {
183
        if (isset($this->options['target']) && $this->options['target']) {
184
            $configuration['phpdocumentor']['paths']['output'] = new Dsn($this->options['target']);
185
        }
186
187
        return $configuration;
188
    }
189
190
    /**
191
     * @param array $configuration
192
     *
193
     * @return array
194
     */
195
    private function overwriteCacheFolder(array $configuration)
196
    {
197
        if (isset($this->options['cache-folder']) && $this->options['cache-folder']) {
198
            $configuration['phpdocumentor']['paths']['cache'] = new Path($this->options['cache-folder']);
199
        }
200
201
        return $configuration;
202
    }
203
204
    /**
205
     * @param $version
206
     *
207
     * @return mixed
208
     */
209
    private function overwriteMarkers($version)
210
    {
211
        if (! isset($this->options['markers']) || ! $this->options['markers']) {
212
            return $version;
213
        }
214
215
        if (! isset($version['api'])) {
216
            $version['api'] = $this->createDefaultApiSettings();
217
        }
218
219
        $version['api']['markers'] = $this->options['markers'];
220
221
        return $version;
222
    }
223
224
    /**
225
     * @param $version
226
     *
227
     * @return mixed
228
     */
229
    private function overwriteVisibility($version)
230
    {
231
        if (! isset($this->options['visibility']) || ! $this->options['visibility']) {
232
            return $version;
233
        }
234
235
        if (! isset($version['api'])) {
236
            $version['api'] = $this->createDefaultApiSettings();
237
        }
238
239
        $version['api']['visibility'] = $this->options['visibility'];
240
241
        return $version;
242
    }
243
244
    /**
245
     * @param $version
246
     *
247
     * @return mixed
248
     */
249
    private function overwriteDefaultPackageName($version)
250
    {
251
        if (! isset($this->options['defaultpackagename']) || ! $this->options['defaultpackagename']) {
252
            return $version;
253
        }
254
255
        if (! isset($version['api'])) {
256
            $version['api'] = $this->createDefaultApiSettings();
257
        }
258
259
        $version['api']['default-package-name'] = $this->options['defaultpackagename'];
260
261
        return $version;
262
    }
263
264
    /**
265
     * @param array $configuration
266
     *
267
     * @return array
268
     */
269
    private function overwriteTitle(array $configuration)
270
    {
271
        if (isset($this->options['title']) && $this->options['title']) {
272
            $configuration['phpdocumentor']['title'] = $this->options['title'];
273
        }
274
275
        return $configuration;
276
    }
277
278
    /**
279
     * Changes the given configuration array to feature the templates from the options.
280
     *
281
     * @param string[] $configuration
282
     *
283
     * @return string[]
284
     */
285
    private function overwriteTemplates(array $configuration)
286
    {
287
        if (isset($this->options['template']) && $this->options['template']) {
288
            $configuration['phpdocumentor']['templates'] = (array)$this->options['template'];
289
        }
290
291
        return $configuration;
292
    }
293
294
    /**
295
     * Changes the given configuration array so that the cache handling is disabled.
296
     *
297
     * @param string[] $configuration
298
     *
299
     * @return string[]
300
     */
301
    private function disableCache($configuration)
302
    {
303
        if (isset($this->options['force']) && $this->options['force']) {
304
            $configuration['phpdocumentor']['use-cache'] = false;
305
        }
306
307
        return $configuration;
308
    }
309
}
310