|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Repository\Generators; |
|
4
|
|
|
|
|
5
|
|
|
class Stub |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The base path of stub file. |
|
9
|
|
|
* |
|
10
|
|
|
* @var null|string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected static $basePath = null; |
|
13
|
|
|
/** |
|
14
|
|
|
* The stub path. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $path; |
|
19
|
|
|
/** |
|
20
|
|
|
* The replacements array. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $replaces = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The contructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $path |
|
30
|
|
|
* @param array $replaces |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($path, array $replaces = []) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->path = $path; |
|
35
|
|
|
$this->replaces = $replaces; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Create new self instance. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $path |
|
42
|
|
|
* @param array $replaces |
|
43
|
|
|
* |
|
44
|
|
|
* @return self |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function create($path, array $replaces = []) |
|
47
|
|
|
{ |
|
48
|
|
|
return new static($path, $replaces); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set base path. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $path |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function setBasePath($path) |
|
59
|
|
|
{ |
|
60
|
|
|
static::$basePath = $path; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Set replacements array. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $replaces |
|
67
|
|
|
* |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
|
|
public function replace(array $replaces = []) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->replaces = $replaces; |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get replacements. |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getReplaces() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->replaces; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Handle magic method __toString. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function __toString() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->render(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get stub contents. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function render() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->getContents(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get stub contents. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getContents() |
|
113
|
|
|
{ |
|
114
|
|
|
$contents = file_get_contents($this->getPath()); |
|
115
|
|
|
foreach ($this->replaces as $search => $replace) { |
|
116
|
|
|
$contents = str_replace('$'.strtoupper($search).'$', $replace, $contents); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $contents; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get stub path. |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getPath() |
|
128
|
|
|
{ |
|
129
|
|
|
return static::$basePath.$this->path; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set stub path. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $path |
|
136
|
|
|
* |
|
137
|
|
|
* @return self |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setPath($path) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->path = $path; |
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|