Skip to content

Commit

Permalink
Merge pull request #47 from koriym/clear
Browse files Browse the repository at this point in the history
Add ScriptInjector::clear() method
  • Loading branch information
koriym authored May 7, 2018
2 parents 1a89539 + 89bd2f4 commit 504404f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/ScriptInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public function __wakeup()
$this->scriptDir,
function () {
$module = $this->scriptDir . self::MODULE;
return file_exists($module) ? \unserialize(\file_get_contents($module)) : new EmptyModule();

return \file_exists($module) ? \unserialize(\file_get_contents($module)) : new EmptyModule();
}
);
}
Expand All @@ -140,6 +141,17 @@ public function getInstance($interface, $name = Name::ANY)
return $instance;
}

public function clear()
{
$unlink = function ($path) use (&$unlink) {
foreach (\glob(\rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*') as $file) {
\is_dir($file) ? $unlink($file) : \unlink($file);
@\rmdir($file);
}
};
$unlink($this->scriptDir);
}

public function isSingleton($dependencyIndex) : bool
{
$module = \unserialize(\file_get_contents($this->scriptDir . self::MODULE));
Expand Down
16 changes: 16 additions & 0 deletions tests/ScriptInjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,20 @@ function () {
$result = $aop->returnSame(1);
$this->assertSame(2, $result);
}

public function testClear()
{
$injector = new ScriptInjector(
$_ENV['TMP_DIR'],
function () {
return new FakeCarModule;
}
);
$injector->getInstance(FakeCar::class);
$count = \count(\glob($_ENV['TMP_DIR'] . '/*'));
$this->assertGreaterThan(0, $count);
$injector->clear();
$countAfterClear = \count(\glob($_ENV['TMP_DIR'] . '/*'));
$this->assertSame(0, $countAfterClear);
}
}

0 comments on commit 504404f

Please sign in to comment.