From 1e1c1009f2016dfa7e7e1550fe18ba61edf010a2 Mon Sep 17 00:00:00 2001 From: lukmzig <30526586+lukmzig@users.noreply.github.com> Date: Fri, 24 Mar 2023 13:02:51 +0100 Subject: [PATCH] [Improvement]: Check hardcoded namespaces in models/DataObject/ClassDefinition/Data/Extension/Relation.php (#14407) * feat: use asset type loaders instead of hardcoded namespace * fix: consider class overwrites * fix: consider unknown class names * fix: use class resolver instead of the type loader * fix: use factory for data objects --- .../Data/Extension/Relation.php | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/models/DataObject/ClassDefinition/Data/Extension/Relation.php b/models/DataObject/ClassDefinition/Data/Extension/Relation.php index c456e0f6197..c49493cb6b3 100644 --- a/models/DataObject/ClassDefinition/Data/Extension/Relation.php +++ b/models/DataObject/ClassDefinition/Data/Extension/Relation.php @@ -16,11 +16,14 @@ namespace Pimcore\Model\DataObject\ClassDefinition\Data\Extension; +use Pimcore\Loader\ImplementationLoader\Exception\UnsupportedException; use Pimcore\Model\Asset; use Pimcore\Model\DataObject\AbstractObject; use Pimcore\Model\Document; use Pimcore\Model\Document\Page; use Pimcore\Model\Document\Snippet; +use Pimcore\Model\Factory; +use Pimcore\Resolver\ClassResolver; trait Relation { @@ -30,12 +33,16 @@ trait Relation protected function getPhpDocClassString(bool $asArray = false): string { $types = []; + $factory = \Pimcore::getContainer()->get('pimcore.model.factory'); // add documents if ($this->getDocumentsAllowed()) { if ($documentTypes = $this->getDocumentTypes()) { + $resolver = \Pimcore::getContainer()->get('pimcore.class.resolver.document'); foreach ($documentTypes as $item) { - $types[] = sprintf('\Pimcore\Model\Document\%s', ucfirst($item['documentTypes'])); + if ($className = $this->resolveClassName($factory, $resolver, $item['documentTypes'])) { + $types[] = $className; + } } } else { $types[] = '\\' . Page::class; @@ -47,8 +54,11 @@ protected function getPhpDocClassString(bool $asArray = false): string // add assets if ($this->getAssetsAllowed()) { if ($assetTypes = $this->getAssetTypes()) { + $resolver = \Pimcore::getContainer()->get('pimcore.class.resolver.asset'); foreach ($assetTypes as $item) { - $types[] = sprintf('\Pimcore\Model\Asset\%s', ucfirst($item['assetTypes'])); + if ($className = $this->resolveClassName($factory, $resolver, $item['assetTypes'])) { + $types[] = $className; + } } } else { $types[] = '\\' . Asset::class; @@ -59,7 +69,11 @@ protected function getPhpDocClassString(bool $asArray = false): string if ($this->getObjectsAllowed()) { if ($classes = $this->getClasses()) { foreach ($classes as $item) { - $types[] = sprintf('\Pimcore\Model\DataObject\%s', ucfirst($item['classes'])); + try { + $types[] = $factory->getClassNameFor(sprintf('\Pimcore\Model\DataObject\%s', ucfirst($item['classes']))); + } catch (UnsupportedException) { + continue; + } } } else { $types[] = '\\' . AbstractObject::class; @@ -111,4 +125,17 @@ public function getObjectsAllowed(): bool { return false; } + + private function resolveClassName(Factory $factory, ClassResolver $resolver, string $type): ?string + { + if ($className = $resolver->resolve($type)) { + try { + return $factory->getClassNameFor($className); + } catch (UnsupportedException) { + return null; + } + } + + return null; + } }