Completed
Push — master ( 4f49d4...1096e4 )
by Emmanuel
06:21 queued 43s
created

RoboFile.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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(
35
        $opts = ['php' => '7.1', 'db' => 'mysql', 'keep-cts' => false, 'wait' => 5]
36
    ) {
37
        $this->setupDocker($opts['php'], $opts['db'], $opts['wait']);
38
39
        $this->taskDockerExec('robo_php')
40
            ->interactive()
41
            ->option('--user', 'www-data')
42
            ->exec($this->taskExec('/bin/bash -c "cd /var/www/html ; vendor/bin/phpunit"'))
43
            ->run();
44
45
        if ($opts['keep-cts'] === false) {
46
            $this->destroyDocker();
47
        }
48
    }
49
50
51
    private function setupDocker(string $php = '7.1', string $dbType = 'mysql', int $wait)
52
    {
53
        $this->destroyDocker();
54
55
        if (!in_array($dbType, ['mysql', 'postgres', 'sqlsrv'])) {
56
            throw new \InvalidArgumentException('Database can be only mysql, postgres or sqlsrv');
57
        }
58
59
        $this->startDb($dbType);
60
        $this->say("Waiting $wait seconds $dbType to start");
61
        sleep($wait);
62
        // Now create a DB For SQL Server
63
        if ($dbType === 'sqlsrv') {
64
            $createSqlQuery = '/opt/mssql-tools/bin/sqlcmd -U sa -P rootRoot44root -S localhost -Q "CREATE DATABASE test_db"';
65
            $this->taskDockerExec('robo_db')
66
                 ->interactive()
67
                 ->exec($this->taskExec($createSqlQuery))
68
                 ->run();
69
        }
70
71
        $this->startPHP($php, $dbType);
72
    }
73
74
    private function startDb($type)
75
    {
76
        $image = $type;
77
        if ($type === 'sqlsrv') {
78
            $image = 'microsoft/mssql-server-linux:2017-latest';
79
        }
80
81
        $dbCt = $this->taskDockerRun($image)->detached()->name('robo_db')->option('--rm');
82
        if ($type === 'mysql') {
83
            $dbCt = $dbCt->env('MYSQL_ROOT_PASSWORD', 'rootRoot44root')->env('MYSQL_DATABASE', 'test_db');
84
        } elseif ($type === 'postgres') {
85
            $dbCt = $dbCt->env('POSTGRES_PASSWORD', 'rootRoot44root')->env('POSTGRES_DB', 'test_db');
86
        } elseif ($type === 'sqlsrv') {
87
            $dbCt = $dbCt->env('ACCEPT_EULA', 'Y')->env('SA_PASSWORD', 'rootRoot44root');
88
        }
89
90
        $dbCt->run();
91
    }
92
93
94
    private function startPHP(string $version, string $dbType)
95
    {
96
        $dbUser = 'root';
97
        if ($dbType === 'postgres') {
98
            $dbUser = 'postgres';
99
        } elseif ($dbType === 'sqlsrv') {
100
            $dbUser = 'sa';
101
        }
102
103
        $this->taskDockerRun('edyan/php:' . $version . '-sqlsrv')
104
            ->detached()->name('robo_php')->option('--rm')
105
            ->env('FPM_UID', getmyuid())->env('FPM_GID', getmygid())
106
            ->env('DB_HOST', 'robo_db')->env('DB_DRIVER', $dbType)
107
            ->env('DB_PASSWORD', 'rootRoot44root')->env('DB_USER', $dbUser)
108
            ->volume(__DIR__, '/var/www/html')
109
            ->link('robo_db', 'robo_db')
110
            ->run();
111
    }
112
    
113
114
    private function destroyDocker()
115
    {
116
        $cts = ['robo_db', 'robo_php'];
117
        foreach ($cts as $ct) {
118
            $this->stopContainer($ct);
119
        }
120
    }
121
122
123
    private function stopContainer(string $ct)
124
    {
125
        $process = new \Symfony\Component\Process\Process("docker ps | grep $ct | wc -l");
126
        $process->run();
127
128
        if ((int)$process->getOutput() === 0) {
129
            return;
130
        }
131
132
        $this->say('Destroying container ' . $ct);
133
        $this->taskDockerStop($ct)->run();
134
    }
135
136
    /**
137
     * Build phar executable.
138
     */
139
    public function pharBuild()
140
    {
141
        // Create a collection builder to hold the temporary
142
        // directory until the pack phar task runs.
143
        $collection = $this->collectionBuilder();
144
        $workDir = $collection->tmpDir();
145
        $buildDir = "$workDir/neuralyzer";
146
147
        $prepTasks = $this->collectionBuilder();
148
        $preparationResult = $prepTasks
149
            ->taskFilesystemStack()
150
                ->mkdir($workDir)
151
152
            ->taskCopyDir([
153
                __DIR__ . '/src' => $buildDir . '/src'
154
            ])
155
156
            ->taskFilesystemStack()
157
                ->copy(__DIR__ . '/bin/neuralyzer', $buildDir . '/bin/neuralyzer')
158
                ->copy(__DIR__ . '/composer.json', $buildDir . '/composer.json')
159
                ->copy(__DIR__ . '/composer.lock', $buildDir . '/composer.lock')
160
                ->copy(__DIR__ . '/LICENSE', $buildDir . '/LICENSE')
161
                ->copy(__DIR__ . '/README.md', $buildDir . '/README.md')
162
163
            ->taskComposerInstall()
164
                ->dir($buildDir)
165
                ->noDev()
166
                ->noScripts()
167
                ->printOutput(true)
168
                ->optimizeAutoloader()
169
                ->run();
170
171
        // Exit if the preparation step failed
172
        if (!$preparationResult->wasSuccessful()) {
173
            return $preparationResult;
174
        }
175
176
        // Decide which files we're going to pack
177
        $files = \Symfony\Component\Finder\Finder::create()->ignoreVCS(true)
178
            ->files()
179
            ->name('*.php')
180
            ->name('*.exe') // for 1symfony/console/Resources/bin/hiddeninput.exe
181
            ->path('src')
182
            ->path('vendor')
183
            ->notPath('docs')
184
            ->notPath('/vendor\/.*\/[Tt]est/')
185
            ->in(is_dir($buildDir) ? $buildDir : __DIR__);
186
187
        // Build the phar
188
        return $collection
189
            ->taskPackPhar('neuralyzer.phar')
190
                ->compress()
191
                ->addFile('bin/neuralyzer', 'bin/neuralyzer')
192
                ->addFiles($files)
193
                ->executable('bin/neuralyzer')
194
            ->taskFilesystemStack()
195
                ->chmod(__DIR__ . '/neuralyzer.phar', 0755)
196
            ->run();
197
    }
198
}
199