|
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) |
|
53
|
|
|
{ |
|
54
|
|
|
return str_repeat(' ', $n); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function getNamespace($path = false) |
|
58
|
|
|
{ |
|
59
|
|
|
if (! $path) { |
|
60
|
|
|
$path = $this->option('path'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return str_replace(' ', '\\', ucwords(trim(str_replace('/', ' ', $path)))); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
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: