1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\ConsoleTasker; |
4
|
|
|
|
5
|
|
|
use Cerbero\ConsoleTasker\Traits\AddsLines; |
6
|
|
|
use Illuminate\Container\Container; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The manipulated file. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class ManipulatedFile |
13
|
|
|
{ |
14
|
|
|
use AddsLines; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The file path. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $path; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Whether the file was created. |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected $wasCreated; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The original content. |
32
|
|
|
* |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected $originalContent; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The reason why the file needs to be updated manually. |
39
|
|
|
* |
40
|
|
|
* @var string|null |
41
|
|
|
*/ |
42
|
|
|
protected $manualUpdateReason; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Instantiate the class. |
46
|
|
|
* |
47
|
|
|
* @param string $path |
48
|
|
|
*/ |
49
|
|
|
public function __construct(string $path) |
50
|
|
|
{ |
51
|
|
|
$this->path = $path; |
52
|
|
|
$this->wasCreated = !file_exists($path); |
53
|
|
|
$this->originalContent = $this->wasCreated ? null : file_get_contents($path); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Retrieve the file path |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getPath(): string |
62
|
|
|
{ |
63
|
|
|
return $this->path; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Retrieve the file relative path |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getRelativePath(): string |
72
|
|
|
{ |
73
|
|
|
$basePath = Container::getInstance()->make('path.base'); |
74
|
|
|
|
75
|
|
|
return substr($this->path, strlen($basePath) + 1); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Determine whether the file was created |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function wasCreated(): bool |
84
|
|
|
{ |
85
|
|
|
return $this->wasCreated; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Determine whether the file was updated |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function wasUpdated(): bool |
94
|
|
|
{ |
95
|
|
|
return !$this->wasCreated(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieve the original content |
100
|
|
|
* |
101
|
|
|
* @return string|null |
102
|
|
|
*/ |
103
|
|
|
public function getOriginalContent(): ?string |
104
|
|
|
{ |
105
|
|
|
return $this->originalContent; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Retrieve the reason why the file needs to be updated manually |
110
|
|
|
* |
111
|
|
|
* @return string|null |
112
|
|
|
*/ |
113
|
|
|
public function getManualUpdateReason(): ?string |
114
|
|
|
{ |
115
|
|
|
return $this->manualUpdateReason; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the reason why the file needs to be updated manually |
120
|
|
|
* |
121
|
|
|
* @param string $reason |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
|
|
public function needsManualUpdateTo(string $reason): self |
125
|
|
|
{ |
126
|
|
|
$this->manualUpdateReason = $reason; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Determine whether the file needs to be updated manually |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function needsManualUpdate(): bool |
137
|
|
|
{ |
138
|
|
|
return isset($this->manualUpdateReason); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|