Skip to content

Commit 07533f9

Browse files
committed
Replace File with procedural functions (for vfsStream)
1 parent bd1828e commit 07533f9

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

src/Libraries/Manifests.php

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,42 +72,40 @@ public function publish($path): bool
7272
return $result;
7373
}
7474

75-
// Read in and verify a manifest from a file path
76-
protected function manifestFromFile($path): ?object
75+
// Read in and verify a manifest from a file
76+
protected function manifestFromFile($file): ?object
7777
{
7878
// Make sure the file is valid and accessible
79-
$file = new File($path);
80-
81-
if (! $file->isFile())
79+
if (! is_file($file))
8280
{
8381
if ($this->config->silent)
8482
{
85-
$error = lang('Files.fileNotFound', [$path]);
83+
$error = lang('Files.fileNotFound', [$file]);
8684
log_message('warning', $error);
8785
$this->messages[] = [$error, 'red'];
8886

8987
return null;
9088
}
9189

92-
throw FileNotFoundException::forFileNotFound($path);
90+
throw FileNotFoundException::forFileNotFound($file);
9391
}
9492

9593
// Make sure the file is JSON
96-
$manifest = file_get_contents($file->getRealPath());
94+
$manifest = file_get_contents($file);
9795
$manifest = json_decode($manifest);
9896
if ($manifest === NULL)
9997
{
10098
$errornum = json_last_error();
10199

102100
if ($this->config->silent)
103101
{
104-
$error = 'JSON Error #' . $errornum . '. ' . lang('Manifests.invalidFileFormat', [$path]);
102+
$error = 'JSON Error #' . $errornum . '. ' . lang('Manifests.invalidFileFormat', [$file]);
105103
log_message('warning', $error);
106104
$this->messages[] = [$error, 'red'];
107105
return null;
108106
}
109107

110-
throw ManifestsException::forInvalidFileFormat($path);
108+
throw ManifestsException::forInvalidFileFormat($file);
111109
}
112110

113111
// Verify necessary fields
@@ -117,13 +115,13 @@ protected function manifestFromFile($path): ?object
117115
{
118116
if ($this->config->silent)
119117
{
120-
$error = lang('Manifests.fieldMissingFromFile', [$field, $path]);
118+
$error = lang('Manifests.fieldMissingFromFile', [$field, $file]);
121119
log_message('warning', $error);
122120
$this->messages[] = [$error, 'red'];
123121
return null;
124122
}
125123

126-
throw ManifestsException::forFieldMissingFromFile($field, $path);
124+
throw ManifestsException::forFieldMissingFromFile($field, $file);
127125
}
128126
}
129127

@@ -193,17 +191,16 @@ protected function ensureDirectory($directory): bool
193191
// Create index.html in the destination to prevent list access
194192
protected function addIndexToDirectory($directory): bool
195193
{
196-
$path = $directory . 'index.html';
197-
$file = new File($path);
194+
$file = rtrim($directory, '/') . '/' . 'index.html';
198195

199196
// Check for existing file
200-
if ($file->isFile())
197+
if (is_file($file))
201198
{
202199
return true;
203200
}
204201

205202
// Directory should be writable but just in case...
206-
if (! $file->isWritable())
203+
if (! is_writable($file))
207204
{
208205
$error = lang('Manifests.directoryNotWritable', [$directory]);
209206
log_message('warning', $error);
@@ -212,10 +209,9 @@ protected function addIndexToDirectory($directory): bool
212209
}
213210

214211
// Do it
215-
$file = $file->openFile('w');
216-
if (! $file->fwrite($this->getIndexHtml))
212+
if (file_put_contents($file, $this->getIndexHtml) === false)
217213
{
218-
$error = lang('Manifests.cannotCreateIndexFile', [$path]);
214+
$error = lang('Manifests.cannotCreateIndexFile', [$file]);
219215
log_message('warning', $error);
220216
$this->messages[] = [$error, 'red'];
221217
return false;
@@ -265,22 +261,21 @@ protected function publishResource($resource): bool
265261
}
266262

267263
// Make sure the source exists
268-
$file = new File($resource->source);
269-
if (! $file->getRealPath())
264+
if (! file_exists($resource->source))
270265
{
271266
if ($this->config->silent)
272267
{
273-
$error = lang('Files.fileNotFound', [$path]);
268+
$error = lang('Files.fileNotFound', [$resource->source]);
274269
log_message('warning', $error);
275270
$this->messages[] = [$error, 'red'];
276271

277272
return false;
278273
}
279274

280-
throw FileNotFoundException::forFileNotFound($path);
275+
throw FileNotFoundException::forFileNotFound($resource->source);
281276
}
282277

283-
return $file->isDir() ?
278+
return is_dir($resource->source) ?
284279
$this->publishResourceDirectory($resource) :
285280
$this->publishFile($resource->source, $resource->destination);
286281
}

tests/manifests/ManifestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testLocate()
5151
public function testManifestFromFile()
5252
{
5353
$method = $this->getPrivateMethodInvoker($this->manifests, 'manifestFromFile');
54-
$path = SUPPORTPATH . 'Manifests/Widgets.json';
54+
$path = MODULESUPPORTPATH . 'Manifests/Widgets.json';
5555

5656
$manifest = $method($path);
5757

0 commit comments

Comments
 (0)