Passed
Pull Request — master (#5)
by Joao
04:22
created

PostCreateScript::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 19
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Builder;
4
5
use Composer\Script\Event;
6
7
class PostCreateScript
8
{
9
    public function execute($workdir, $namespace, $composerName, $phpVersion, $mysqlConnection, $timezone)
10
    {
11
        $directory = new \RecursiveDirectoryIterator($workdir);
12
        $filter = new \RecursiveCallbackFilterIterator($directory, function ($current/*, $key, $iterator*/) {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type string expected by parameter $callback of RecursiveCallbackFilterIterator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

12
        $filter = new \RecursiveCallbackFilterIterator($directory, /** @scrutinizer ignore-type */ function ($current/*, $key, $iterator*/) {
Loading history...
13
            // Skip hidden files and directories.
14
            if ($current->getFilename()[0] === '.') {
15
                return false;
16
            }
17
            if ($current->isDir()) {
18
                // Only recurse into intended subdirectories.
19
                return $current->getFilename() !== 'fw';
20
            }
21
            // else {
22
            //     // Only consume files of interest.
23
            //     return strpos($current->getFilename(), 'wanted_filename') === 0;
24
            // }
25
            return true;
26
        });
27
28
29
        //Replace composer name:
30
        $contents = file_get_contents($workdir . '/composer.json');
31
        file_put_contents(
32
            $workdir . '/composer.json',
33
            str_replace('byjg/resttemplate', $composerName, $contents)
34
        );
35
36
        // Replace Docker PHP Version
37
        $files = [ 'docker/Dockerfile', 'docker/Dockerfile-dev' ];
38
        foreach ($files as $file) {
39
            $contents = file_get_contents("$workdir/$file");
40
            $contents = str_replace('ENV TZ=America/Sao_Paulo', "ENV TZ=${timezone}", $contents);
41
            file_put_contents(
42
                "$workdir/$file",
43
                str_replace('FROM byjg/php:7.2-fpm-nginx', "FROM byjg/php:${phpVersion}-fpm-nginx", $contents)
44
            );
45
        }
46
47
        // Replace MySQL Connection
48
        $files = [ 'config/config-dev.php', 'config/config-homolog.php' , 'config/config-live.php', 'config/config-test.php'];
49
        foreach ($files as $file) {
50
            $contents = file_get_contents("$workdir/$file");
51
            file_put_contents(
52
                "$workdir/$file",
53
                str_replace('mysql://root:password@mysql-container/database', "$mysqlConnection", $contents)
54
            );
55
        }
56
57
        // Replace Namespace
58
        $objects = new \RecursiveIteratorIterator($filter);
59
        foreach ($objects as $name => $object) {
60
            $contents = file_get_contents($name);
61
            if (strpos($contents, 'RestTemplate') !== false) {
62
                echo "$name\n";
63
64
                // Replace inside Quotes
65
                $contents = preg_replace(
66
                    "/([\'\"])RestTemplate(.*?[\'\"])/",
67
                    '$1' . str_replace('\\', '\\\\\\\\', $namespace) . '$2',
68
                    $contents
69
                );
70
71
                // Replace reserved name
72
                $contents = str_replace('RestTemplate', $namespace, $contents);
73
74
                // Replace reserved name
75
                $contents = str_replace(
76
                    'resttemplate',
77
                    str_replace('/', '', $composerName),
78
                    $contents
79
                );
80
81
                // Save it
82
                file_put_contents(
83
                    $name,
84
                    $contents
85
                );
86
            }
87
        }
88
    }
89
90
    public static function run(Event $event)
91
    {
92
        $workdir = realpath(__DIR__ . '/../..');
93
        $stdIo = $event->getIO();
94
95
        $stdIo->write("========================================================");
96
        $stdIo->write("  Setup RestTemplate");
97
        $stdIo->write("========================================================");
98
        $stdIo->write("");
99
        $stdIo->write("Project Directory: " . $workdir);
100
        $phpVersion = $stdIo->ask('PHP Version [7.2]: ', '7.2');
101
        $namespace = $stdIo->ask('Project namespace [MyRest]: ', 'MyRest');
102
        $composerName = $stdIo->ask('Composer name [me/myrest]: ', 'me/myrest');
103
        $mysqlConnection = $stdIo->ask('MySQL connection DEV [mysql://root:password@mysql-container/database]: ', 'mysql://root:password@mysql-container/database');
104
        $timezone = $stdIo->ask('Timezone [America/Sao_Paulo]: ', 'America/Sao_Paulo');
105
        $stdIo->ask('Press <ENTER> to continue');
106
107
        $script = new PostCreateScript();
108
        $script->execute($workdir, $namespace, $composerName, $phpVersion, $mysqlConnection, $timezone);
109
    }
110
}
111