|
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
|
|
|
* @param $path |
|
39
|
|
|
* @param $contents |
|
40
|
|
|
* @param null $filesystem |
|
41
|
|
|
*/ |
|
42
|
96 |
|
public function __construct($path, $contents, $filesystem = null) |
|
43
|
|
|
{ |
|
44
|
96 |
|
$this->path = $path; |
|
45
|
96 |
|
$this->contents = $contents; |
|
46
|
96 |
|
$this->filesystem = $filesystem ?: new Filesystem(); |
|
47
|
96 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get contents. |
|
51
|
|
|
* |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
96 |
|
public function getContents() |
|
55
|
|
|
{ |
|
56
|
96 |
|
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
|
|
|
* Get path. |
|
99
|
|
|
* |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
*/ |
|
102
|
96 |
|
public function getPath() |
|
103
|
|
|
{ |
|
104
|
96 |
|
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
|
|
|
|
|
121
|
96 |
|
public function withFileOverwrite(bool $overwrite): FileGenerator |
|
122
|
|
|
{ |
|
123
|
96 |
|
$this->overwriteFile = $overwrite; |
|
124
|
|
|
|
|
125
|
96 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Generate the file. |
|
130
|
|
|
*/ |
|
131
|
96 |
|
public function generate() |
|
132
|
|
|
{ |
|
133
|
96 |
|
$path = $this->getPath(); |
|
134
|
96 |
|
if (!$this->filesystem->exists($path)) { |
|
135
|
96 |
|
return $this->filesystem->put($path, $this->getContents()); |
|
136
|
|
|
} |
|
137
|
5 |
|
if ($this->overwriteFile === true) { |
|
138
|
2 |
|
return $this->filesystem->put($path, $this->getContents()); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
4 |
|
throw new FileAlreadyExistException('File already exists!'); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|