mirror of
https://github.com/pestphp/pest.git
synced 2026-03-07 08:17:22 +01:00
41 lines
786 B
PHP
41 lines
786 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pest\Repositories;
|
|
|
|
use Closure;
|
|
use Pest\Exceptions\BeforeEachAlreadyExist;
|
|
use Pest\Support\NullClosure;
|
|
|
|
/**
|
|
* @internal
|
|
*/
|
|
final class BeforeEachRepository
|
|
{
|
|
/**
|
|
* @var array<string, Closure>
|
|
*/
|
|
private $state = [];
|
|
|
|
/**
|
|
* Sets a before each closure.
|
|
*/
|
|
public function set(string $filename, Closure $closure): void
|
|
{
|
|
if (array_key_exists($filename, $this->state)) {
|
|
throw new BeforeEachAlreadyExist($filename);
|
|
}
|
|
|
|
$this->state[$filename] = $closure;
|
|
}
|
|
|
|
/**
|
|
* Gets a before each closure by the given filename.
|
|
*/
|
|
public function get(string $filename): Closure
|
|
{
|
|
return $this->state[$filename] ?? NullClosure::create();
|
|
}
|
|
}
|