-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* develop: specify next release call all registered cleanup callbacks instead of the last declared one directly open the readme
- Loading branch information
Showing
8 changed files
with
262 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\Immutable; | ||
|
||
final class RegisterCleanup | ||
{ | ||
/** @var callable(): void */ | ||
private $cleanup; | ||
private ?self $child = null; | ||
|
||
/** | ||
* @psalm-mutation-free | ||
* | ||
* @param callable(): void $cleanup | ||
*/ | ||
private function __construct(callable $cleanup) | ||
{ | ||
$this->cleanup = $cleanup; | ||
} | ||
|
||
/** | ||
* @param callable(): void $cleanup | ||
*/ | ||
public function __invoke(callable $cleanup): void | ||
{ | ||
$this->cleanup = $cleanup; | ||
} | ||
|
||
/** | ||
* @internal | ||
* @psalm-pure | ||
*/ | ||
public static function noop(): self | ||
{ | ||
return new self(static fn() => null); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function push(): self | ||
{ | ||
return $this->child = self::noop(); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function pop(): void | ||
{ | ||
$this->child = null; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function cleanup(): void | ||
{ | ||
if ($this->child) { | ||
$this->child->cleanup(); | ||
} | ||
|
||
($this->cleanup)(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.