|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Log1x\HtmlForms\Console; |
| 4 | + |
| 5 | +use Illuminate\Support\Str; |
| 6 | +use Roots\Acorn\Console\Commands\GeneratorCommand; |
| 7 | + |
| 8 | +class FormMakeCommand extends GeneratorCommand |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The console command signature. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $signature = 'make:form {name* : The form slug.}'; |
| 16 | + |
| 17 | + /** |
| 18 | + * The console command description. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $description = 'Create a new form view for the HTML Forms plugin.'; |
| 23 | + |
| 24 | + /** |
| 25 | + * The view stub used when generated. |
| 26 | + * |
| 27 | + * @var string|bool |
| 28 | + */ |
| 29 | + protected $view = 'default'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Execute the console command. |
| 33 | + * |
| 34 | + * @return mixed |
| 35 | + */ |
| 36 | + public function handle() |
| 37 | + { |
| 38 | + $this->task("Generating {$this->getViewName()}", function () { |
| 39 | + if (! $this->files->exists($this->getViewPath())) { |
| 40 | + $this->files->makeDirectory($this->getViewPath()); |
| 41 | + } |
| 42 | + |
| 43 | + if ($this->files->exists($this->getView())) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + $this->files->put($this->getView(), $this->files->get($this->getViewStub())); |
| 48 | + }); |
| 49 | + |
| 50 | + return $this->summary(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Return the full view destination. |
| 55 | + * |
| 56 | + * @return string |
| 57 | + */ |
| 58 | + public function getView() |
| 59 | + { |
| 60 | + return Str::finish($this->getViewPath(), $this->getViewName()); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Return the view destination filename. |
| 65 | + * |
| 66 | + * @return string |
| 67 | + */ |
| 68 | + public function getViewName() |
| 69 | + { |
| 70 | + return Str::finish( |
| 71 | + str_replace('.', '/', Str::slug(Str::snake($this->getNameInput()))), |
| 72 | + '.blade.php' |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Return the view destination path. |
| 78 | + * |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + public function getViewPath() |
| 82 | + { |
| 83 | + return Str::finish($this->getPaths(), '/forms/'); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get the view stub file for the generator. |
| 88 | + * |
| 89 | + * @return string |
| 90 | + */ |
| 91 | + protected function getViewStub() |
| 92 | + { |
| 93 | + return __DIR__ . "/stubs/views/{$this->view}.stub"; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Return the applications view path. |
| 98 | + * |
| 99 | + * @param string $name |
| 100 | + * @return void |
| 101 | + */ |
| 102 | + protected function getPaths() |
| 103 | + { |
| 104 | + $paths = $this->app['view.finder']->getPaths(); |
| 105 | + |
| 106 | + if (count($paths) === 1) { |
| 107 | + return head($paths); |
| 108 | + } |
| 109 | + |
| 110 | + return $this->choice('Where do you want to create the view(s)?', $paths, head($paths)); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Return the block creation summary. |
| 115 | + * |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + protected function summary() |
| 119 | + { |
| 120 | + $this->line(''); |
| 121 | + $this->line('<fg=blue;options=bold>Form View Created</>'); |
| 122 | + $this->line(" ⮑ <fg=blue>{$this->shortenPath($this->getView(), 4)}</>"); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Returns a shortened path. |
| 127 | + * |
| 128 | + * @param string $path |
| 129 | + * @param int $i |
| 130 | + * @return string |
| 131 | + */ |
| 132 | + protected function shortenPath($path, $i = 3) |
| 133 | + { |
| 134 | + return collect( |
| 135 | + explode('/', $path) |
| 136 | + )->slice(-$i, $i)->implode('/'); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Get the stub file for the generator. |
| 141 | + * |
| 142 | + * @return string |
| 143 | + */ |
| 144 | + protected function getStub() |
| 145 | + { |
| 146 | + // |
| 147 | + } |
| 148 | +} |
0 commit comments