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