Mercurial > packages > auth
annotate src/Console/InstallCommand.php @ 2:ac199a7a8931 default tip
Changes to the views and only defualt option is available
| author | luka |
|---|---|
| date | Tue, 19 Aug 2025 20:32:36 -0400 |
| parents | 90e38de8f2ba |
| children |
| rev | line source |
|---|---|
| 0 | 1 <?php |
| 2 | |
| 3 namespace Wizard\Auth\Console; | |
| 4 | |
| 5 use Illuminate\Console\Command; | |
| 6 use Illuminate\Filesystem\Filesystem; | |
| 7 use Illuminate\Support\Arr; | |
| 8 use Illuminate\Support\Str; | |
| 9 use RuntimeException; | |
| 10 use Symfony\Component\Console\Attribute\AsCommand; | |
| 11 use Symfony\Component\Console\Input\InputInterface; | |
| 12 use Symfony\Component\Console\Output\OutputInterface; | |
| 13 use Symfony\Component\Finder\Finder; | |
| 14 use Symfony\Component\Process\PhpExecutableFinder; | |
| 15 use Symfony\Component\Process\Process; | |
| 16 | |
| 17 use function Laravel\Prompts\confirm; | |
| 18 use function Laravel\Prompts\multiselect; | |
| 19 use function Laravel\Prompts\select; | |
| 20 | |
|
2
ac199a7a8931
Changes to the views and only defualt option is available
luka
parents:
0
diff
changeset
|
21 #[AsCommand(name: 'auth:install')] |
|
ac199a7a8931
Changes to the views and only defualt option is available
luka
parents:
0
diff
changeset
|
22 class InstallCommand extends Command |
| 0 | 23 { |
| 24 use InstallsBladeStack; | |
| 25 | |
| 26 /** | |
| 27 * The name and signature of the console command. | |
| 28 * | |
| 29 * @var string | |
| 30 */ | |
|
2
ac199a7a8931
Changes to the views and only defualt option is available
luka
parents:
0
diff
changeset
|
31 protected $signature = 'auth:install'; |
| 0 | 32 |
| 33 /** | |
| 34 * The console command description. | |
| 35 * | |
| 36 * @var string | |
| 37 */ | |
|
2
ac199a7a8931
Changes to the views and only defualt option is available
luka
parents:
0
diff
changeset
|
38 protected $description = 'Install the Basic Auth controllers and resources'; |
| 0 | 39 |
| 40 /** | |
| 41 * Execute the console command. | |
| 42 * | |
| 43 * @return int|null | |
| 44 */ | |
| 45 public function handle() | |
| 46 { | |
| 47 return $this->installBladeStack(); | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * Install Breeze's tests. | |
| 52 * | |
| 53 * @return bool | |
| 54 */ | |
| 55 protected function installTests() | |
| 56 { | |
| 57 (new Filesystem)->ensureDirectoryExists(base_path('tests/Feature')); | |
| 58 | |
|
2
ac199a7a8931
Changes to the views and only defualt option is available
luka
parents:
0
diff
changeset
|
59 (new Filesystem)->copyDirectory(__DIR__.'/../../stubs/default/tests/Feature', base_path('tests/Feature')); |
| 0 | 60 |
| 61 return true; | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * Install the given middleware names into the application. | |
| 66 * | |
| 67 * @param array|string $name | |
| 68 * @param string $group | |
| 69 * @param string $modifier | |
| 70 * @return void | |
| 71 */ | |
| 72 protected function installMiddleware($names, $group = 'web', $modifier = 'append') | |
| 73 { | |
| 74 $bootstrapApp = file_get_contents(base_path('bootstrap/app.php')); | |
| 75 | |
| 76 $names = collect(Arr::wrap($names)) | |
| 77 ->filter(fn ($name) => ! Str::contains($bootstrapApp, $name)) | |
| 78 ->whenNotEmpty(function ($names) use ($bootstrapApp, $group, $modifier) { | |
| 79 $names = $names->map(fn ($name) => "$name")->implode(','.PHP_EOL.' '); | |
| 80 | |
| 81 $stubs = [ | |
| 82 '->withMiddleware(function (Middleware $middleware) {', | |
| 83 '->withMiddleware(function (Middleware $middleware): void {', | |
| 84 ]; | |
| 85 | |
| 86 $bootstrapApp = str_replace( | |
| 87 $stubs, | |
| 88 collect($stubs)->transform(fn ($stub) => $stub | |
| 89 .PHP_EOL." \$middleware->$group($modifier: [" | |
| 90 .PHP_EOL." $names," | |
| 91 .PHP_EOL.' ]);' | |
| 92 .PHP_EOL | |
| 93 )->all(), | |
| 94 $bootstrapApp, | |
| 95 ); | |
| 96 | |
| 97 file_put_contents(base_path('bootstrap/app.php'), $bootstrapApp); | |
| 98 }); | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * Install the given middleware aliases into the application. | |
| 103 * | |
| 104 * @param array $aliases | |
| 105 * @return void | |
| 106 */ | |
| 107 protected function installMiddlewareAliases($aliases) | |
| 108 { | |
| 109 $bootstrapApp = file_get_contents(base_path('bootstrap/app.php')); | |
| 110 | |
| 111 $aliases = collect($aliases) | |
| 112 ->filter(fn ($alias) => ! Str::contains($bootstrapApp, $alias)) | |
| 113 ->whenNotEmpty(function ($aliases) use ($bootstrapApp) { | |
| 114 $aliases = $aliases->map(fn ($name, $alias) => "'$alias' => $name")->implode(','.PHP_EOL.' '); | |
| 115 | |
| 116 $stubs = [ | |
| 117 '->withMiddleware(function (Middleware $middleware) {', | |
| 118 '->withMiddleware(function (Middleware $middleware): void {', | |
| 119 ]; | |
| 120 | |
| 121 $bootstrapApp = str_replace( | |
| 122 $stubs, | |
| 123 collect($stubs)->transform(fn ($stub) => $stub | |
| 124 .PHP_EOL.' $middleware->alias([' | |
| 125 .PHP_EOL." $aliases," | |
| 126 .PHP_EOL.' ]);' | |
| 127 .PHP_EOL | |
| 128 )->all(), | |
| 129 $bootstrapApp, | |
| 130 ); | |
| 131 | |
| 132 file_put_contents(base_path('bootstrap/app.php'), $bootstrapApp); | |
| 133 }); | |
| 134 } | |
| 135 | |
| 136 /** | |
| 137 * Determine if the given Composer package is installed. | |
| 138 * | |
| 139 * @param string $package | |
| 140 * @return bool | |
| 141 */ | |
| 142 protected function hasComposerPackage($package) | |
| 143 { | |
| 144 $packages = json_decode(file_get_contents(base_path('composer.json')), true); | |
| 145 | |
| 146 return array_key_exists($package, $packages['require'] ?? []) | |
| 147 || array_key_exists($package, $packages['require-dev'] ?? []); | |
| 148 } | |
| 149 | |
| 150 /** | |
| 151 * Installs the given Composer Packages into the application. | |
| 152 * | |
| 153 * @param bool $asDev | |
| 154 * @return bool | |
| 155 */ | |
| 156 protected function requireComposerPackages(array $packages, $asDev = false) | |
| 157 { | |
| 158 $composer = $this->option('composer'); | |
| 159 | |
| 160 if ($composer !== 'global') { | |
| 161 $command = ['php', $composer, 'require']; | |
| 162 } | |
| 163 | |
| 164 $command = array_merge( | |
| 165 $command ?? ['composer', 'require'], | |
| 166 $packages, | |
| 167 $asDev ? ['--dev'] : [], | |
| 168 ); | |
| 169 | |
| 170 return (new Process($command, base_path(), ['COMPOSER_MEMORY_LIMIT' => '-1'])) | |
| 171 ->setTimeout(null) | |
| 172 ->run(function ($type, $output) { | |
| 173 $this->output->write($output); | |
| 174 }) === 0; | |
| 175 } | |
| 176 | |
| 177 /** | |
| 178 * Removes the given Composer Packages from the application. | |
| 179 * | |
| 180 * @param bool $asDev | |
| 181 * @return bool | |
| 182 */ | |
| 183 protected function removeComposerPackages(array $packages, $asDev = false) | |
| 184 { | |
| 185 $composer = $this->option('composer'); | |
| 186 | |
| 187 if ($composer !== 'global') { | |
| 188 $command = ['php', $composer, 'remove']; | |
| 189 } | |
| 190 | |
| 191 $command = array_merge( | |
| 192 $command ?? ['composer', 'remove'], | |
| 193 $packages, | |
| 194 $asDev ? ['--dev'] : [], | |
| 195 ); | |
| 196 | |
| 197 return (new Process($command, base_path(), ['COMPOSER_MEMORY_LIMIT' => '-1'])) | |
| 198 ->setTimeout(null) | |
| 199 ->run(function ($type, $output) { | |
| 200 $this->output->write($output); | |
| 201 }) === 0; | |
| 202 } | |
| 203 | |
| 204 /** | |
| 205 * Update the dependencies in the "package.json" file. | |
| 206 * | |
| 207 * @param bool $dev | |
| 208 * @return void | |
| 209 */ | |
| 210 protected static function updateNodePackages(callable $callback, $dev = true) | |
| 211 { | |
| 212 if (! file_exists(base_path('package.json'))) { | |
| 213 return; | |
| 214 } | |
| 215 | |
| 216 $configurationKey = $dev ? 'devDependencies' : 'dependencies'; | |
| 217 | |
| 218 $packages = json_decode(file_get_contents(base_path('package.json')), true); | |
| 219 | |
| 220 $packages[$configurationKey] = $callback( | |
| 221 array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : [], | |
| 222 $configurationKey | |
| 223 ); | |
| 224 | |
| 225 ksort($packages[$configurationKey]); | |
| 226 | |
| 227 file_put_contents( | |
| 228 base_path('package.json'), | |
| 229 json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).PHP_EOL | |
| 230 ); | |
| 231 } | |
| 232 | |
| 233 /** | |
| 234 * Update the scripts in the "package.json" file. | |
| 235 * | |
| 236 * @return void | |
| 237 */ | |
| 238 protected static function updateNodeScripts(callable $callback) | |
| 239 { | |
| 240 if (! file_exists(base_path('package.json'))) { | |
| 241 return; | |
| 242 } | |
| 243 | |
| 244 $content = json_decode(file_get_contents(base_path('package.json')), true); | |
| 245 | |
| 246 $content['scripts'] = $callback( | |
| 247 array_key_exists('scripts', $content) ? $content['scripts'] : [] | |
| 248 ); | |
| 249 | |
| 250 file_put_contents( | |
| 251 base_path('package.json'), | |
| 252 json_encode($content, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).PHP_EOL | |
| 253 ); | |
| 254 } | |
| 255 | |
| 256 /** | |
| 257 * Delete the "node_modules" directory and remove the associated lock files. | |
| 258 * | |
| 259 * @return void | |
| 260 */ | |
| 261 protected static function flushNodeModules() | |
| 262 { | |
| 263 tap(new Filesystem, function ($files) { | |
| 264 $files->deleteDirectory(base_path('node_modules')); | |
| 265 | |
| 266 $files->delete(base_path('pnpm-lock.yaml')); | |
| 267 $files->delete(base_path('yarn.lock')); | |
| 268 $files->delete(base_path('bun.lock')); | |
| 269 $files->delete(base_path('bun.lockb')); | |
| 270 $files->delete(base_path('deno.lock')); | |
| 271 $files->delete(base_path('package-lock.json')); | |
| 272 }); | |
| 273 } | |
| 274 | |
| 275 /** | |
| 276 * Replace a given string within a given file. | |
| 277 * | |
| 278 * @param string $search | |
| 279 * @param string $replace | |
| 280 * @param string $path | |
| 281 * @return void | |
| 282 */ | |
| 283 protected function replaceInFile($search, $replace, $path) | |
| 284 { | |
| 285 file_put_contents($path, str_replace($search, $replace, file_get_contents($path))); | |
| 286 } | |
| 287 | |
| 288 /** | |
| 289 * Get the path to the appropriate PHP binary. | |
| 290 * | |
| 291 * @return string | |
| 292 */ | |
| 293 protected function phpBinary() | |
| 294 { | |
| 295 if (function_exists('Illuminate\Support\php_binary')) { | |
| 296 return \Illuminate\Support\php_binary(); | |
| 297 } | |
| 298 | |
| 299 return (new PhpExecutableFinder)->find(false) ?: 'php'; | |
| 300 } | |
| 301 | |
| 302 /** | |
| 303 * Run the given commands. | |
| 304 * | |
| 305 * @param array $commands | |
| 306 * @return void | |
| 307 */ | |
| 308 protected function runCommands($commands) | |
| 309 { | |
| 310 $process = Process::fromShellCommandline(implode(' && ', $commands), null, null, null, null); | |
| 311 | |
| 312 if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) { | |
| 313 try { | |
| 314 $process->setTty(true); | |
| 315 } catch (RuntimeException $e) { | |
| 316 $this->output->writeln(' <bg=yellow;fg=black> WARN </> '.$e->getMessage().PHP_EOL); | |
| 317 } | |
| 318 } | |
| 319 | |
| 320 $process->run(function ($type, $line) { | |
| 321 $this->output->write(' '.$line); | |
| 322 }); | |
| 323 } | |
| 324 | |
| 325 /** | |
| 326 * Remove Tailwind dark classes from the given files. | |
| 327 * | |
| 328 * @return void | |
| 329 */ | |
| 330 protected function removeDarkClasses(Finder $finder) | |
| 331 { | |
| 332 foreach ($finder as $file) { | |
| 333 file_put_contents($file->getPathname(), preg_replace('/\sdark:[^\s"\']+/', '', $file->getContents())); | |
| 334 } | |
| 335 } | |
| 336 | |
| 337 /** | |
| 338 * Determine whether the project is already using Pest. | |
| 339 * | |
| 340 * @return bool | |
| 341 */ | |
| 342 protected function isUsingPest() | |
| 343 { | |
| 344 return class_exists(\Pest\TestSuite::class); | |
| 345 } | |
| 346 } |
