|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Microboard\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
|
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
|
|
10
|
|
|
abstract class WorkingWithStubs extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var Filesystem */ |
|
13
|
|
|
protected $files; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Set all Stubs replaces |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $variables = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param Filesystem $file |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Filesystem $file) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct(); |
|
28
|
|
|
|
|
29
|
|
|
$this->files = $file; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get stub content and replace variables, then put it into directory |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $stub |
|
36
|
|
|
* @param string $directory output directory |
|
37
|
|
|
* @param string $name file name |
|
38
|
|
|
* @param array $variables |
|
39
|
|
|
*/ |
|
40
|
|
|
public function createFromStub($stub, $directory, $name, $variables = []) |
|
41
|
|
|
{ |
|
42
|
|
|
try { |
|
43
|
|
|
$stub = str_replace( |
|
44
|
|
|
array_keys($variables), |
|
45
|
|
|
array_values($variables), |
|
46
|
|
|
$this->files->get($stub) |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
$directory = $this->makeDirectory($directory); |
|
50
|
|
|
|
|
51
|
|
|
$this->files->put("{$directory}/{$name}", $stub); |
|
52
|
|
|
} catch (FileNotFoundException $e) { |
|
53
|
|
|
$this->error($e->getMessage()); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Parse the class name and format according to the root namespace. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function qualifyClass($name) |
|
64
|
|
|
{ |
|
65
|
|
|
$name = ltrim($name, '\\/'); |
|
66
|
|
|
|
|
67
|
|
|
$rootNamespace = $this->rootNamespace(); |
|
68
|
|
|
|
|
69
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
|
70
|
|
|
return $name; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$name = str_replace('/', '\\', $name); |
|
74
|
|
|
|
|
75
|
|
|
return $this->qualifyClass( |
|
76
|
|
|
$this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get the default namespace for the class. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $rootNamespace |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function getDefaultNamespace($rootNamespace) |
|
87
|
|
|
{ |
|
88
|
|
|
return $rootNamespace; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the root namespace for the class. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function rootNamespace() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->laravel->getNamespace(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Build the directory for the class if necessary. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $path |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function makeDirectory($path) |
|
108
|
|
|
{ |
|
109
|
|
|
if (! $this->files->isDirectory($path)) { |
|
110
|
|
|
$this->files->makeDirectory($path, 0777, true, true); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $path; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get the desired class name from the input. |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
abstract protected function getNameInput(); |
|
122
|
|
|
} |
|
123
|
|
|
|