Completed
Pull Request — master (#768)
by Antoine
02:53
created

ScriptHandler::installSwaggerUi()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ApiPlatform\Core\Composer;
13
14
use Composer\Script\Event;
15
use Symfony\Component\Filesystem\Filesystem;
16
17
class ScriptHandler
18
{
19
    const SWAGGER_UI_DESTINATION = 'swagger-ui';
20
    const SWAGGER_UI_SOURCE = 'swagger-api/swagger-ui/dist';
21
    const SYMFONY_WEB_DIR = 'web';
22
23
    public static function installSwaggerUi(Event $event)
24
    {
25
        $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir').'/'.self::SWAGGER_UI_SOURCE;
26
        $webDir = ($event->getComposer()->getPackage()->getExtra()['symfony-web-dir'] ?? self::SYMFONY_WEB_DIR).'/'.self::SWAGGER_UI_DESTINATION;
27
        $io = $event->getIO();
28
29
        if (!file_exists($vendorDir)) {
30
            $io->writeError('No assets for Swagger-UI, please require "swagger-api/swagger-ui".');
31
32
            return;
33
        }
34
35
        $event->getIO()->write('Installing assets for Swagger-UI...');
36
37
        (new Filesystem())->mirror(
38
            $vendorDir,
39
            $webDir,
40
            null,
41
            ['override' => true, 'delete' => true, 'copyonwindows' => true]
42
        );
43
    }
44
}
45