-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
244f358
commit cc89819
Showing
22 changed files
with
1,017 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\DoctrineMongoDBAdminBundle\FieldDescription; | ||
|
||
use Doctrine\ODM\MongoDB\DocumentManager; | ||
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; | ||
use Sonata\AdminBundle\FieldDescription\FieldDescriptionFactoryInterface; | ||
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription; | ||
|
||
final class FieldDescriptionFactory implements FieldDescriptionFactoryInterface | ||
{ | ||
/** | ||
* @var ManagerRegistry | ||
*/ | ||
private $registry; | ||
|
||
public function __construct(ManagerRegistry $registry) | ||
{ | ||
$this->registry = $registry; | ||
} | ||
|
||
public function create(string $class, string $name, array $options = []): FieldDescriptionInterface | ||
{ | ||
if (!isset($options['route']['name'])) { | ||
$options['route']['name'] = 'edit'; | ||
} | ||
|
||
if (!isset($options['route']['parameters'])) { | ||
$options['route']['parameters'] = []; | ||
} | ||
|
||
[$metadata, $propertyName, $parentAssociationMappings] = $this->getParentMetadataForProperty($class, $name); | ||
|
||
return new FieldDescription( | ||
$name, | ||
$options, | ||
$metadata->fieldMappings[$propertyName] ?? [], | ||
$metadata->associationMappings[$propertyName] ?? [], | ||
$parentAssociationMappings, | ||
$propertyName | ||
); | ||
} | ||
|
||
private function getParentMetadataForProperty(string $baseClass, string $propertyFullName): array | ||
{ | ||
$nameElements = explode('.', $propertyFullName); | ||
$lastPropertyName = array_pop($nameElements); | ||
$class = $baseClass; | ||
$parentAssociationMappings = []; | ||
|
||
foreach ($nameElements as $nameElement) { | ||
$metadata = $this->getMetadata($class); | ||
$parentAssociationMappings[] = $metadata->associationMappings[$nameElement]; | ||
$class = $metadata->getAssociationTargetClass($nameElement); | ||
} | ||
|
||
return [$this->getMetadata($class), $lastPropertyName, $parentAssociationMappings]; | ||
} | ||
|
||
/** | ||
* @param class-string $class | ||
*/ | ||
private function getMetadata(string $class): ClassMetadata | ||
{ | ||
return $this->getDocumentManager($class)->getClassMetadata($class); | ||
} | ||
|
||
/** | ||
* @param class-string $class | ||
* | ||
* @throw \RuntimeException | ||
*/ | ||
private function getDocumentManager(string $class): DocumentManager | ||
{ | ||
$dm = $this->registry->getManagerForClass($class); | ||
|
||
if (!$dm instanceof DocumentManager) { | ||
throw new \RuntimeException(sprintf('No document manager defined for class %s', $class)); | ||
} | ||
|
||
return $dm; | ||
} | ||
} |
Oops, something went wrong.