-
-
Save alemohamad/a86634e0ae03f017e082 to your computer and use it in GitHub Desktop.
Ejemplo usado en la charla Comandos Artesanales (https://slides.com/alemohamad/comandos-artesanales).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputArgument; | |
class ExampleCommand extends Command { | |
/** | |
* El nombre del comando. | |
* | |
* @var string | |
*/ | |
protected $name = 'alemohamad:example'; | |
/** | |
* La descripcion del comando. | |
* | |
* @var string | |
*/ | |
protected $description = "Ejemplo usado en la charla Comandos Artesanales."; | |
/** | |
* Ejecuta el comando en la consola. | |
* | |
* @return void | |
*/ | |
public function fire() | |
{ | |
$this->info('Vamos, que esto funciona!'); | |
// Tomamos el argumento nombre y la opcion edad del ingreso del usuario. | |
$nombre = $this->argument('nombre'); | |
$edad = $this->option('edad'); | |
// Confirmamos que el usuario ingreso correctamente su edad, y si no lo hizo | |
// le preguntamos nuevamente. | |
if ( ! $this->confirm("¿Realmente tenés {$edad} años? [yes|no]", true)) | |
{ | |
$edad = $this->ask('¿Entonces qué edad tenés?'); | |
} | |
$this->comment("Entonces podemos decir que {$nombre} tiene {$edad} años."); | |
} | |
/** | |
* Tomamos los argumentos del comando en la consola. | |
* | |
* @return array | |
*/ | |
protected function getArguments() | |
{ | |
return array( | |
array('nombre', InputArgument::REQUIRED, 'Nombre del usuario.'), | |
); | |
} | |
/** | |
* Tomamos las opciones del comando en la consola. | |
* | |
* @return array | |
*/ | |
protected function getOptions() | |
{ | |
return array( | |
array('edad', null, InputOption::VALUE_REQUIRED, 'Edad del usuario.', null) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment