Completed
Push — master ( e43114...eadd48 )
by ARCANEDEV
06:13
created

Stub::replaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 1
crap 1
1
<?php namespace Arcanedev\Support;
2
3
/**
4
 * Class     Stub
5
 *
6
 * @package  Arcanedev\Support
7
 * @author   ARCANEDEV <[email protected]>
8
 */
9
class Stub
10
{
11
    /* ------------------------------------------------------------------------------------------------
12
     |  Properties
13
     | ------------------------------------------------------------------------------------------------
14
     */
15
    /**
16
     * The stub path.
17
     *
18
     * @var string
19
     */
20
    protected $path;
21
22
    /**
23
     * The base path of stub file.
24
     *
25
     * @var string|null
26
     */
27
    protected static $basePath = null;
28
29
    /**
30
     * The replacements array.
31
     *
32
     * @var array
33
     */
34
    protected $replaces = [];
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Constructor
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Create a new instance.
42
     *
43
     * @param  string  $path
44
     * @param  array   $replaces
45
     */
46 24
    public function __construct($path, array $replaces = [])
47
    {
48 24
        $this->setPath($path);
49 24
        $this->setReplaces($replaces);
50 24
    }
51
52
    /* ------------------------------------------------------------------------------------------------
53
     |  Getters & Setters
54
     | ------------------------------------------------------------------------------------------------
55
     */
56
    /**
57
     * Get stub path.
58
     *
59
     * @return string
60
     */
61 24
    public function getPath()
62
    {
63 24
        $path = $this->path;
64
65 24
        if ( ! empty(static::$basePath)) {
66 8
            $path = static::$basePath . DS . ltrim($path, DS);
67 6
        }
68
69 24
        return $path;
70
    }
71
72
    /**
73
     * Set stub path.
74
     *
75
     * @param  string  $path
76
     *
77
     * @return self
78
     */
79 24
    public function setPath($path)
80
    {
81 24
        $this->path = $path;
82
83 24
        return $this;
84
    }
85
86
    /**
87
     * Get base path.
88
     *
89
     * @return string|null
90
     */
91 24
    public static function getBasePath()
92
    {
93 24
        return static::$basePath;
94
    }
95
96
    /**
97
     * Set base path.
98
     *
99
     * @param  string  $path
100
     */
101 24
    public static function setBasePath($path)
102
    {
103 24
        static::$basePath = $path;
104 24
    }
105
106
    /**
107
     * Get replacements.
108
     *
109
     * @return array
110
     */
111 24
    public function getReplaces()
112
    {
113 24
        return $this->replaces;
114
    }
115
116
    /**
117
     * Set replacements array.
118
     *
119
     * @param  array  $replaces
120
     *
121
     * @return self
122
     */
123 24
    public function setReplaces(array $replaces = [])
124
    {
125 24
        $this->replaces = $replaces;
126
127 24
        return $this;
128
    }
129
130
    /**
131
     * Set replacements array.
132
     *
133
     * @param  array  $replaces
134
     *
135
     * @return self
136
     */
137 8
    public function replaces(array $replaces = [])
138
    {
139 8
        return $this->setReplaces($replaces);
140
    }
141
142
    /* ------------------------------------------------------------------------------------------------
143
     |  Main Functions
144
     | ------------------------------------------------------------------------------------------------
145
     */
146
    /**
147
     * Create new self instance.
148
     *
149
     * @param  string  $path
150
     * @param  array   $replaces
151
     *
152
     * @return self
153
     */
154 16
    public static function create($path, array $replaces = [])
155
    {
156 16
        return new static($path, $replaces);
157
    }
158
159
    /**
160
     * Create new self instance from full path.
161
     *
162
     * @param  string  $path
163
     * @param  array   $replaces
164
     *
165
     * @return self
166
     */
167 8
    public static function createFromPath($path, array $replaces = [])
168
    {
169 8
        $stub = static::create($path, $replaces);
170 8
        $stub->setBasePath('');
171
172 8
        return $stub;
173
    }
174
175
    /**
176
     * Get stub contents.
177
     *
178
     * @return string
179
     */
180 16
    public function render()
181
    {
182 16
        return $this->getContents();
183
    }
184
185
    /**
186
     * Save stub to base path.
187
     *
188
     * @param  string  $filename
189
     *
190
     * @return bool
191
     */
192 8
    public function save($filename)
193
    {
194 8
        return $this->saveTo(self::getBasePath(), $filename);
195
    }
196
197
    /**
198
     * Save stub to specific path.
199
     *
200
     * @param  string  $path
201
     * @param  string  $filename
202
     *
203
     * @return bool
204
     */
205 8
    public function saveTo($path, $filename)
206
    {
207 8
        return file_put_contents($path . DS . $filename, $this->render());
208
    }
209
210
    /**
211
     * Get stub contents.
212
     *
213
     * @return string|mixed
214
     */
215 16
    public function getContents()
216
    {
217 16
        $contents = file_get_contents($this->getPath());
218
219 16
        foreach ($this->getReplaces() as $search => $replace) {
220 8
            $contents = str_replace('$' . strtoupper($search) . '$', $replace, $contents);
221 12
        }
222
223 16
        return $contents;
224
    }
225
226
    /**
227
     * Handle magic method __toString.
228
     *
229
     * @return string
230
     */
231 8
    public function __toString()
232
    {
233 8
        return $this->render();
234
    }
235
}
236