1
|
|
|
<?php namespace Wn\Generators\Commands; |
2
|
|
|
|
3
|
|
|
use Illuminate\Console\Command; |
4
|
|
|
use Illuminate\Filesystem\Filesystem; |
5
|
|
|
use Wn\Generators\Argument\ArgumentFormatLoader; |
6
|
|
|
use Wn\Generators\Argument\ArgumentParser; |
7
|
|
|
use Wn\Generators\Template\TemplateLoader; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class BaseCommand extends Command { |
11
|
|
|
|
12
|
|
|
protected $fs; |
13
|
|
|
protected $templates; |
14
|
|
|
|
15
|
|
|
public function __construct(Filesystem $fs) |
16
|
|
|
{ |
17
|
|
|
parent::__construct(); |
18
|
|
|
|
19
|
|
|
$this->fs = $fs; |
20
|
|
|
$this->templates = new TemplateLoader($fs); |
21
|
|
|
$this->argumentFormatLoader = new ArgumentFormatLoader($fs); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function getTemplate($name) |
25
|
|
|
{ |
26
|
|
|
return $this->templates->load($name); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function getArgumentParser($name){ |
30
|
|
|
$format = $this->argumentFormatLoader->load($name); |
31
|
|
|
return new ArgumentParser($format); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function save($content, $path, $name, $force = false) |
35
|
|
|
{ |
36
|
|
|
if (!$force && $this->fs->exists($path) && $this->input->hasOption('force') && !$this->option('force')) { |
37
|
|
|
$this->info("{$name} already exists; use --force option to override it !"); |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
$this->makeDirectory($path); |
41
|
|
|
$this->fs->put($path, $content); |
42
|
|
|
$this->info("{$name} generated in {$path}!"); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function makeDirectory($path) |
46
|
|
|
{ |
47
|
|
|
if (!$this->fs->isDirectory(dirname($path))) { |
48
|
|
|
$this->fs->makeDirectory(dirname($path), 0777, true, true); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function spaces($n): string |
53
|
|
|
{ |
54
|
|
|
return str_repeat(' ', $n); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getNamespace(string $path = null): string |
58
|
|
|
{ |
59
|
|
|
if (! $path) { |
|
|
|
|
60
|
|
|
$path = $this->option('path'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return implode('\\', array_map('studly_case', array_filter(array_map('trim', explode('/', $path)), function($value) { |
64
|
|
|
return !empty($value); |
65
|
|
|
}))); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function parseValue($value, string $parser) |
69
|
|
|
{ |
70
|
|
|
if(! $value){ |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if(! $this->option('parsed')){ |
75
|
|
|
return $this->getArgumentParser($parser)->parse($value); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $value; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function prependNamespace(string $value, string $path = null): string |
82
|
|
|
{ |
83
|
|
|
if (strpos($value, '\\') === false) { |
84
|
|
|
return $this->getNamespace($path) . '\\' . studly_case(str_singular($value)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function extractClassName(string $fqClassName): string |
91
|
|
|
{ |
92
|
|
|
$names = array_reverse(explode("\\", $fqClassName)); |
93
|
|
|
return $names[0]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: