edyan /
neuralyzer
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
|
|||
| 9 | { |
||
| 10 | /** |
||
| 11 | * A test command just to make sure robo works on that computer |
||
| 12 | */ |
||
| 13 | public function dockertest() |
||
| 14 | { |
||
| 15 | $this->stopOnFail(true); |
||
| 16 | |||
| 17 | $this->taskDockerStop('robo_test')->run(); |
||
| 18 | |||
| 19 | $this->taskDockerRun('edyan/php:7.1') |
||
| 20 | ->name('robo_test') |
||
| 21 | ->detached() |
||
| 22 | ->option('--rm') |
||
| 23 | ->run(); |
||
| 24 | |||
| 25 | $this->taskDockerExec('robo_test') |
||
| 26 | ->interactive() |
||
| 27 | ->exec($this->taskExec('php -v')) |
||
| 28 | ->run(); |
||
| 29 | |||
| 30 | $this->taskDockerStop('robo_test')->run(); |
||
| 31 | } |
||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * Run All Unit Test |
||
| 36 | * @param array $opts |
||
| 37 | */ |
||
| 38 | public function test( |
||
| 39 | $opts = ['php' => '7.1', 'db' => 'mysql', 'keep-cts' => false, 'wait' => 5] |
||
| 40 | ) { |
||
| 41 | $this->stopOnFail(true); |
||
| 42 | |||
| 43 | $this->setupDocker($opts['php'], $opts['db'], $opts['wait']); |
||
| 44 | |||
| 45 | // Run the tests |
||
| 46 | $this->taskDockerExec('robo_php') |
||
| 47 | ->interactive() |
||
| 48 | ->option('--user', 'www-data') |
||
| 49 | ->exec($this->taskExec('/bin/bash -c "cd /var/www/html ; vendor/bin/phpunit"')) |
||
| 50 | ->run(); |
||
| 51 | |||
| 52 | if ($opts['keep-cts'] === false) { |
||
| 53 | $this->destroyDocker(); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Build an executable phar |
||
| 60 | */ |
||
| 61 | public function phar() |
||
| 62 | { |
||
| 63 | if ((int)ini_get('phar.readonly') === 1) { |
||
| 64 | throw new \RuntimeException( |
||
| 65 | 'You must have phar.readonly = 1 or run' . PHP_EOL . |
||
| 66 | 'php -d phar.readonly=0 vendor/bin/robo release' |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | // Create a collection builder to hold the temporary |
||
| 70 | // directory until the pack phar task runs. |
||
| 71 | $collection = $this->collectionBuilder(); |
||
| 72 | $workDir = $collection->tmpDir(); |
||
| 73 | $buildDir = "$workDir/neuralyzer"; |
||
| 74 | |||
| 75 | $prepTasks = $this->collectionBuilder(); |
||
| 76 | $preparationResult = $prepTasks |
||
| 77 | ->taskFilesystemStack() |
||
| 78 | ->mkdir($workDir) |
||
| 79 | ->taskCopyDir([__DIR__ . '/src' => $buildDir . '/src']) |
||
| 80 | ->taskFilesystemStack() |
||
| 81 | ->copy(__DIR__ . '/bin/neuralyzer', $buildDir . '/bin/neuralyzer') |
||
| 82 | ->copy(__DIR__ . '/composer.json', $buildDir . '/composer.json') |
||
| 83 | ->copy(__DIR__ . '/composer.lock', $buildDir . '/composer.lock') |
||
| 84 | ->copy(__DIR__ . '/LICENSE', $buildDir . '/LICENSE') |
||
| 85 | ->copy(__DIR__ . '/README.md', $buildDir . '/README.md') |
||
| 86 | |||
| 87 | ->taskComposerInstall() |
||
| 88 | ->dir($buildDir) |
||
| 89 | ->noDev() |
||
| 90 | ->noScripts() |
||
| 91 | ->printOutput(true) |
||
| 92 | ->optimizeAutoloader() |
||
| 93 | ->run(); |
||
| 94 | |||
| 95 | // Exit if the preparation step failed |
||
| 96 | if (!$preparationResult->wasSuccessful()) { |
||
| 97 | return $preparationResult; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Decide which files we're going to pack |
||
| 101 | $files = \Symfony\Component\Finder\Finder::create()->ignoreVCS(true) |
||
| 102 | ->files() |
||
| 103 | ->name('*.php') |
||
| 104 | ->name('*.exe') // for 1symfony/console/Resources/bin/hiddeninput.exe |
||
| 105 | ->path('src') |
||
| 106 | ->path('vendor') |
||
| 107 | ->notPath('docs') |
||
| 108 | ->notPath('/vendor\/.*\/[Tt]est/') |
||
| 109 | ->in(is_dir($buildDir) ? $buildDir : __DIR__); |
||
| 110 | |||
| 111 | // Build the phar |
||
| 112 | return $collection |
||
| 113 | ->taskPackPhar('neuralyzer.phar') |
||
| 114 | ->compress() |
||
| 115 | ->addFile('bin/neuralyzer', 'bin/neuralyzer') |
||
| 116 | ->addFiles($files) |
||
| 117 | ->executable('bin/neuralyzer') |
||
| 118 | ->taskFilesystemStack() |
||
| 119 | ->chmod(__DIR__ . '/neuralyzer.phar', 0755) |
||
| 120 | ->run(); |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | public function release() |
||
| 125 | { |
||
| 126 | $this->stopOnFail(true); |
||
| 127 | |||
| 128 | $this->gitVerifyEverythingIsCommited(); |
||
| 129 | $this->gitVerifyBranchIsMaster(); |
||
| 130 | $this->gitVerifyBranchIsUpToDate(); |
||
| 131 | |||
| 132 | $version = null; |
||
| 133 | $currentVersion = \Edyan\Neuralyzer\Console\Application::VERSION; |
||
| 134 | while (empty($version)) { |
||
| 135 | $version = $this->ask("Whats the version number ? (current : $currentVersion)"); |
||
| 136 | } |
||
| 137 | $versionDesc = null; |
||
| 138 | while (empty($versionDesc)) { |
||
| 139 | $versionDesc = $this->ask('Describe your release'); |
||
| 140 | } |
||
| 141 | |||
| 142 | $this->say("Preparing version $version"); |
||
| 143 | |||
| 144 | // Patch the right files |
||
| 145 | $this->taskReplaceInFile(__DIR__ . '/src/Console/Application.php') |
||
| 146 | ->from("const VERSION = '$currentVersion';") |
||
| 147 | ->to("const VERSION = '$version';") |
||
| 148 | ->run(); |
||
| 149 | |||
| 150 | $this->phar(); |
||
| 151 | |||
| 152 | // Commit a bump version |
||
| 153 | $this->taskGitStack() |
||
| 154 | ->add(__DIR__ . '/src/Console/Application.php') |
||
| 155 | ->add(__DIR__ . '/neuralyzer.phar') |
||
| 156 | ->commit("Bump version $version") |
||
| 157 | ->push('origin', 'master') |
||
| 158 | ->tag($version) |
||
| 159 | ->push('origin', $version) |
||
| 160 | ->run(); |
||
| 161 | |||
| 162 | // Create a release |
||
| 163 | $this->taskGitHubRelease($version) |
||
| 164 | ->name($versionDesc) |
||
| 165 | ->tag($version) |
||
| 166 | ->description('') |
||
| 167 | ->owner('edyan') |
||
| 168 | ->repo('neuralyzer') |
||
| 169 | ->accessToken(\Robo\Robo::config()->get('settings.github_token')) |
||
| 170 | ->run(); |
||
| 171 | |||
| 172 | $this->say('Release ready, you can push'); |
||
| 173 | } |
||
| 174 | |||
| 175 | |||
| 176 | private function setupDocker( |
||
| 177 | string $php = '7.1', |
||
| 178 | string $dbType = 'mysql', |
||
| 179 | int $wait = 10 |
||
| 180 | ) { |
||
| 181 | $this->destroyDocker(); |
||
| 182 | |||
| 183 | if (!in_array($dbType, ['mysql', 'pgsql', 'sqlsrv'])) { |
||
| 184 | throw new \InvalidArgumentException('Database can be only mysql, pgsql or sqlsrv'); |
||
| 185 | } |
||
| 186 | |||
| 187 | $this->startDb($dbType); |
||
| 188 | $this->say("Waiting $wait seconds $dbType to start"); |
||
| 189 | sleep($wait); |
||
| 190 | // Now create a DB For SQL Server as there is no option in the docker image |
||
| 191 | if ($dbType === 'sqlsrv') { |
||
| 192 | $this->createSQLServerDB(); |
||
| 193 | } |
||
| 194 | |||
| 195 | $this->startPHP($php, $dbType); |
||
| 196 | } |
||
| 197 | |||
| 198 | private function startDb($type) |
||
| 199 | { |
||
| 200 | $image = $type; |
||
| 201 | if ($type === 'sqlsrv') { |
||
| 202 | $image = 'microsoft/mssql-server-linux:2017-latest'; |
||
| 203 | } elseif ($type === 'pgsql') { |
||
| 204 | $image = 'postgres'; |
||
| 205 | } |
||
| 206 | |||
| 207 | $dbCt = $this->taskDockerRun($image)->detached()->name('robo_db')->option('--rm'); |
||
| 208 | if ($type === 'mysql') { |
||
| 209 | $dbCt = $dbCt->env('MYSQL_ROOT_PASSWORD', 'rootRoot44root')->env('MYSQL_DATABASE', 'test_db'); |
||
| 210 | } elseif ($type === 'pgsql') { |
||
| 211 | $dbCt = $dbCt->env('POSTGRES_PASSWORD', 'rootRoot44root')->env('POSTGRES_DB', 'test_db'); |
||
| 212 | } elseif ($type === 'sqlsrv') { |
||
| 213 | $dbCt = $dbCt->env('ACCEPT_EULA', 'Y')->env('SA_PASSWORD', 'rootRoot44root'); |
||
| 214 | } |
||
| 215 | |||
| 216 | $dbCt->run(); |
||
| 217 | } |
||
| 218 | |||
| 219 | |||
| 220 | private function createSQLServerDB() |
||
| 221 | { |
||
| 222 | $createSqlQuery = '/opt/mssql-tools/bin/sqlcmd -U sa -P rootRoot44root '; |
||
| 223 | $createSqlQuery.= '-S localhost -Q "CREATE DATABASE test_db"'; |
||
| 224 | $this->taskDockerExec('robo_db') |
||
| 225 | ->interactive() |
||
| 226 | ->exec($this->taskExec($createSqlQuery)) |
||
| 227 | ->run(); |
||
| 228 | } |
||
| 229 | |||
| 230 | |||
| 231 | private function startPHP(string $version, string $dbType) |
||
| 232 | { |
||
| 233 | if (!in_array($version, ['7.1', '7.2'])) { |
||
| 234 | throw new \InvalidArgumentException('PHP Version must be 7.1 or 7.2'); |
||
| 235 | } |
||
| 236 | |||
| 237 | $dbUser = 'root'; |
||
| 238 | if ($dbType === 'pgsql') { |
||
| 239 | $dbUser = 'postgres'; |
||
| 240 | } elseif ($dbType === 'sqlsrv') { |
||
| 241 | $dbUser = 'sa'; |
||
| 242 | } |
||
| 243 | |||
| 244 | $this->taskDockerRun('edyan/php:' . $version . '-sqlsrv') |
||
| 245 | ->detached()->name('robo_php')->option('--rm') |
||
| 246 | ->env('FPM_UID', getmyuid())->env('FPM_GID', getmygid()) |
||
| 247 | ->env('DB_HOST', 'robo_db')->env('DB_DRIVER', 'pdo_' . $dbType) |
||
| 248 | ->env('DB_PASSWORD', 'rootRoot44root')->env('DB_USER', $dbUser) |
||
| 249 | ->volume(__DIR__, '/var/www/html') |
||
| 250 | ->link('robo_db', 'robo_db') |
||
| 251 | ->run(); |
||
| 252 | } |
||
| 253 | |||
| 254 | |||
| 255 | private function destroyDocker() |
||
| 256 | { |
||
| 257 | $cts = ['robo_db', 'robo_php']; |
||
| 258 | foreach ($cts as $ct) { |
||
| 259 | $this->stopContainer($ct); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | private function stopContainer(string $ct) |
||
| 265 | { |
||
| 266 | $process = new \Symfony\Component\Process\Process("docker ps | grep $ct | wc -l"); |
||
| 267 | $process->run(); |
||
| 268 | |||
| 269 | if ((int)$process->getOutput() === 0) { |
||
| 270 | return; |
||
| 271 | } |
||
| 272 | |||
| 273 | $this->say('Destroying container ' . $ct); |
||
| 274 | $this->taskDockerStop($ct)->run(); |
||
| 275 | } |
||
| 276 | |||
| 277 | private function gitVerifyBranchIsMaster() |
||
| 278 | { |
||
| 279 | $branch = $this->taskGitStack() |
||
| 280 | ->silent(true) |
||
| 281 | ->exec('rev-parse --abbrev-ref HEAD') |
||
| 282 | ->run(); |
||
| 283 | if ($branch->getMessage() !== 'master') { |
||
| 284 | throw new \RuntimeException('You must be on the master branch'); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | private function gitVerifyEverythingIsCommited() |
||
| 289 | { |
||
| 290 | $modifiedFiles = $this->taskGitStack() |
||
| 291 | ->silent(true) |
||
| 292 | ->exec('status -s') |
||
| 293 | ->run(); |
||
| 294 | if (!empty($modifiedFiles->getMessage())) { |
||
| 295 | throw new \RuntimeException('Some files have not been commited yet'); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | private function gitVerifyBranchIsUpToDate() |
||
| 300 | { |
||
| 301 | $modifiedFiles = $this->taskGitStack() |
||
| 302 | ->silent(true) |
||
| 303 | ->exec('fetch --dry-run') |
||
| 304 | ->run(); |
||
| 305 | if (!empty($modifiedFiles->getMessage())) { |
||
| 306 | throw new \RuntimeException('Your local repo is not up to date, run "git pull"'); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | } |
||
| 310 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.