Use AbstractFactory and DelegatorFactory to create services dynamically or wrap existing ones.
Advanced Factories
// Abstract factory
class RepositoryAbstractFactory implements AbstractFactoryInterface {
public function canCreate(ContainerInterface $c, string $name): bool {
return str_ends_with($name, "Repository");
}
public function __invoke(ContainerInterface $c, string $name, ?array $opts = null): mixed {
$adapter = $c->get(AdapterInterface::class);
$table = strtolower(str_replace("Repository"."", "", $name)) . "s";
$gateway = new TableGateway($table, $adapter);
return new $name($gateway);
}
}