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