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

ScriptHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 28
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A installSwaggerUi() 0 21 2
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