Skip to content

Commit

Permalink
Refactor library/Mockery/Loader/* (#1388)
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 Mar 6, 2024
2 parents ee709e1 + 9f53026 commit 4b9e432
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
14 changes: 10 additions & 4 deletions library/Mockery/Loader/EvalLoader.php
Expand Up @@ -4,23 +4,29 @@
* Mockery (https://docs.mockery.io/)
*
* @copyright https://github.com/mockery/mockery/blob/HEAD/COPYRIGHT.md
* @license https://github.com/mockery/mockery/blob/HEAD/LICENSE BSD 3-Clause License
* @link https://github.com/mockery/mockery for the canonical source repository
* @license https://github.com/mockery/mockery/blob/HEAD/LICENSE BSD 3-Clause License
* @link https://github.com/mockery/mockery for the canonical source repository
*/

namespace Mockery\Loader;

use Mockery\Generator\MockDefinition;
use Mockery\Loader\Loader;

use function class_exists;

class EvalLoader implements Loader
{
/**
* Load the given mock definition
*
* @return void
*/
public function load(MockDefinition $definition)
{
if (class_exists($definition->getClassName(), false)) {
return;
}

eval("?>" . $definition->getCode());
eval('?>' . $definition->getCode());
}
}
9 changes: 7 additions & 2 deletions library/Mockery/Loader/Loader.php
Expand Up @@ -4,8 +4,8 @@
* Mockery (https://docs.mockery.io/)
*
* @copyright https://github.com/mockery/mockery/blob/HEAD/COPYRIGHT.md
* @license https://github.com/mockery/mockery/blob/HEAD/LICENSE BSD 3-Clause License
* @link https://github.com/mockery/mockery for the canonical source repository
* @license https://github.com/mockery/mockery/blob/HEAD/LICENSE BSD 3-Clause License
* @link https://github.com/mockery/mockery for the canonical source repository
*/

namespace Mockery\Loader;
Expand All @@ -14,5 +14,10 @@

interface Loader
{
/**
* Load the given mock definition
*
* @return void
*/
public function load(MockDefinition $definition);
}
38 changes: 25 additions & 13 deletions library/Mockery/Loader/RequireLoader.php
Expand Up @@ -4,57 +4,69 @@
* Mockery (https://docs.mockery.io/)
*
* @copyright https://github.com/mockery/mockery/blob/HEAD/COPYRIGHT.md
* @license https://github.com/mockery/mockery/blob/HEAD/LICENSE BSD 3-Clause License
* @link https://github.com/mockery/mockery for the canonical source repository
* @license https://github.com/mockery/mockery/blob/HEAD/LICENSE BSD 3-Clause License
* @link https://github.com/mockery/mockery for the canonical source repository
*/

namespace Mockery\Loader;

use Mockery\Generator\MockDefinition;
use Mockery\Loader\Loader;

use function array_diff;
use function class_exists;
use function file_exists;
use function file_put_contents;
use function glob;
use function realpath;
use function sprintf;
use function sys_get_temp_dir;
use function uniqid;
use function unlink;

use const DIRECTORY_SEPARATOR;

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

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

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

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

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

foreach ($files as $file) {
@unlink($file);
}
}

/**
* Load the given mock definition
*
* @return void
*/
public function load(MockDefinition $definition)
{
if (class_exists($definition->getClassName(), false)) {
return;
}

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

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

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

0 comments on commit 4b9e432

Please sign in to comment.