Issues (197)

src/Support/Stub.php (1 issue)

1
<?php
2
3
namespace Salah3id\Domains\Support;
4
5
class Stub
6
{
7
    /**
8
     * The stub path.
9
     *
10
     * @var string
11
     */
12
    protected $path;
13
14
    /**
15
     * The base path of stub file.
16
     *
17
     * @var null|string
18
     */
19
    protected static $basePath = null;
20
21
    /**
22
     * The replacements array.
23
     *
24
     * @var array
25
     */
26
    protected $replaces = [];
27
28
    /**
29
     * The contructor.
30
     *
31
     * @param string $path
32
     * @param array  $replaces
33
     */
34
    public function __construct($path, array $replaces = [])
35
    {
36
        $this->path = $path;
37
        $this->replaces = $replaces;
38
    }
39
40
    /**
41
     * Create new self instance.
42
     *
43
     * @param string $path
44
     * @param array  $replaces
45
     *
46
     * @return self
47
     */
48
    public static function create($path, array $replaces = [])
49
    {
50
        return new static($path, $replaces);
51
    }
52
53
    /**
54
     * Set stub path.
55
     *
56
     * @param string $path
57
     *
58
     * @return self
59
     */
60
    public function setPath($path)
61
    {
62
        $this->path = $path;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Get stub path.
69
     *
70
     * @return string
71
     */
72
    public function getPath()
73
    {
74
        $path = static::getBasePath() . $this->path;
75
76
        return file_exists($path) ? $path : __DIR__ . '/../Commands/stubs' . $this->path;
77
    }
78
79
    /**
80
     * Set base path.
81
     *
82
     * @param string $path
83
     */
84
    public static function setBasePath($path)
85
    {
86
        static::$basePath = $path;
87
    }
88
89
    /**
90
     * Get base path.
91
     *
92
     * @return string|null
93
     */
94
    public static function getBasePath()
95
    {
96
        return static::$basePath;
97
    }
98
99
    /**
100
     * Get stub contents.
101
     *
102
     * @return mixed|string
103
     */
104
    public function getContents()
105
    {
106
        $contents = file_get_contents($this->getPath());
107
108
        foreach ($this->replaces as $search => $replace) {
109
            $contents = str_replace('$' . strtoupper($search) . '$', $replace, $contents);
110
        }
111
112
        return $contents;
113
    }
114
115
    /**
116
     * Get stub contents.
117
     *
118
     * @return string
119
     */
120
    public function render()
121
    {
122
        return $this->getContents();
123
    }
124
125
    /**
126
     * Save stub to specific path.
127
     *
128
     * @param string $path
129
     * @param string $filename
130
     *
131
     * @return bool
132
     */
133
    public function saveTo($path, $filename)
134
    {
135
        return file_put_contents($path . '/' . $filename, $this->getContents());
0 ignored issues
show
Bug Best Practice introduced by
The expression return file_put_contents..., $this->getContents()) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
136
    }
137
138
    /**
139
     * Set replacements array.
140
     *
141
     * @param array $replaces
142
     *
143
     * @return $this
144
     */
145
    public function replace(array $replaces = [])
146
    {
147
        $this->replaces = $replaces;
148
149
        return $this;
150
    }
151
152
    /**
153
     * Get replacements.
154
     *
155
     * @return array
156
     */
157
    public function getReplaces()
158
    {
159
        return $this->replaces;
160
    }
161
162
    /**
163
     * Handle magic method __toString.
164
     *
165
     * @return string
166
     */
167
    public function __toString()
168
    {
169
        return $this->render();
170
    }
171
}
172