9use Psr\Log\LoggerInterface;
10use Psr\Log\NullLogger;
11use UnexpectedValueException;
12use Wikimedia\ObjectFactory\ObjectFactory;
23trait SpecBasedFactoryTrait {
28 protected string $langCode;
30 protected ?LoggerInterface
$logger =
null;
37 protected array $singletons = [];
44 abstract protected function getSpecMap(): array;
56 abstract protected function getSpecArgs( array
$spec, LoggerInterface
$logger ): array;
69 protected function createFromSpec(
string $class ) {
78 if ( !isset(
$specs[$class] ) ) {
79 throw new UnexpectedValueException(
"Class not registered: $class" );
83 $logger = $this->logger ??
new NullLogger();
86 if ( !empty(
$spec[
'args'] ) && is_array(
$spec[
'args'] ) ) {
98 throw new UnexpectedValueException(
99 "Expected instance of $class, got " . get_class(
$instance )