|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Migratio\Resource\StubManager; |
|
4
|
|
|
|
|
5
|
|
|
use Migratio\Schema; |
|
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
7
|
|
|
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Stubber |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var $config |
|
|
|
|
|
|
13
|
|
|
*/ |
|
14
|
|
|
protected $config; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var $schema Schema |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
|
|
protected $schema; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var $stubber |
|
|
|
|
|
|
23
|
|
|
*/ |
|
24
|
|
|
protected $stubber; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var $file |
|
|
|
|
|
|
28
|
|
|
*/ |
|
29
|
|
|
protected $file; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct($schema) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->file = new Filesystem(); |
|
34
|
|
|
$this->stubber = realpath(__DIR__.'/../../Stub'); |
|
35
|
|
|
$this->schema = $schema; |
|
36
|
|
|
$this->config = $this->schema->getConfig(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param $params |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
private function getPaths($params) |
|
44
|
|
|
{ |
|
45
|
|
|
if(!isset($params[3])){ |
|
46
|
|
|
return [$this->config['paths'][0]]; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $executionPath |
|
52
|
|
|
* @param $path |
|
53
|
|
|
* @param $params |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
private function fopenprocess($executionPath,$path,$params) |
|
57
|
|
|
{ |
|
58
|
|
|
$dt = fopen($executionPath, "r"); |
|
59
|
|
|
$content = fread($dt, filesize($executionPath)); |
|
|
|
|
|
|
60
|
|
|
fclose($dt); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
foreach ($params as $key=>$value){ |
|
63
|
|
|
|
|
64
|
|
|
$content=str_replace("__".$key."__",$value,$content); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
$dt = fopen($path, "w"); |
|
69
|
|
|
fwrite($dt, $content); |
|
|
|
|
|
|
70
|
|
|
fclose($dt); |
|
71
|
|
|
|
|
72
|
|
|
return true; |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $params |
|
79
|
|
|
*/ |
|
80
|
|
|
public function get($params) |
|
81
|
|
|
{ |
|
82
|
|
|
list($table,$name,$type) = $params; |
|
83
|
|
|
|
|
84
|
|
|
$results = []; |
|
85
|
|
|
|
|
86
|
|
|
$paths = $this->getPaths($params); |
|
87
|
|
|
|
|
88
|
|
|
foreach ($paths as $pathKey=>$path){ |
|
89
|
|
|
|
|
90
|
|
|
$tableDirectory = $path.'/'.ucfirst($table); |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
if(!file_exists($tableDirectory)){ |
|
94
|
|
|
$results['directory'][$pathKey]= $this->fileProcess($tableDirectory,'makeDirectory'); |
|
95
|
|
|
} |
|
96
|
|
|
else{ |
|
97
|
|
|
$results['directory'][$pathKey]= $this->getResult(false, |
|
98
|
|
|
'Already exist the specified directory'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$results['directory'][$pathKey]['table']= $table; |
|
102
|
|
|
$results['directory'][$pathKey]['directory']= $table; |
|
103
|
|
|
$results['directory'][$pathKey]['type']= $type; |
|
104
|
|
|
|
|
105
|
|
|
$fileName = ucfirst($name); |
|
106
|
|
|
|
|
107
|
|
|
$fileNameWithTime = time().'_'.$fileName; |
|
108
|
|
|
|
|
109
|
|
|
$filePath = $tableDirectory.'/'.$fileNameWithTime.'.php'; |
|
110
|
|
|
|
|
111
|
|
|
if(!file_exists($filePath)){ |
|
112
|
|
|
$results['file'][$pathKey]=$this->fileProcess($filePath,'makeFile'); |
|
113
|
|
|
} |
|
114
|
|
|
else{ |
|
115
|
|
|
$results['file'][$pathKey]= $this->getResult(false, |
|
116
|
|
|
'Already exist the specified file'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$results['file'][$pathKey]['table']=$table; |
|
120
|
|
|
$results['file'][$pathKey]['file']=$fileName; |
|
121
|
|
|
$results['file'][$pathKey]['type']=$type; |
|
122
|
|
|
|
|
123
|
|
|
$stubber = $this->stubber.'/'.$type.'.stub'; |
|
124
|
|
|
|
|
125
|
|
|
$this->fopenprocess($stubber,$filePath,['className'=>$fileName]); |
|
126
|
|
|
|
|
127
|
|
|
$this->file->chmod($tableDirectory,0777,000,true); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $results; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param $path |
|
135
|
|
|
* @param $process |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
private function fileProcess($path,$process) |
|
139
|
|
|
{ |
|
140
|
|
|
try { |
|
141
|
|
|
$this->{$process}($path); |
|
142
|
|
|
return $this->getResult(true,null); |
|
143
|
|
|
} catch (IOExceptionInterface $exception) { |
|
144
|
|
|
return $this->getResult(false, |
|
145
|
|
|
"An error occurred while creating your directory at ".$exception->getPath().""); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param $path |
|
151
|
|
|
* @param string $mode |
|
152
|
|
|
*/ |
|
153
|
|
|
private function makeDirectory($path,$mode='0777') |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->file->mkdir($path,$mode); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param $path |
|
160
|
|
|
*/ |
|
161
|
|
|
private function makeFile($path) |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->file->touch($path); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param $success |
|
168
|
|
|
* @param $message |
|
169
|
|
|
* @return array |
|
170
|
|
|
*/ |
|
171
|
|
|
private function getResult($success,$message) |
|
172
|
|
|
{ |
|
173
|
|
|
return [ |
|
174
|
|
|
'success'=>$success, |
|
175
|
|
|
'message'=>$message |
|
176
|
|
|
]; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
|