From c6654d6449343a9b9fd287adaec8f8ad9b0d3734 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Thu, 29 Jul 2021 16:01:24 +0200 Subject: [PATCH] Add review suggestions --- .../DataMapper/AuthorDataMapper.php | 12 +++--- ...erface.php => ContactFactoryInterface.php} | 6 +-- Content/Domain/Model/AuthorInterface.php | 6 +-- Content/Domain/Model/AuthorTrait.php | 8 ++-- .../{UserFactory.php => ContactFactory.php} | 20 ++++----- .../Sulu/Admin/ContentViewBuilderFactory.php | 21 +++++++++- .../Sulu/Form/SettingsFormMetadataVisitor.php | 42 +++++++------------ .../Compiler/SettingsFormPass.php | 25 ++++++----- Resources/config/data-mapper.xml | 2 +- Resources/config/form-visitor.xml | 3 +- .../config/forms/content_settings_author.xml | 8 ++-- Resources/config/services.xml | 5 ++- Resources/translations/admin.de.json | 4 +- Resources/translations/admin.en.json | 4 +- Tests/Application/.env | 9 +++- Tests/Application/.env.dev | 4 +- .../Admin/ContentViewBuilderFactoryTest.php | 3 +- 17 files changed, 101 insertions(+), 81 deletions(-) rename Content/Domain/Factory/{UserFactoryInterface.php => ContactFactoryInterface.php} (64%) rename Content/Infrastructure/Doctrine/{UserFactory.php => ContactFactory.php} (55%) diff --git a/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapper.php b/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapper.php index ef256adb..9c661313 100644 --- a/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapper.php +++ b/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapper.php @@ -13,20 +13,20 @@ namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper; -use Sulu\Bundle\ContentBundle\Content\Domain\Factory\UserFactoryInterface; +use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContactFactoryInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface; class AuthorDataMapper implements DataMapperInterface { /** - * @var UserFactoryInterface + * @var ContactFactoryInterface */ - private $userFactory; + private $contactFactory; - public function __construct(UserFactoryInterface $userFactory) + public function __construct(ContactFactoryInterface $contactFactory) { - $this->userFactory = $userFactory; + $this->contactFactory = $contactFactory; } public function map( @@ -62,7 +62,7 @@ public function map( private function setAuthorData(AuthorInterface $dimensionContent, array $data): void { if (isset($data['author'])) { - $dimensionContent->setAuthor($this->userFactory->create($data['author'])); + $dimensionContent->setAuthor($this->contactFactory->create($data['author'])); } if (isset($data['authored'])) { diff --git a/Content/Domain/Factory/UserFactoryInterface.php b/Content/Domain/Factory/ContactFactoryInterface.php similarity index 64% rename from Content/Domain/Factory/UserFactoryInterface.php rename to Content/Domain/Factory/ContactFactoryInterface.php index b14e094c..aef9825e 100644 --- a/Content/Domain/Factory/UserFactoryInterface.php +++ b/Content/Domain/Factory/ContactFactoryInterface.php @@ -13,9 +13,9 @@ namespace Sulu\Bundle\ContentBundle\Content\Domain\Factory; -use Sulu\Component\Security\Authentication\UserInterface; +use Sulu\Bundle\ContactBundle\Entity\ContactInterface; -interface UserFactoryInterface +interface ContactFactoryInterface { - public function create(?int $userId): ?UserInterface; + public function create(?int $contactId): ?ContactInterface; } diff --git a/Content/Domain/Model/AuthorInterface.php b/Content/Domain/Model/AuthorInterface.php index 2680227e..68e5d72d 100644 --- a/Content/Domain/Model/AuthorInterface.php +++ b/Content/Domain/Model/AuthorInterface.php @@ -13,13 +13,13 @@ namespace Sulu\Bundle\ContentBundle\Content\Domain\Model; -use Sulu\Component\Security\Authentication\UserInterface; +use Sulu\Bundle\ContactBundle\Entity\ContactInterface; interface AuthorInterface { - public function getAuthor(): ?UserInterface; + public function getAuthor(): ?ContactInterface; - public function setAuthor(?UserInterface $author): void; + public function setAuthor(?ContactInterface $author): void; public function getAuthored(): ?\DateTimeImmutable; diff --git a/Content/Domain/Model/AuthorTrait.php b/Content/Domain/Model/AuthorTrait.php index c07f810d..799396f7 100644 --- a/Content/Domain/Model/AuthorTrait.php +++ b/Content/Domain/Model/AuthorTrait.php @@ -13,7 +13,7 @@ namespace Sulu\Bundle\ContentBundle\Content\Domain\Model; -use Sulu\Component\Security\Authentication\UserInterface; +use Sulu\Bundle\ContactBundle\Entity\ContactInterface; /** * Basic implementation of the AuthorTrait. @@ -21,7 +21,7 @@ trait AuthorTrait { /** - * @var UserInterface|null + * @var ContactInterface|null */ private $author; @@ -30,12 +30,12 @@ trait AuthorTrait */ private $authored; - public function getAuthor(): ?UserInterface + public function getAuthor(): ?ContactInterface { return $this->author; } - public function setAuthor(?UserInterface $author): void + public function setAuthor(?ContactInterface $author): void { $this->author = $author; } diff --git a/Content/Infrastructure/Doctrine/UserFactory.php b/Content/Infrastructure/Doctrine/ContactFactory.php similarity index 55% rename from Content/Infrastructure/Doctrine/UserFactory.php rename to Content/Infrastructure/Doctrine/ContactFactory.php index 5842395b..467d33c8 100644 --- a/Content/Infrastructure/Doctrine/UserFactory.php +++ b/Content/Infrastructure/Doctrine/ContactFactory.php @@ -14,10 +14,10 @@ namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine; use Doctrine\ORM\EntityManagerInterface; -use Sulu\Bundle\ContentBundle\Content\Domain\Factory\UserFactoryInterface; -use Sulu\Component\Security\Authentication\UserInterface; +use Sulu\Bundle\ContactBundle\Entity\ContactInterface; +use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContactFactoryInterface; -class UserFactory implements UserFactoryInterface +class ContactFactory implements ContactFactoryInterface { /** * @var EntityManagerInterface @@ -29,18 +29,18 @@ public function __construct(EntityManagerInterface $entityManager) $this->entityManager = $entityManager; } - public function create(?int $userId): ?UserInterface + public function create(?int $contactId): ?ContactInterface { - if (!$userId) { + if (!$contactId) { return null; } - /** @var UserInterface|null $user */ - $user = $this->entityManager->getPartialReference( - UserInterface::class, - $userId + /** @var ContactInterface|null $contact */ + $contact = $this->entityManager->getPartialReference( + ContactInterface::class, + $contactId ); - return $user; + return $contact; } } diff --git a/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php b/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php index 1f4b5dd5..821af766 100644 --- a/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php +++ b/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php @@ -50,16 +50,26 @@ class ContentViewBuilderFactory implements ContentViewBuilderFactoryInterface */ private $securityChecker; + /** + * @var array + */ + private $settingsForms; + + /** + * @param array $settingsForms + */ public function __construct( ViewBuilderFactoryInterface $viewBuilderFactory, PreviewObjectProviderRegistryInterface $objectProviderRegistry, ContentMetadataInspectorInterface $contentMetadataInspector, - SecurityCheckerInterface $securityChecker + SecurityCheckerInterface $securityChecker, + array $settingsForms ) { $this->viewBuilderFactory = $viewBuilderFactory; $this->objectProviderRegistry = $objectProviderRegistry; $this->contentMetadataInspector = $contentMetadataInspector; $this->securityChecker = $securityChecker; + $this->settingsForms = $settingsForms; } public function getDefaultToolbarActions( @@ -286,8 +296,15 @@ private function createSettingsFormView( array $toolbarActions, string $dimensionContentClass ): ViewBuilderInterface { + $forms = []; + foreach ($this->settingsForms as $key => $tag) { + if (is_subclass_of($dimensionContentClass, $tag['instanceOf'])) { + $forms[] = $key; + } + } + return $this->createFormViewBuilder($parentView . '.settings', '/settings', $previewEnabled) - ->addMetadataRequestParameters(['class' => $dimensionContentClass]) + ->addMetadataRequestParameters(['forms' => $forms]) ->setResourceKey($resourceKey) ->setFormKey('content_settings') ->setTabTitle('sulu_page.settings') diff --git a/Content/Infrastructure/Sulu/Form/SettingsFormMetadataVisitor.php b/Content/Infrastructure/Sulu/Form/SettingsFormMetadataVisitor.php index 60af6a2e..23bb6f8a 100644 --- a/Content/Infrastructure/Sulu/Form/SettingsFormMetadataVisitor.php +++ b/Content/Infrastructure/Sulu/Form/SettingsFormMetadataVisitor.php @@ -13,49 +13,35 @@ namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Form; -use Sulu\Bundle\AdminBundle\FormMetadata\FormXmlLoader; use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata; use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadataVisitorInterface; -use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\LocalizedFormMetadataCollection; +use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\XmlFormMetadataLoader; class SettingsFormMetadataVisitor implements FormMetadataVisitorInterface { /** - * @var FormXmlLoader + * @var XmlFormMetadataLoader */ - private $formXmlLoader; + private $xmlFormMetadataLoader; - /** - * @var array - */ - private $formMappingTags; - - /** - * @param array $formMappingTags - */ - public function __construct(FormXmlLoader $formXmlLoader, array $formMappingTags) + public function __construct(XmlFormMetadataLoader $xmlFormMetadataLoader) { - $this->formXmlLoader = $formXmlLoader; - $this->formMappingTags = $formMappingTags; + $this->xmlFormMetadataLoader = $xmlFormMetadataLoader; } public function visitFormMetadata(FormMetadata $formMetadata, string $locale, array $metadataOptions = []): void { if ('content_settings' === $formMetadata->getKey()) { - usort($this->formMappingTags, static function ($a, $b) { - return $b['priority'] <=> $a['priority']; - }); - - foreach ($this->formMappingTags as $tag) { - $class = $metadataOptions['class']; - if (is_subclass_of($class, $tag['instanceOf'])) { - /** @var LocalizedFormMetadataCollection $formMetadataCollection */ - $formMetadataCollection = $this->formXmlLoader->load($tag['path']); - $settingsMetadata = $formMetadataCollection->getItems()[$locale]; - - $formMetadata->setItems(array_merge($formMetadata->getItems(), $settingsMetadata->getItems())); - $formMetadata->setSchema($formMetadata->getSchema()->merge($settingsMetadata->getSchema())); + foreach ($metadataOptions['forms'] ?? [] as $form) { + /** @var FormMetadata|null $subFormMetadata */ + $subFormMetadata = $this->xmlFormMetadataLoader->getMetadata($form, $locale, $metadataOptions); + + if (!$subFormMetadata || !($subFormMetadata instanceof FormMetadata)) { + continue; } + + $formMetadata->setItems(array_merge($formMetadata->getItems(), $subFormMetadata->getItems())); + $formMetadata->setSchema($formMetadata->getSchema()->merge($subFormMetadata->getSchema())); } } } diff --git a/DependencyInjection/Compiler/SettingsFormPass.php b/DependencyInjection/Compiler/SettingsFormPass.php index ed6ed980..3c2ec689 100644 --- a/DependencyInjection/Compiler/SettingsFormPass.php +++ b/DependencyInjection/Compiler/SettingsFormPass.php @@ -23,10 +23,10 @@ class SettingsFormPass implements CompilerPassInterface public function process(ContainerBuilder $container): void { $formDirectories = $container->getParameter('sulu_admin.forms.directories'); - $formMappingTags = []; $finder = new Finder(); $finder->files()->in($formDirectories); + $settingsForms = []; foreach ($finder as $file) { $document = new \DOMDocument(); @@ -45,19 +45,24 @@ public function process(ContainerBuilder $container): void /** @var \DOMElement $tagNode */ foreach ($tagNodes as $tagNode) { - $tag = [ - 'name' => $tagNode->getAttribute('name'), - 'instanceOf' => $tagNode->getAttribute('instanceOf'), - 'priority' => $tagNode->getAttribute('priority'), - 'path' => $file->getPathname(), - ]; + $instanceOf = $tagNode->getAttribute('instanceOf'); + $priority = $tagNode->getAttribute('priority'); - if (!empty($tag['instanceOf'])) { - $formMappingTags[] = $tag; + if (empty($instanceOf)) { + continue; } + + $settingsForms[$tagNode->getAttribute('name')] = [ + 'instanceOf' => $instanceOf, + 'priority' => $priority, + ]; } } - $container->setParameter('sulu_content.settings_mapping_tags', $formMappingTags); + uasort($settingsForms, static function ($a, $b) { + return $b['priority'] <=> $a['priority']; + }); + + $container->setParameter('sulu_content.settings_forms', $settingsForms); } } diff --git a/Resources/config/data-mapper.xml b/Resources/config/data-mapper.xml index 6d1aa3dc..87ba3465 100644 --- a/Resources/config/data-mapper.xml +++ b/Resources/config/data-mapper.xml @@ -27,7 +27,7 @@ - + diff --git a/Resources/config/form-visitor.xml b/Resources/config/form-visitor.xml index d6fa1f08..19183e5c 100644 --- a/Resources/config/form-visitor.xml +++ b/Resources/config/form-visitor.xml @@ -5,8 +5,7 @@ - - %sulu_content.settings_mapping_tags% + diff --git a/Resources/config/forms/content_settings_author.xml b/Resources/config/forms/content_settings_author.xml index 3b3fe5bd..c205090d 100644 --- a/Resources/config/forms/content_settings_author.xml +++ b/Resources/config/forms/content_settings_author.xml @@ -5,22 +5,22 @@ > content_settings_author - +
- sulu_page.editing_information + sulu_content.author - sulu_page.authored_date + sulu_content.authored_date - sulu_page.author + sulu_content.author diff --git a/Resources/config/services.xml b/Resources/config/services.xml index ec91a2ac..598b7e99 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -13,6 +13,7 @@ + %sulu_content.settings_forms% @@ -38,8 +39,8 @@ - - + + diff --git a/Resources/translations/admin.de.json b/Resources/translations/admin.de.json index 2ba4ef47..0b5da4a9 100644 --- a/Resources/translations/admin.de.json +++ b/Resources/translations/admin.de.json @@ -2,5 +2,7 @@ "sulu_content.seo": "SEO", "sulu_content.excerpt": "Auszug & Taxonomien", "sulu_content.content": "Inhalt", - "sulu_content.published": "Veröffentlicht am" + "sulu_content.published": "Veröffentlicht am", + "sulu_content.author": "Author", + "sulu_page.authored_date": "Authored Date" } diff --git a/Resources/translations/admin.en.json b/Resources/translations/admin.en.json index 25e241f7..c9f009de 100644 --- a/Resources/translations/admin.en.json +++ b/Resources/translations/admin.en.json @@ -2,5 +2,7 @@ "sulu_content.seo": "SEO", "sulu_content.excerpt": "Excerpt & Taxonomies", "sulu_content.content": "Content", - "sulu_content.published": "Published on" + "sulu_content.published": "Published on", + "sulu_content.author": "Autor", + "sulu_content.authored_date": "Verfasst am" } diff --git a/Tests/Application/.env b/Tests/Application/.env index a33dd03a..2becffdc 100644 --- a/Tests/Application/.env +++ b/Tests/Application/.env @@ -1,4 +1,9 @@ -APP_ENV=test -DATABASE_URL=mysql://root:@127.0.0.1:3306/su_content_test?serverVersion=5.7 +#APP_ENV=test +#DATABASE_URL=mysql://root:@127.0.0.1:3306/su_content_test?serverVersion=5.7 DATABASE_CHARSET=utf8mb4 DATABASE_COLLATE=utf8mb4_unicode_ci + + +APP_ENV=dev +APP_DEBUG=1 +DATABASE_URL=mysql://root:root@127.0.0.1:13306/su_content_dev?serverVersion=5.7 diff --git a/Tests/Application/.env.dev b/Tests/Application/.env.dev index 393bcccd..b9376eb9 100644 --- a/Tests/Application/.env.dev +++ b/Tests/Application/.env.dev @@ -1 +1,3 @@ -DATABASE_URL=mysql://root:@127.0.0.1:3306/su_content_test_dev?serverVersion=5.7 +APP_ENV=dev +APP_DEBUG=1 +DATABASE_URL=mysql://root:root@127.0.0.1:13306/su_content_dev?serverVersion=5.7 diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactoryTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactoryTest.php index 60ae106a..eea713b1 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactoryTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactoryTest.php @@ -58,7 +58,8 @@ protected function createContentViewBuilder( new ViewBuilderFactory(), $previewObjectProviderRegistry, $contentMetadataInspector, - $securityChecker + $securityChecker, + [] ); }