Skip to content

Commit

Permalink
[Improvement]: Check hardcoded namespaces in models/DataObject/ClassD…
Browse files Browse the repository at this point in the history
…efinition/Data/Extension/Relation.php (pimcore#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
  • Loading branch information
lukmzig authored Mar 24, 2023
1 parent 44f958a commit 1e1c100
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions models/DataObject/ClassDefinition/Data/Extension/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 1e1c100

Please sign in to comment.