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 $file; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var null|object |
16
|
|
|
*/ |
17
|
|
|
protected $fileSystem; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var null|string |
21
|
|
|
*/ |
22
|
|
|
protected $path; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var null|string |
26
|
|
|
*/ |
27
|
|
|
protected $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var null|string |
31
|
|
|
*/ |
32
|
|
|
protected $namespace; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $type = 'class'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var null|string |
41
|
|
|
*/ |
42
|
|
|
protected $stubPath; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Generator constructor. |
46
|
|
|
* @param null $path |
|
|
|
|
47
|
|
|
* @param null $fileSystem |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
public function __construct($path=null,$fileSystem=null) |
50
|
|
|
{ |
51
|
|
|
$this->path = $path; |
52
|
|
|
|
53
|
|
|
$this->fileSystem = $fileSystem; |
54
|
|
|
|
55
|
|
|
$this->namespace = Utils::getNamespace($this->path); |
56
|
|
|
|
57
|
|
|
$this->createPath(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* creates directory for generator |
62
|
|
|
* |
63
|
|
|
* @return mixed|void |
64
|
|
|
*/ |
65
|
|
|
public function createPath() |
66
|
|
|
{ |
67
|
|
|
if(!file_exists($this->path)){ |
68
|
|
|
if(!$this->fileSystem->makeDirectory($this->path)){ |
|
|
|
|
69
|
|
|
throw new \Error($this->path.' makeDirectory fail'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* creates file for generator |
76
|
|
|
* |
77
|
|
|
* @param null $name |
|
|
|
|
78
|
|
|
* @param array $replacement |
79
|
|
|
* |
80
|
|
|
* @return Generator |
81
|
|
|
* |
82
|
|
|
* @throws FileNotFoundException |
83
|
|
|
*/ |
84
|
|
|
public function createFile($name=null,$replacement=array()) |
85
|
|
|
{ |
86
|
|
|
if(file_exists($this->path) && !is_null($name)){ |
|
|
|
|
87
|
|
|
|
88
|
|
|
$content = $this->fileSystem->get($this->getStubFile()); |
89
|
|
|
$this->file = $this->path.''.DIRECTORY_SEPARATOR.''.ucfirst($name).'.php'; |
90
|
|
|
|
91
|
|
|
if($this->fileSystem->put($this->file,$content)!==FALSE){ |
92
|
|
|
$this->name = $name; |
93
|
|
|
$this->replaceFileContent($replacement,$this->file); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* creates method to file for generator |
102
|
|
|
* |
103
|
|
|
* @param array $params |
104
|
|
|
*/ |
105
|
|
|
public function createMethod($params=array()) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* get stub file for generator |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getStubFile() |
116
|
|
|
{ |
117
|
|
|
$stubFile = $this->stubPath.''.DIRECTORY_SEPARATOR.''.$this->type.'.stub'; |
118
|
|
|
|
119
|
|
|
if(!file_exists($stubFile)){ |
120
|
|
|
throw new \Error($stubFile.' path is not available'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $stubFile; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* replace with replacement variables content of the given file |
128
|
|
|
* |
129
|
|
|
* @param $replacement |
130
|
|
|
* @param $file |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
private function replaceFileContent($replacement,$file) |
134
|
|
|
{ |
135
|
|
|
$replacementVariables = $this->replacementVariables($replacement); |
136
|
|
|
$content = $this->fileSystem->get($file); |
137
|
|
|
|
138
|
|
|
foreach ($replacementVariables as $key=>$replacementVariable){ |
139
|
|
|
$search = '/__'.$key.'__/'; |
140
|
|
|
$replace = $replacementVariable; |
141
|
|
|
$content = preg_replace($search,$replace,$content); |
142
|
|
|
} |
143
|
|
|
$this->fileSystem->replace($file,$content); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* get replacement variables |
148
|
|
|
* |
149
|
|
|
* @param array $replacement |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
private function replacementVariables($replacement=array()) |
153
|
|
|
{ |
154
|
|
|
$replacement['namespace'] = $this->namespace; |
155
|
|
|
$replacement['class'] = $this->name; |
156
|
|
|
|
157
|
|
|
return array_map(function($item){ |
158
|
|
|
return ucfirst($item); |
159
|
|
|
},$replacement); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* set stub path |
164
|
|
|
* |
165
|
|
|
* @param $stubPath |
166
|
|
|
*/ |
167
|
|
|
public function setStubPath($stubPath) |
168
|
|
|
{ |
169
|
|
|
$this->stubPath = $stubPath; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|