From 77cdaa77121cc9df0c6241f9a488d46b0cf7583f Mon Sep 17 00:00:00 2001 From: j3nsch Date: Thu, 11 Aug 2022 10:26:07 +0200 Subject: [PATCH] #291 Avoid using class_exists for decisions --- library/Opus/ModelFactory.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/library/Opus/ModelFactory.php b/library/Opus/ModelFactory.php index a65d4a01..5da86b2e 100644 --- a/library/Opus/ModelFactory.php +++ b/library/Opus/ModelFactory.php @@ -36,8 +36,18 @@ use function class_exists; +/** + * Creates model and model repository objects. + * + * + */ class ModelFactory implements ModelFactoryInterface { + /** @var string[] Mapping of model types to separate DocumentRepository classes */ + protected $repositoryClasses = [ + 'Document' => 'Opus\DocumentRepository' + ]; + /** * @param string $type * @return mixed @@ -73,12 +83,14 @@ public function get($type, $modelId) * @return mixed * * TODO reuse repository instance + * TODO Using class_exists causes exceptions in the autoloader, because it tries to load the class. If autoload + * is disabled, it doesn't find existing classes. Therefore mapping in $repositoryClasses was created to + * avoid having to check if a class exists for the decision which one to use. */ public function getRepository($type) { - $repositoryClass = 'Opus\\' . $type . 'Repository'; - - if (class_exists($repositoryClass)) { + if (array_key_exists($type, $this->repositoryClasses)) { + $repositoryClass = $this->repositoryClasses[$type]; return new $repositoryClass(); } else { // TODO in old implementation model classes also serve as "repositories"