|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Commands; |
| 4 | + |
| 5 | +use Faker\Generator as Faker; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Console\ConfirmableTrait; |
| 8 | +use Illuminate\Database\ConnectionResolverInterface as Resolver; |
| 9 | +use Illuminate\Support\Str; |
| 10 | +use Symfony\Component\Process\Process; |
| 11 | + |
| 12 | +class DevCommand extends Command |
| 13 | +{ |
| 14 | + use ConfirmableTrait; |
| 15 | + |
| 16 | + protected $signature = 'restify:dev {--path= : The path to the root local directory.} |
| 17 | + {--git : Use the latest vcs git repository} |
| 18 | + '; |
| 19 | + |
| 20 | + protected $description = 'Add laravel-restify from a local directory.'; |
| 21 | + |
| 22 | + /** * @var Faker */ |
| 23 | + private $faker; |
| 24 | + |
| 25 | + public function __construct(Resolver $resolver, Faker $faker) |
| 26 | + { |
| 27 | + parent::__construct(); |
| 28 | + $this->resolver = $resolver; |
| 29 | + $this->faker = $faker; |
| 30 | + } |
| 31 | + |
| 32 | + public function handle() |
| 33 | + { |
| 34 | + if (!$this->confirmToProceed()) { |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + $this->addRepositoryToRootComposer(); |
| 39 | + |
| 40 | + $this->info('Added local path to repositories.'); |
| 41 | + |
| 42 | + $this->addPackageToRootComposer(); |
| 43 | + |
| 44 | + $this->info('Package added to the root composer as *.'); |
| 45 | + |
| 46 | + $this->composerUpdate(); |
| 47 | + |
| 48 | + $this->info('Composer updated.'); |
| 49 | + |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + protected function addPackageToRootComposer() |
| 54 | + { |
| 55 | + $composer = json_decode(file_get_contents(base_path('composer.json')), true); |
| 56 | + |
| 57 | + $composer['require']['binaryk/laravel-restify'] = $this->resolveVersion(); |
| 58 | + |
| 59 | + file_put_contents( |
| 60 | + base_path('composer.json'), |
| 61 | + json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + protected function addRepositoryToRootComposer() |
| 66 | + { |
| 67 | + $composer = json_decode(file_get_contents(base_path('composer.json')), true); |
| 68 | + |
| 69 | + if (array_key_exists('repositories', $composer)) { |
| 70 | + $composer['repositories'] = collect($composer['repositories'])->filter(function ($repository) { |
| 71 | + if (!array_key_exists('url', $repository)) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + $pathIsAlreadyInRepositories = Str::contains( |
| 76 | + $repository['url'], |
| 77 | + $this->resolvePath() |
| 78 | + ); |
| 79 | + |
| 80 | + if ($pathIsAlreadyInRepositories) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + return ! Str::contains($repository['url'], 'laravel-restify'); |
| 85 | + })->values()->toArray(); |
| 86 | + } |
| 87 | + |
| 88 | + if ($this->option('git')) { |
| 89 | + $composer['repositories'][] = [ |
| 90 | + 'type' => 'vcs', |
| 91 | + 'url' => $this-> option( 'path') ?: '[email protected]:BinarCode/laravel-restify.git' |
| 92 | + ]; |
| 93 | + } else { |
| 94 | + $composer['repositories'][] = [ |
| 95 | + 'type' => 'path', |
| 96 | + 'url' => $this->option('path') ?: '../../binarcode/laravel-restify' |
| 97 | + ]; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + file_put_contents( |
| 102 | + base_path('composer.json'), |
| 103 | + json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + protected function composerUpdate() |
| 109 | + { |
| 110 | + $this->executeCommand('composer update binaryk/laravel-restify', getcwd()); |
| 111 | + } |
| 112 | + |
| 113 | + protected function executeCommand($command, $path) |
| 114 | + { |
| 115 | + $process = (Process::fromShellCommandline($command, $path))->setTimeout(null); |
| 116 | + |
| 117 | + if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) { |
| 118 | + $process->setTty(true); |
| 119 | + } |
| 120 | + |
| 121 | + $process->run(function ($type, $line) { |
| 122 | + $this->output->write($line); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + public function resolveVersion(): string |
| 127 | + { |
| 128 | + return $this->option('git') |
| 129 | + ? '3.x-dev' |
| 130 | + : '*'; |
| 131 | + } |
| 132 | + |
| 133 | + public function resolvePath() |
| 134 | + { |
| 135 | + return $this->option('path') ?: $this->resolveDefaultPath(); |
| 136 | + } |
| 137 | + |
| 138 | + private function resolveDefaultPath() |
| 139 | + { |
| 140 | + return $this->option('git') |
| 141 | + ? '[email protected]:BinarCode/laravel-restify.git' |
| 142 | + : '../../binarcode/laravel-restify'; |
| 143 | + } |
| 144 | +} |
0 commit comments