Completed
Push — master ( 9384cf...fa6152 )
by ARCANEDEV
22s
created

Stub::setRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php namespace Arcanedev\Assets\Helpers;
2
3
use Illuminate\Support\Facades\File;
4
5
/**
6
 * Class     Stub
7
 *
8
 * @package  Arcanedev\Assets\Helpers
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Stub
12
{
13
    /* -----------------------------------------------------------------
14
     |  Properties
15
     | -----------------------------------------------------------------
16
     */
17
18
    /** @var  string */
19
    protected $content;
20
21
    /* -----------------------------------------------------------------
22
     |  Main Methods
23
     | -----------------------------------------------------------------
24
     */
25
26
    /**
27
     * @param  string|null  $file
28
     *
29
     * @return static
30
     */
31
    public static function make($file = null)
32
    {
33
        return (new static)->setContent(
34
            static::load($file ?: static::getFilePath())
35
        );
36
    }
37
38
    /**
39
     * Get the stub file path.
40
     *
41
     * @return string
42
     *
43
     * @throws \Exception
44
     */
45
    protected static function getFilePath()
46
    {
47
        throw new \Exception('You need to specify the stub file path.');
48
    }
49
50
    /**
51
     * Get the stub content.
52
     *
53
     * @return string
54
     */
55
    public function content()
56
    {
57
        return $this->content;
58
    }
59
60
    /**
61
     * Set the stub content.
62
     *
63
     * @param  string  $content
64
     *
65
     * @return static
66
     */
67
    protected function setContent($content)
68
    {
69
        $this->content = $content;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Replace the placeholders.
76
     *
77
     * @param  string|array  $search
78
     * @param  string|array  $replace
79
     *
80
     * @return static
81
     */
82
    public function replace($search, $replace)
83
    {
84
        return $this->setContent(
85
            str_replace($search, $replace, $this->content)
86
        );
87
    }
88
89
    /**
90
     * Get the content.
91
     *
92
     * @param  string  $file
93
     *
94
     * @return string
95
     */
96
    public static function load($file)
97
    {
98
        return File::get(static::path($file));
99
    }
100
101
    /**
102
     * Save the content.
103
     *
104
     * @param  string  $path
105
     *
106
     * @return int
107
     */
108
    public function save($path)
109
    {
110
        return File::put($path, $this->content);
111
    }
112
113
    /**
114
     * Prepare the stub path.
115
     *
116
     * @param  string|null  $file
117
     *
118
     * @return string
119
     */
120
    public static function path($file = null)
121
    {
122
        $path = static::getBasePath();
123
124
        if ( ! is_null($file))
125
            $path = "{$path}/".trim($file, '/');
126
127
        return $path;
128
    }
129
130
    /**
131
     * Get the base stubs' path.
132
     *
133
     * @return string
134
     */
135
    public static function getBasePath()
136
    {
137
        return realpath(dirname(__DIR__, 2).'/stubs');
138
    }
139
140
    /**
141
     * Transform the stub content (json) to associated array.
142
     *
143
     * @return array
144
     */
145
    public function toArray()
146
    {
147
        return json_decode($this->content, true);
148
    }
149
}
150