1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resta\Support; |
4
|
|
|
|
5
|
|
|
use Resta\Exception\FileNotFoundException; |
6
|
|
|
|
7
|
|
|
class Generator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var null|string |
11
|
|
|
*/ |
12
|
|
|
protected $path; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var null|string |
16
|
|
|
*/ |
17
|
|
|
protected $name; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var null|string |
21
|
|
|
*/ |
22
|
|
|
protected $namespace; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $type = 'class'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var null|string |
31
|
|
|
*/ |
32
|
|
|
protected $stubPath; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Generator constructor. |
36
|
|
|
* @param null $path |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function __construct($path=null) |
39
|
|
|
{ |
40
|
|
|
$this->path = $path; |
41
|
|
|
|
42
|
|
|
$this->namespace = Utils::getNamespace($this->path); |
43
|
|
|
|
44
|
|
|
$this->createPath(); |
45
|
|
|
|
46
|
|
|
$this->stubPath = app()->corePath().'Console/Stubs/generator'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* creates directory for generator |
51
|
|
|
* |
52
|
|
|
* @return mixed|void |
53
|
|
|
*/ |
54
|
|
|
public function createPath() |
55
|
|
|
{ |
56
|
|
|
if(!file_exists($this->path)){ |
57
|
|
|
if(!files()->makeDirectory($this->path)){ |
58
|
|
|
exception()->runtime($this->path.' makeDirectory fail'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* creates file for generator |
65
|
|
|
* |
66
|
|
|
* @param null $name |
|
|
|
|
67
|
|
|
* @param array $replacement |
68
|
|
|
* |
69
|
|
|
* @throws FileNotFoundException |
70
|
|
|
*/ |
71
|
|
|
public function createFile($name=null,$replacement=array()) |
72
|
|
|
{ |
73
|
|
|
if(file_exists($this->path) && !is_null($name)){ |
|
|
|
|
74
|
|
|
|
75
|
|
|
$content = files()->get($this->getStubFile()); |
76
|
|
|
|
77
|
|
|
$file = $this->path.''.DIRECTORY_SEPARATOR.''.ucfirst($name).'.php'; |
78
|
|
|
|
79
|
|
|
if(files()->put($file,$content)!==FALSE){ |
80
|
|
|
|
81
|
|
|
$this->name = $name; |
82
|
|
|
$this->replaceFileContent($replacement,$file); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* get stub file for generator |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getStubFile() |
93
|
|
|
{ |
94
|
|
|
return $this->stubPath.''.DIRECTORY_SEPARATOR.''.$this->type.'.stub'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* replace with replacement variables content of the given file |
99
|
|
|
* |
100
|
|
|
* @param $replacement |
101
|
|
|
* @param $file |
102
|
|
|
* @return void |
103
|
|
|
* |
104
|
|
|
* @throws FileNotFoundException |
105
|
|
|
*/ |
106
|
|
|
private function replaceFileContent($replacement,$file) |
107
|
|
|
{ |
108
|
|
|
$replacementVariables = $this->replacementVariables($replacement); |
109
|
|
|
$content = files()->get($file); |
110
|
|
|
|
111
|
|
|
foreach ($replacementVariables as $key=>$replacementVariable){ |
112
|
|
|
$search = '/__'.$key.'__/'; |
113
|
|
|
$replace = $replacementVariable; |
114
|
|
|
$content = preg_replace($search,$replace,$content); |
115
|
|
|
} |
116
|
|
|
files()->replace($file,$content); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* get replacement variables |
121
|
|
|
* |
122
|
|
|
* @param array $replacement |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
private function replacementVariables($replacement=array()) |
126
|
|
|
{ |
127
|
|
|
$replacement['namespace'] = $this->namespace; |
128
|
|
|
$replacement['class'] = $this->name; |
129
|
|
|
|
130
|
|
|
return array_map(function($item){ |
131
|
|
|
return ucfirst($item); |
132
|
|
|
},$replacement); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|