1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Nwidart\Modules\Exceptions\FileAlreadyExistException; |
7
|
|
|
|
8
|
|
|
class FileGenerator extends Generator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The path wil be used. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $path; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The contens will be used. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $contents; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The laravel filesystem or null. |
26
|
|
|
* |
27
|
|
|
* @var \Illuminate\Filesystem\Filesystem|null |
28
|
|
|
*/ |
29
|
|
|
protected $filesystem; |
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $overwriteFile; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The constructor. |
37
|
|
|
* |
38
|
92 |
|
* @param $path |
39
|
|
|
* @param $contents |
40
|
92 |
|
* @param null $filesystem |
41
|
92 |
|
*/ |
42
|
92 |
|
public function __construct($path, $contents, $filesystem = null) |
43
|
92 |
|
{ |
44
|
|
|
$this->path = $path; |
45
|
|
|
$this->contents = $contents; |
46
|
|
|
$this->filesystem = $filesystem ?: new Filesystem(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
92 |
|
* Get contents. |
51
|
|
|
* |
52
|
92 |
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function getContents() |
55
|
|
|
{ |
56
|
|
|
return $this->contents; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set contents. |
61
|
|
|
* |
62
|
|
|
* @param mixed $contents |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function setContents($contents) |
67
|
|
|
{ |
68
|
|
|
$this->contents = $contents; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get filesystem. |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function getFilesystem() |
79
|
|
|
{ |
80
|
|
|
return $this->filesystem; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set filesystem. |
85
|
|
|
* |
86
|
|
|
* @param Filesystem $filesystem |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setFilesystem(Filesystem $filesystem) |
91
|
|
|
{ |
92
|
|
|
$this->filesystem = $filesystem; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
92 |
|
* Get path. |
99
|
|
|
* |
100
|
92 |
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function getPath() |
103
|
|
|
{ |
104
|
|
|
return $this->path; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set path. |
109
|
|
|
* |
110
|
|
|
* @param mixed $path |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function setPath($path) |
115
|
|
|
{ |
116
|
|
|
$this->path = $path; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
92 |
|
|
121
|
|
|
public function withFileOverwrite(bool $overwrite): FileGenerator |
122
|
92 |
|
{ |
123
|
92 |
|
$this->overwriteFile = $overwrite; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
3 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Generate the file. |
130
|
|
|
*/ |
131
|
|
|
public function generate() |
132
|
|
|
{ |
133
|
|
|
$path = $this->getPath(); |
134
|
|
|
if (!$this->filesystem->exists($path)) { |
135
|
|
|
return $this->filesystem->put($path, $this->getContents()); |
136
|
|
|
} |
137
|
|
|
if ($this->overwriteFile === true) { |
138
|
|
|
return $this->filesystem->put($path, $this->getContents()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
throw new FileAlreadyExistException('File already exists!'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|