1
|
|
|
<?php namespace Way\Generators\Commands; |
2
|
|
|
|
3
|
|
|
use Illuminate\Console\Command; |
4
|
|
|
use Illuminate\Contracts\Filesystem\FileExistsException; |
5
|
|
|
use Illuminate\Support\Facades\Config; |
6
|
|
|
use Way\Generators\Generator; |
7
|
|
|
|
8
|
|
|
abstract class GeneratorCommand extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The Generator instance. |
12
|
|
|
* |
13
|
|
|
* @var Generator |
14
|
|
|
*/ |
15
|
|
|
protected $generator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create a new GeneratorCommand instance. |
19
|
|
|
* |
20
|
|
|
* @param Generator $generator |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Generator $generator) |
23
|
|
|
{ |
24
|
|
|
$this->generator = $generator; |
25
|
|
|
|
26
|
|
|
parent::__construct(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Fetch the template data. |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
abstract protected function getTemplateData(): array; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The path to where the file will be created. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
abstract protected function getFileGenerationPath(): string; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the path to the generator template. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
abstract protected function getTemplatePath(): string; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Compile and generate the file. |
52
|
|
|
*/ |
53
|
|
|
public function create() |
54
|
|
|
{ |
55
|
|
|
$filePathToGenerate = $this->getFileGenerationPath(); |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
$this->generator->make( |
59
|
|
|
$this->getTemplatePath(), |
60
|
|
|
$this->getTemplateData(), |
61
|
|
|
$filePathToGenerate |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$this->info("Created: {$filePathToGenerate}"); |
65
|
|
|
} catch (FileExistsException $e) { |
66
|
|
|
$this->error("The file, {$filePathToGenerate}, already exists! I don't want to overwrite it."); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get a directory path through a command option, or from the configuration. |
72
|
|
|
* |
73
|
|
|
* @param string $option |
74
|
|
|
* @param string $configName |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function getPathByOptionOrConfig(string $option, string $configName): string |
78
|
|
|
{ |
79
|
|
|
if ($path = (string) $this->option($option)) { |
80
|
|
|
return $path; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return (string) Config::get("generators.config.{$configName}"); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|