Completed
Push — master ( 83a00c...7fd29b )
by Emmanuel
04:00
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($opts = ['php' => '7.1', 'db' => 'mysql', 'keep-cts' => false])
35
    {
36
        $this->setupDocker($opts['php'], $opts['db']);
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')
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->startPHP($php, $dbType);
60
    }
61
62
    private function startDb($type)
63
    {
64
        $dbCt = $this->taskDockerRun($type)->detached()->name('robo_db')->option('--rm');
65
        if ($type === 'mysql') {
66
            $dbCt = $dbCt->env('MYSQL_ROOT_PASSWORD', 'root')
67
                         ->env('MYSQL_DATABASE', 'test_db');
68
        } elseif ($type === 'postgres') {
69
            $dbCt = $dbCt->env('POSTGRES_PASSWORD', 'root')
70
                         ->env('POSTGRES_USER', 'root')
71
                         ->env('POSTGRES_DB', 'test_db');
72
        }
73
74
        $dbCt->run();
75
76
        $this->say("Waiting 10 seconds $type to start");
77
        sleep(10);
78
    }
79
80
81
    private function startPHP(string $version, string $dbType)
82
    {
83
        $driver = $dbType === 'postgres' ? 'pdo_pgsql' : 'pdo_mysql';
84
85
        $this->taskDockerRun('edyan/php:' . $version)
86
            ->detached()->name('robo_php')->option('--rm')
87
            ->env('FPM_UID', getmyuid())->env('FPM_GID', getmygid())
88
            ->env('DB_HOST', 'mysql')->env('DB_PASSWORD', 'root')->env('DB_DRIVER', $driver)
89
            ->volume(__DIR__, '/var/www/html')
90
            ->link('robo_db', 'mysql')
91
            ->run();
92
    }
93
94
95
    private function destroyDocker()
96
    {
97
        $cts = ['robo_db', 'robo_php'];
98
        foreach ($cts as $ct) {
99
            $this->stopContainer($ct);
100
        }
101
    }
102
103
104
    private function stopContainer(string $ct)
105
    {
106
        $process = new \Symfony\Component\Process\Process("docker ps | grep $ct | wc -l");
107
        $process->run();
108
109
        if ((int)$process->getOutput() === 0) {
110
            return;
111
        }
112
113
        $this->say('Destroying container ' . $ct);
114
        $this->taskDockerStop($ct)->run();
115
    }
116
117
    /**
118
     * Build phar executable.
119
     */
120
    public function pharBuild()
121
    {
122
        // Create a collection builder to hold the temporary
123
        // directory until the pack phar task runs.
124
        $collection = $this->collectionBuilder();
125
        $workDir = $collection->tmpDir();
126
        $buildDir = "$workDir/neuralyzer";
127
128
        $prepTasks = $this->collectionBuilder();
129
        $preparationResult = $prepTasks
130
            ->taskFilesystemStack()
131
                ->mkdir($workDir)
132
133
            ->taskCopyDir([
134
                __DIR__ . '/src' => $buildDir . '/src'
135
            ])
136
137
            ->taskFilesystemStack()
138
                ->copy(__DIR__ . '/bin/neuralyzer', $buildDir . '/bin/neuralyzer')
139
                ->copy(__DIR__ . '/composer.json', $buildDir . '/composer.json')
140
                ->copy(__DIR__ . '/composer.lock', $buildDir . '/composer.lock')
141
                ->copy(__DIR__ . '/LICENSE', $buildDir . '/LICENSE')
142
                ->copy(__DIR__ . '/README.md', $buildDir . '/README.md')
143
144
            ->taskComposerInstall()
145
                ->dir($buildDir)
146
                ->noDev()
147
                ->noScripts()
148
                ->printed(true)
149
                ->optimizeAutoloader()
150
                ->run();
151
152
        // Exit if the preparation step failed
153
        if (!$preparationResult->wasSuccessful()) {
154
            return $preparationResult;
155
        }
156
157
        // Decide which files we're going to pack
158
        $files = \Symfony\Component\Finder\Finder::create()->ignoreVCS(true)
159
            ->files()
160
            ->name('*.php')
161
            ->name('*.exe') // for 1symfony/console/Resources/bin/hiddeninput.exe
162
            ->path('src')
163
            ->path('vendor')
164
            ->notPath('docs')
165
            ->notPath('/vendor\/.*\/[Tt]est/')
166
            ->in(is_dir($buildDir) ? $buildDir : __DIR__);
167
168
        // Build the phar
169
        return $collection
170
            ->taskPackPhar('neuralyzer.phar')
171
                ->compress()
172
                ->addFile('bin/neuralyzer', 'bin/neuralyzer')
173
                ->addFiles($files)
174
                ->executable('bin/neuralyzer')
175
            ->taskFilesystemStack()
176
                ->chmod(__DIR__ . '/neuralyzer.phar', 0755)
177
            ->run();
178
    }
179
}
180