Passed
Push — master ( c103e2...560387 )
by Emmanuel
03:39
created

RoboFile::setupDocker()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 3
1
<?php
2
/**
3
 * This is project's console commands configuration for Robo task runner.
4
 *
5
 * @see http://robo.li/
6
 */
7
8
class RoboFile extends \Robo\Tasks
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    public function dockertest()
11
    {
12
        $this->taskDockerStop('robo_test')->run();
13
14
        $this->taskDockerRun('edyan/php:7.1')
15
             ->name('robo_test')
16
             ->detached()
17
             ->option('--rm')
18
             ->run();
19
20
        $this->taskDockerExec('robo_test')
21
             ->interactive()
22
             ->exec($this->taskExec('php -v'))
23
             ->run();
24
25
        $this->taskDockerStop('robo_test')->run();
26
    }
27
28
29
30
    /**
31
     * Run All Unit Test
32
     * @param  array  $opts
33
     */
34
    public function test($opts = ['php' => '7.1', 'db' => 'mysql', 'keep-cts' => false, 'wait' => 5])
35
    {
36
        $this->setupDocker($opts['php'], $opts['db'], $opts['wait']);
37
38
        $this->taskDockerExec('robo_php')
39
            ->interactive()
40
            ->option('--user', 'www-data')
41
            ->exec($this->taskExec('/bin/bash -c "cd /var/www/html ; vendor/bin/phpunit"'))
42
            ->run();
43
44
        if ($opts['keep-cts'] === false) {
45
            $this->destroyDocker();
46
        }
47
    }
48
49
50
    private function setupDocker(string $php = '7.1', string $dbType = 'mysql', int $wait)
51
    {
52
        $this->destroyDocker();
53
54
        if (!in_array($dbType, ['mysql', 'postgres'])) {
55
            throw new \InvalidArgumentException('Database can be only mysql or postgres');
56
        }
57
58
        $this->startDb($dbType);
59
        $this->say("Waiting $wait seconds $dbType to start");
60
        sleep($wait);
61
        $this->startPHP($php, $dbType);
62
    }
63
64
    private function startDb($type)
65
    {
66
        $dbCt = $this->taskDockerRun($type)->detached()->name('robo_db')->option('--rm');
67
        if ($type === 'mysql') {
68
            $dbCt = $dbCt->env('MYSQL_ROOT_PASSWORD', 'root')
69
                         ->env('MYSQL_DATABASE', 'test_db');
70
        } elseif ($type === 'postgres') {
71
            $dbCt = $dbCt->env('POSTGRES_PASSWORD', 'root')
72
                         ->env('POSTGRES_DB', 'test_db');
73
        }
74
75
        $dbCt->run();
76
    }
77
78
79
    private function startPHP(string $version, string $dbType)
80
    {
81
        $driver = 'pdo_mysql';
82
        $dbUser = 'root';
83
        if ($dbType === 'postgres') {
84
            $driver = 'pdo_pgsql';
85
            $dbUser = 'postgres';
86
        }
87
88
        $this->taskDockerRun('edyan/php:' . $version)
89
            ->detached()->name('robo_php')->option('--rm')
90
            ->env('FPM_UID', getmyuid())->env('FPM_GID', getmygid())
91
            ->env('DB_HOST', 'robo_db')->env('DB_DRIVER', $driver)
92
            ->env('DB_PASSWORD', 'root')->env('DB_USER', $dbUser)
93
            ->volume(__DIR__, '/var/www/html')
94
            ->link('robo_db', 'robo_db')
95
            ->run();
96
    }
97
98
99
    private function destroyDocker()
100
    {
101
        $cts = ['robo_db', 'robo_php'];
102
        foreach ($cts as $ct) {
103
            $this->stopContainer($ct);
104
        }
105
    }
106
107
108
    private function stopContainer(string $ct)
109
    {
110
        $process = new \Symfony\Component\Process\Process("docker ps | grep $ct | wc -l");
111
        $process->run();
112
113
        if ((int)$process->getOutput() === 0) {
114
            return;
115
        }
116
117
        $this->say('Destroying container ' . $ct);
118
        $this->taskDockerStop($ct)->run();
119
    }
120
121
    /**
122
     * Build phar executable.
123
     */
124
    public function pharBuild()
125
    {
126
        // Create a collection builder to hold the temporary
127
        // directory until the pack phar task runs.
128
        $collection = $this->collectionBuilder();
129
        $workDir = $collection->tmpDir();
130
        $buildDir = "$workDir/neuralyzer";
131
132
        $prepTasks = $this->collectionBuilder();
133
        $preparationResult = $prepTasks
134
            ->taskFilesystemStack()
135
                ->mkdir($workDir)
136
137
            ->taskCopyDir([
138
                __DIR__ . '/src' => $buildDir . '/src'
139
            ])
140
141
            ->taskFilesystemStack()
142
                ->copy(__DIR__ . '/bin/neuralyzer', $buildDir . '/bin/neuralyzer')
143
                ->copy(__DIR__ . '/composer.json', $buildDir . '/composer.json')
144
                ->copy(__DIR__ . '/composer.lock', $buildDir . '/composer.lock')
145
                ->copy(__DIR__ . '/LICENSE', $buildDir . '/LICENSE')
146
                ->copy(__DIR__ . '/README.md', $buildDir . '/README.md')
147
148
            ->taskComposerInstall()
149
                ->dir($buildDir)
150
                ->noDev()
151
                ->noScripts()
152
                ->printed(true)
153
                ->optimizeAutoloader()
154
                ->run();
155
156
        // Exit if the preparation step failed
157
        if (!$preparationResult->wasSuccessful()) {
158
            return $preparationResult;
159
        }
160
161
        // Decide which files we're going to pack
162
        $files = \Symfony\Component\Finder\Finder::create()->ignoreVCS(true)
163
            ->files()
164
            ->name('*.php')
165
            ->name('*.exe') // for 1symfony/console/Resources/bin/hiddeninput.exe
166
            ->path('src')
167
            ->path('vendor')
168
            ->notPath('docs')
169
            ->notPath('/vendor\/.*\/[Tt]est/')
170
            ->in(is_dir($buildDir) ? $buildDir : __DIR__);
171
172
        // Build the phar
173
        return $collection
174
            ->taskPackPhar('neuralyzer.phar')
175
                ->compress()
176
                ->addFile('bin/neuralyzer', 'bin/neuralyzer')
177
                ->addFiles($files)
178
                ->executable('bin/neuralyzer')
179
            ->taskFilesystemStack()
180
                ->chmod(__DIR__ . '/neuralyzer.phar', 0755)
181
            ->run();
182
    }
183
}
184