Skip to content

Commit

Permalink
fix(MonkeyPatching) handle feel fopen
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Nov 23, 2023
1 parent ed36263 commit 18c4079
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/MonkeyPatch/FileStreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,24 @@ private function patchFile(string $absPath): string
*/
private function openFile(string $absPath, string $mode, bool $useIncludePath): void
{
if (!file_exists($absPath) && !is_dir(dirname($absPath))) {
/*
* The file open operation will never succeed, so we don't even try.
* The `w`, `c` and `x` modes will create the file if it does not exist,
* but will not create the directory structure to it
*/
return;
}

if (isset($this->context)) {
$handle = fopen($absPath, $mode, $useIncludePath, $this->context);
} else {
$handle = fopen($absPath, $mode, $useIncludePath);
}

if (!is_resource($handle)) {
throw new MonkeyPatchingException("Could not open file $absPath.");
return;
// throw new MonkeyPatchingException("Could not open file $absPath.");
}

$this->fileResource = $handle;
Expand Down
6 changes: 6 additions & 0 deletions src/WordPress/FileRequests/FileRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ public function execute(): array
if (($errno & $this->errorLevel) === 0) {
return true;
}

// Ignore E_WARNING from `fopen` calls: they are used by some plugins to try and check if a file exists.
if ($errno === E_WARNING && str_starts_with($errstr, 'fopen')) {
return true;
}

throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}, E_ALL);

Expand Down

0 comments on commit 18c4079

Please sign in to comment.