forked from coenjacobs/mozart
-
Notifications
You must be signed in to change notification settings - Fork 25
/
File.php
158 lines (132 loc) · 3.85 KB
/
File.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace BrianHenryIE\Strauss\Files;
use BrianHenryIE\Strauss\Types\DiscoveredSymbol;
class File implements FileBase
{
/**
* @var string The absolute path to the file on disk.
*/
protected string $sourceAbsolutePath;
/**
* Should this file be copied to the target directory?
*/
protected bool $doCopy = true;
/**
* Should this file be deleted from the source directory?
*/
protected bool $doDelete = false;
/** @var DiscoveredSymbol[] */
protected array $discoveredSymbols = [];
protected string $absoluteTargetPath;
protected bool $didDelete = false;
public function __construct(string $sourceAbsolutePath)
{
$this->sourceAbsolutePath = $sourceAbsolutePath;
}
public function getSourcePath(string $relativeTo = ''): string
{
return str_replace($relativeTo, '', $this->sourceAbsolutePath);
}
public function isPhpFile(): bool
{
return substr($this->sourceAbsolutePath, -4) === '.php';
}
/**
* Some combination of file copy exclusions and vendor-dir == target-dir
*
* @param bool $doCopy
*
* @return void
*/
public function setDoCopy(bool $doCopy): void
{
$this->doCopy = $doCopy;
}
public function isDoCopy(): bool
{
return $this->doCopy;
}
public function setDoPrefix(bool $doPrefix): void
{
}
/**
* Is this correct? Is there ever a time that NO changes should be made to a file? I.e. another file would have its
* namespace changed and it needs to be updated throughout.
*
* Is this really a Symbol level function?
*/
public function isDoPrefix(): bool
{
return true;
}
/**
* Used to mark files that are symlinked as not-to-be-deleted.
*
* @param bool $doDelete
*/
public function setDoDelete(bool $doDelete): void
{
$this->doDelete = $doDelete;
}
/**
* Should file be deleted?
*
* NB: Also respect the "delete_vendor_files"|"delete_vendor_packages" settings.
*/
public function isDoDelete(): bool
{
return $this->doDelete;
}
public function setDidDelete(bool $didDelete): void
{
$this->didDelete = $didDelete;
}
public function getDidDelete(): bool
{
return $this->didDelete;
}
public function addDiscoveredSymbol(DiscoveredSymbol $symbol): void
{
$this->discoveredSymbols[$symbol->getOriginalSymbol()] = $symbol;
}
/**
* @return array<string, DiscoveredSymbol> The discovered symbols in the file, indexed by their original string name.
*/
public function getDiscoveredSymbols(): array
{
return $this->discoveredSymbols;
}
public function setAbsoluteTargetPath(string $absoluteTargetPath): void
{
$this->absoluteTargetPath = $absoluteTargetPath;
}
/**
* The target path to (maybe) copy the file to, and the target path to perform replacements in (which may be the
* original path).
*/
public function getAbsoluteTargetPath(string $relativeTo = ''): string
{
return isset($this->absoluteTargetPath)
? str_replace($relativeTo, '', $this->absoluteTargetPath)
: $this->getSourcePath($relativeTo);
}
public function getContents(): string
{
// TODO: use flysystem
// $contents = $this->filesystem->read($targetFile);
$contents = file_get_contents($this->sourceAbsolutePath);
if (false === $contents) {
throw new \Exception("Failed to read file at {$this->sourceAbsolutePath}");
}
return $contents;
}
protected bool $didUpdate = false;
public function setDidUpdate(): void
{
$this->didUpdate = true;
}
public function getDidUpdate(): bool
{
return $this->didUpdate;
}
}