Skip to content

Commit

Permalink
Keep the file that caused an error for inspection (#1325)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
  • Loading branch information
ghostwriter committed Aug 7, 2023
2 parents 68782e9 + 1b7d230 commit 2351243
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions library/Mockery/Loader/RequireLoader.php
Expand Up @@ -15,16 +15,31 @@

class RequireLoader implements Loader
{
/**
* @var string
*/
protected $path;

/**
* @var string
*/
protected $lastPath = '';

public function __construct($path = null)
{
$this->path = realpath($path) ?: sys_get_temp_dir();

register_shutdown_function([$this, '__destruct']);
}

public function __destruct()
{
foreach (glob($this->path . DIRECTORY_SEPARATOR . 'Mockery_*.php') as $file) {
$files = array_diff(
glob($this->path . DIRECTORY_SEPARATOR . 'Mockery_*.php')?:[],
[$this->lastPath]
);

foreach ($files as $file) {
@unlink($file);
}
}
Expand All @@ -35,10 +50,12 @@ public function load(MockDefinition $definition)
return;
}

$fileName = sprintf('%s%s%s.php', $this->path, DIRECTORY_SEPARATOR, uniqid('Mockery_'));
$this->lastPath = sprintf('%s%s%s.php', $this->path, DIRECTORY_SEPARATOR, uniqid('Mockery_'));

file_put_contents($fileName, $definition->getCode());
file_put_contents($this->lastPath, $definition->getCode());

require $fileName;
if (file_exists($this->lastPath)){
require $this->lastPath;
}
}
}

0 comments on commit 2351243

Please sign in to comment.