|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Igorsgm\GitHooks\Console\Commands; |
| 4 | + |
| 5 | +use Igorsgm\GitHooks\Facades\GitHooks; |
| 6 | +use Illuminate\Console\Concerns\CreatesMatchingTest; |
| 7 | +use Illuminate\Console\GeneratorCommand; |
| 8 | +use Illuminate\Support\Str; |
| 9 | +use Symfony\Component\Console\Input\InputArgument; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | + |
| 12 | +class MakeHook extends GeneratorCommand |
| 13 | +{ |
| 14 | + use CreatesMatchingTest; |
| 15 | + |
| 16 | + /** |
| 17 | + * The console command name. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected $name = 'git-hooks:make'; |
| 22 | + |
| 23 | + /** |
| 24 | + * The console command description. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $description = 'Create a new Hook'; |
| 29 | + |
| 30 | + /** |
| 31 | + * The type of class being generated. |
| 32 | + * |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + protected $type = 'Git Hook'; |
| 36 | + |
| 37 | + /** |
| 38 | + * Execute the console command. |
| 39 | + * |
| 40 | + * @return bool|null |
| 41 | + * |
| 42 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 43 | + */ |
| 44 | + public function handle() |
| 45 | + { |
| 46 | + $supportedHooks = GitHooks::getSupportedHooks(); |
| 47 | + |
| 48 | + if (! in_array($this->argument('hookType'), $supportedHooks)) { |
| 49 | + $this->getOutput()->writeln(sprintf( |
| 50 | + '<bg=red;fg=white> ERROR </> Invalid hook type. Valid types are: <comment>%s</comment>', |
| 51 | + implode(', ', $supportedHooks) |
| 52 | + )); |
| 53 | + |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + return parent::handle(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Replace the class name for the given stub. |
| 62 | + * |
| 63 | + * @param string $stub |
| 64 | + * @param string $name |
| 65 | + * @return string |
| 66 | + */ |
| 67 | + protected function replaceClass($stub, $name) |
| 68 | + { |
| 69 | + $stub = parent::replaceClass($stub, $name); |
| 70 | + |
| 71 | + $hookName = Str::of($name)->classBasename()->snake()->replace('_', ' ')->title()->value(); |
| 72 | + |
| 73 | + return str_replace(['{{ hookName }}'], $hookName, $stub); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the stub file for the generator. |
| 78 | + * |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + protected function getStub() |
| 82 | + { |
| 83 | + $relativePath = '/stubs/'.$this->argument('hookType').'-console.stub'; |
| 84 | + |
| 85 | + return file_exists($customPath = $this->laravel->basePath(trim($relativePath, '/'))) |
| 86 | + ? $customPath |
| 87 | + : __DIR__.$relativePath; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get the default namespace for the class. |
| 92 | + * |
| 93 | + * @param string $rootNamespace |
| 94 | + * @return string |
| 95 | + */ |
| 96 | + protected function getDefaultNamespace($rootNamespace) |
| 97 | + { |
| 98 | + return $rootNamespace.'\Console\GitHooks'; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get the console command arguments. |
| 103 | + * |
| 104 | + * @return array |
| 105 | + */ |
| 106 | + protected function getArguments() |
| 107 | + { |
| 108 | + return [ |
| 109 | + ['hookType', InputArgument::REQUIRED, 'The type of the Git Hook', null, GitHooks::getSupportedHooks()], |
| 110 | + ['name', InputArgument::REQUIRED, 'The name of the Git Hook'], |
| 111 | + ]; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Get the console command options. |
| 116 | + * |
| 117 | + * @return array |
| 118 | + */ |
| 119 | + protected function getOptions() |
| 120 | + { |
| 121 | + return [ |
| 122 | + ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the Git Hook already exists'], |
| 123 | + ]; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Prompt for missing input arguments using the returned questions. |
| 128 | + * |
| 129 | + * @return array |
| 130 | + */ |
| 131 | + protected function promptForMissingArgumentsUsing() |
| 132 | + { |
| 133 | + $supportedHooks = implode(', ', GitHooks::getSupportedHooks()); |
| 134 | + |
| 135 | + return [ |
| 136 | + 'name' => 'What should the '.strtolower($this->type).' be named?', |
| 137 | + 'hookType' => 'What type of the '.strtolower($this->type)."? Possible values: ($supportedHooks)", |
| 138 | + ]; |
| 139 | + } |
| 140 | +} |
0 commit comments