diff --git a/library/Mockery/Loader/RequireLoader.php b/library/Mockery/Loader/RequireLoader.php index db5e8a27c..3ebe4d7f8 100644 --- a/library/Mockery/Loader/RequireLoader.php +++ b/library/Mockery/Loader/RequireLoader.php @@ -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); } } @@ -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; + } } }