Last active
April 14, 2020 09:29
-
-
Save finagin/425ad26dc1e17fc56024509f13a1d44e to your computer and use it in GitHub Desktop.
Must have Laravel Commands
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 Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Artisan; | |
use Illuminate\Support\Facades\Log; | |
use RuntimeException; | |
use Throwable; | |
class IdeHelperCommand extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'ide-helper | |
{--s|skip=* : Skip any of ["generate", "meta", "models"]}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Command description'; | |
protected static $commands = [ | |
'generate' => [], | |
'meta' => [], | |
'models' => [ | |
'--filename' => '.phpstorm.models.php', | |
'--nowrite' => true, | |
], | |
]; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
try { | |
$this->throwIfDisabledIdeHelper(); | |
$this->throwIfSkippedAll(); | |
$this->eachCommand(function ($command, $parameters) { | |
if ($code = Artisan::call($command, $parameters)) { | |
throw new RuntimeException($command.' event returned with error code '.$code, $code); | |
} | |
$this->line(Artisan::output()); | |
}); | |
} catch (Throwable $e) { | |
$this->renderException($e); | |
return $e->getCode(); | |
} | |
return 0; | |
} | |
/** | |
* Command toString formatter | |
* | |
* @param string $command | |
* @param array $parameters | |
* | |
* @return string | |
*/ | |
protected static function commandToString(string $command, array $parameters = []): string | |
{ | |
$parameters = implode( | |
' ', | |
array_map( | |
function ($val, $key) { | |
return is_bool($val) | |
? sprintf("%s", $key) | |
: sprintf("%s=%s", $key, $val); | |
}, | |
$parameters, | |
array_keys($parameters) | |
) | |
); | |
return $command.' '.$parameters; | |
} | |
/** | |
* Check skipped all commands | |
* | |
* @throws \RuntimeException | |
*/ | |
protected function throwIfSkippedAll(): void | |
{ | |
if (empty(array_diff(array_keys(static::$commands), $this->option('skip')))) { | |
throw new RuntimeException('You skipped all the commands', 1); | |
} | |
} | |
/** | |
* Check Ide Helper is disabled | |
* | |
* @throws \RuntimeException | |
*/ | |
protected function throwIfDisabledIdeHelper(): void | |
{ | |
$isDisabledIdeHelper = !class_exists(IdeHelperServiceProvider::class) | |
|| app()->environment('production'); | |
if ($isDisabledIdeHelper) { | |
throw new RuntimeException('Ide Helper is disabled', 0); | |
} | |
} | |
/** | |
* Commands iterator | |
* | |
* @param callable $callback | |
*/ | |
protected function eachCommand(callable $callback): void | |
{ | |
foreach (static::$commands as $command => $parameters) { | |
if (in_array($command, $this->option('skip'))) { | |
continue; | |
} | |
$command = 'ide-helper:'.$command; | |
$this->sharedOptions($parameters); | |
$this->line(static::commandToString($command, $parameters)); | |
$callback($command, $parameters); | |
} | |
} | |
/** | |
* Append shared options | |
* | |
* @param array $parameters | |
*/ | |
protected function sharedOptions(array &$parameters): void | |
{ | |
if ($this->option('ansi')) { | |
$parameters['--ansi'] = true; | |
} | |
} | |
/** | |
* Render throwable exception | |
* | |
* @param \Throwable $throwable | |
*/ | |
protected function renderException(Throwable $throwable): void | |
{ | |
$outputLevel = $throwable->getCode() | |
? 'error' | |
: 'warn'; | |
$this->{$outputLevel}($throwable->getMessage()); | |
$logLevel = $throwable->getCode() | |
? 'error' | |
: 'warning'; | |
Log::{$logLevel}($throwable->getMessage(), ['exception' => $throwable]); | |
} | |
} |
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\Foundation\Console\ModelMakeCommand as BaseModelMakeCommand; | |
class ModelMakeCommand extends BaseModelMakeCommand | |
{ | |
/** | |
* Get the default namespace for the class. | |
* | |
* @param string $rootNamespace | |
* | |
* @return string | |
*/ | |
protected function getDefaultNamespace($rootNamespace) | |
{ | |
return $rootNamespace.'\Models'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment