9use Psr\Log\LoggerInterface;
10use Psr\Log\NullLogger;
11use UnexpectedValueException;
12use Wikimedia\ObjectFactory\ObjectFactory;
23trait SpecBasedFactoryTrait {
28 protected string $langCode;
33 protected ?LoggerInterface
$logger =
null;
40 protected array $singletons = [];
47 abstract protected function getSpecMap(): array;
59 abstract protected function getSpecArgs( array
$spec, LoggerInterface
$logger ): array;
72 protected function createFromSpec(
string $class ) {
81 if ( !isset(
$specs[$class] ) ) {
82 throw new UnexpectedValueException(
"Class not registered: $class" );
86 $logger = $this->logger ??
new NullLogger();
89 if ( !empty(
$spec[
'args'] ) && is_array(
$spec[
'args'] ) ) {
101 throw new UnexpectedValueException(
102 "Expected instance of $class, got " . get_class(
$instance )