From f8425739b7f594af17e49b7eb1cbad3813456ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20R?= Date: Wed, 19 Sep 2018 07:44:40 +0200 Subject: [PATCH] EZP-29613: As a developer I want access ContentType on Content to avoid re-loading it Uses https://github.com/ezsystems/ezpublish-kernel/pull/2444, and $location->getContent() (v2.2) in order avoid several repository loads not needed anymore. Here are some numbers for cache lookups on clean install, expect difference to be bigger when you have more data (demo or project): dashboard: 418 / 1240 (33.71%) _(master)_ 468 / 1315 (35.59%) _(with kernel PR, only)_* 364 / 1043 (34.9%) _(with this PR + kernel PR)_ content structure: 478 / 1401 (34.12%) _(master)_ 450 / 1359 (33.11%) _(with kernel PR, only)_ 436 / 1327 (32.86%) _(with this PR + kernel PR)_ _* So withouth this patch there is a slight regression with more cache loads with the kernel PR on dashboard. Probably due to kernel now always loading content types from API instead of falling back to using SPI, and via api language loading and mapping is needed. With this PR we get rid of the duplicated loads, and for other apps the extra lanfguage load and much more will go away once we have inMemory cache again (v2.4)._ --- .../Controller/ContentViewController.php | 5 +- src/bundle/Controller/LocationController.php | 14 ++---- src/bundle/Controller/SectionController.php | 2 +- src/bundle/Controller/TrashController.php | 2 +- ...tTranslateViewFilterParametersListener.php | 5 +- .../RequestAttributesListener.php | 13 ++--- .../Menu/ContentCreateRightSidebarBuilder.php | 2 +- .../Specification/Location/IsContainer.php | 17 +------ .../Dashboard/PagerContentToDataMapper.php | 4 +- src/lib/Tab/LocationView/RelationsTab.php | 23 ++++----- .../LocationIsContainerValidatorTest.php | 49 +++++++++---------- src/lib/UI/Config/Provider/User.php | 7 +-- src/lib/UI/Dataset/ContentDraftsDataset.php | 31 ++++++------ src/lib/UI/Dataset/PoliciesDataset.php | 4 +- src/lib/UI/Dataset/RelationsDataset.php | 10 ++-- src/lib/UI/Dataset/RolesDataset.php | 5 +- .../Subitems/ContentViewParameterSupplier.php | 16 ++---- src/lib/UI/Value/ValueFactory.php | 17 +++---- .../LocationIsContainerValidator.php | 23 ++------- .../Builder/ContentTranslateViewBuilder.php | 42 ++++++---------- .../Filter/ContentTranslateViewFilter.php | 2 +- 21 files changed, 110 insertions(+), 183 deletions(-) diff --git a/src/bundle/Controller/ContentViewController.php b/src/bundle/Controller/ContentViewController.php index 2695ac630f..492d58fbb0 100644 --- a/src/bundle/Controller/ContentViewController.php +++ b/src/bundle/Controller/ContentViewController.php @@ -208,10 +208,7 @@ private function supplyPathLocations(ContentView $view): void */ private function supplyContentType(ContentView $view): void { - $content = $view->getContent(); - $contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId, $this->siteAccessLanguages); - - $view->addParameters(['contentType' => $contentType]); + $view->addParameters(['contentType' => $view->getContent()->getContentType()]); } /** diff --git a/src/bundle/Controller/LocationController.php b/src/bundle/Controller/LocationController.php index 8808fdec31..cf43fcbb86 100644 --- a/src/bundle/Controller/LocationController.php +++ b/src/bundle/Controller/LocationController.php @@ -117,11 +117,7 @@ public function moveAction(Request $request): Response $location = $data->getLocation(); $newParentLocation = $data->getNewParentLocation(); - $newParentContentType = $this->contentTypeService->loadContentType( - $newParentLocation->getContentInfo()->contentTypeId - ); - - if (!$newParentContentType->isContainer) { + if (!$newParentLocation->getContent()->getContentType()->isContainer) { throw new InvalidArgumentException( '$newParentLocation', 'Cannot move location to a parent that is not a container' @@ -173,11 +169,7 @@ public function copyAction(Request $request): Response $location = $data->getLocation(); $newParentLocation = $data->getNewParentLocation(); - $newParentContentType = $this->contentTypeService->loadContentType( - $newParentLocation->getContentInfo()->contentTypeId - ); - - if (!$newParentContentType->isContainer) { + if (!$newParentLocation->getContent()->getContentType()->isContainer) { throw new InvalidArgumentException( '$newParentLocation', 'Cannot copy location to a parent that is not a container' @@ -283,7 +275,7 @@ public function swapAction(Request $request): Response $newLocation = $data->getNewLocation(); $childCount = $this->locationService->getLocationChildCount($currentLocation); - $contentType = $this->contentTypeService->loadContentType($newLocation->getContentInfo()->contentTypeId); + $contentType = $newLocation->getContent()->getContentType(); if (!$contentType->isContainer && $childCount) { throw new \InvalidArgumentException( diff --git a/src/bundle/Controller/SectionController.php b/src/bundle/Controller/SectionController.php index bd5e9ec365..c52ba8fb11 100644 --- a/src/bundle/Controller/SectionController.php +++ b/src/bundle/Controller/SectionController.php @@ -244,7 +244,7 @@ public function viewSectionContentAction(Section $section, int $page = 1, int $l $assignedContent[] = [ 'id' => $content->id, 'name' => $content->getName(), - 'type' => $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId)->getName(), + 'type' => $content->getContentType()->getName(), 'path' => $this->pathService->loadPathLocations( $this->locationService->loadLocation($content->contentInfo->mainLocationId) ), diff --git a/src/bundle/Controller/TrashController.php b/src/bundle/Controller/TrashController.php index 42ce49fa2a..ab2d5b1b30 100644 --- a/src/bundle/Controller/TrashController.php +++ b/src/bundle/Controller/TrashController.php @@ -145,7 +145,7 @@ public function listAction(Request $request): Response /** @var \eZ\Publish\API\Repository\Values\Content\TrashItem $item */ foreach ($pagerfanta->getCurrentPageResults() as $item) { - $contentType = $this->contentTypeService->loadContentType($item->contentInfo->contentTypeId); + $contentType = $item->getContent()->getContentType(); $ancestors = $this->uiPathService->loadPathLocations($item); $trashItemsList[] = new TrashItemData($item, $contentType, $ancestors); diff --git a/src/lib/EventListener/ContentTranslateViewFilterParametersListener.php b/src/lib/EventListener/ContentTranslateViewFilterParametersListener.php index 9cc983364c..5a738aae11 100644 --- a/src/lib/EventListener/ContentTranslateViewFilterParametersListener.php +++ b/src/lib/EventListener/ContentTranslateViewFilterParametersListener.php @@ -53,9 +53,8 @@ public function onFilterViewParameters(FilterViewParametersEvent $event) } $contentInfo = $view->getContent()->contentInfo; - $contentType = $this->contentTypeService->loadContentType( - $contentInfo->contentTypeId - ); + $contentType = $view->getContent()->getContentType(); + $event->getParameterBag()->add([ 'form' => $view->getFormView(), 'location' => $view->getLocation(), diff --git a/src/lib/EventListener/RequestAttributesListener.php b/src/lib/EventListener/RequestAttributesListener.php index 85cbe5899c..56777c69c3 100644 --- a/src/lib/EventListener/RequestAttributesListener.php +++ b/src/lib/EventListener/RequestAttributesListener.php @@ -63,7 +63,7 @@ public function addRequestAttributes(FilterViewBuilderParametersEvent $event) $parameterBag = $event->getParameters(); if ($parameterBag->has('locationId') && $request->get('_route') === '_ezpublishLocation') { - $location = $this->loadLocation($parameterBag->get('locationId')); + $location = $this->loadLocation($parameterBag->get('locationId'), $parameterBag->get('languageCode')); $parameterBag->remove('locationId'); $parameterBag->set('location', $location); } @@ -72,10 +72,7 @@ public function addRequestAttributes(FilterViewBuilderParametersEvent $event) /** @var Location $location */ $location = $parameterBag->get('location'); - $languageCode = $parameterBag->get('languageCode') ?? $location->contentInfo->mainLanguageCode; - - $content = $this->loadContent($location->contentInfo->id, $languageCode); - $parameterBag->set('content', $content); + $parameterBag->set('content', $location->getContent()); } } @@ -95,11 +92,11 @@ private function hasContentLanguage(Request $request, ParameterBag $parameterBag * * @return Location */ - private function loadLocation($locationId): Location + private function loadLocation($locationId, ?string $languageCode): Location { $location = $this->repository->sudo( - function (Repository $repository) use ($locationId) { - return $repository->getLocationService()->loadLocation($locationId); + function (Repository $repository) use ($locationId, $languageCode) { + return $repository->getLocationService()->loadLocation($locationId, $languageCode ? [$languageCode] : null); } ); diff --git a/src/lib/Menu/ContentCreateRightSidebarBuilder.php b/src/lib/Menu/ContentCreateRightSidebarBuilder.php index 67b72c1305..9d5cb4a1a0 100644 --- a/src/lib/Menu/ContentCreateRightSidebarBuilder.php +++ b/src/lib/Menu/ContentCreateRightSidebarBuilder.php @@ -91,7 +91,7 @@ public function createStructure(array $options): ItemInterface $parentLocation = $options['parentLocation']; /** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */ $contentType = $options['content_type']; - $parentContentType = $this->contentTypeService->loadContentType($parentLocation->contentInfo->contentTypeId); + $parentContentType = $parentLocation->getContent()->getContentType(); /** @var \eZ\Publish\API\Repository\Values\Content\Language $language */ $language = $options['language']; /** @var \Knp\Menu\ItemInterface|\Knp\Menu\ItemInterface[] $menu */ diff --git a/src/lib/Specification/Location/IsContainer.php b/src/lib/Specification/Location/IsContainer.php index 6b3371d3b6..e4d82c9925 100644 --- a/src/lib/Specification/Location/IsContainer.php +++ b/src/lib/Specification/Location/IsContainer.php @@ -12,28 +12,13 @@ class IsContainer extends AbstractSpecification { - /** @var \eZ\Publish\API\Repository\ContentTypeService */ - private $contentTypeService; - - /** - * @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService - */ - public function __construct($contentTypeService) - { - $this->contentTypeService = $contentTypeService; - } - /** * @param \eZ\Publish\API\Repository\Values\Content\Location $item * * @return bool - * - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException */ public function isSatisfiedBy($item): bool { - return $this->contentTypeService->loadContentType( - $item->getContentInfo()->contentTypeId - )->isContainer; + return $item->getContent()->getContentType()->isContainer; } } diff --git a/src/lib/Tab/Dashboard/PagerContentToDataMapper.php b/src/lib/Tab/Dashboard/PagerContentToDataMapper.php index 1ada6d3727..c0b0d5ae03 100644 --- a/src/lib/Tab/Dashboard/PagerContentToDataMapper.php +++ b/src/lib/Tab/Dashboard/PagerContentToDataMapper.php @@ -51,7 +51,7 @@ public function map(Pagerfanta $pager): array $data = []; foreach ($pager as $content) { - $contentInfo = $this->contentService->loadContentInfo($content->id); + $contentInfo = $content->getVersionInfo()->getContentInfo(); $contributor = (new UserExists($this->userService))->isSatisfiedBy($contentInfo->ownerId) ? $this->userService->loadUser($contentInfo->ownerId) : null; @@ -62,7 +62,7 @@ public function map(Pagerfanta $pager): array 'language' => $contentInfo->mainLanguageCode, 'contributor' => $contributor, 'version' => $content->versionInfo->versionNo, - 'type' => $this->contentTypeService->loadContentType($contentInfo->contentTypeId)->getName(), + 'type' => $content->getContentType()->getName(), 'modified' => $content->versionInfo->modificationDate, 'initialLanguageCode' => $content->versionInfo->initialLanguageCode, 'content_is_user' => (new ContentIsUser($this->userService))->isSatisfiedBy($content), diff --git a/src/lib/Tab/LocationView/RelationsTab.php b/src/lib/Tab/LocationView/RelationsTab.php index f7a80fbc8a..2abec966dc 100644 --- a/src/lib/Tab/LocationView/RelationsTab.php +++ b/src/lib/Tab/LocationView/RelationsTab.php @@ -106,22 +106,17 @@ public function renderView(array $parameters): string { /** @var Content $content */ $content = $parameters['content']; - $versionInfo = $content->getVersionInfo(); $relationsDataset = $this->datasetFactory->relations(); - $relationsDataset->load($versionInfo); + $relationsDataset->load($content); - $contentTypes = []; + $contentTypeIds = []; $relations = $relationsDataset->getRelations(); $viewParameters = []; foreach ($relations as $relation) { - $contentTypeId = $relation->getDestinationContentInfo()->contentTypeId; - - if (!isset($contentTypes[$contentTypeId])) { - $contentTypes[$contentTypeId] = $this->contentTypeService->loadContentType($contentTypeId); - } + $contentTypeIds[] = $relation->getDestinationContentInfo()->contentTypeId; } $viewParameters['relations'] = $relations; @@ -130,17 +125,17 @@ public function renderView(array $parameters): string $reverseRelations = $relationsDataset->getReverseRelations(); foreach ($reverseRelations as $relation) { - $contentTypeId = $relation->getSourceContentInfo()->contentTypeId; - - if (!isset($contentTypes[$contentTypeId])) { - $contentTypes[$contentTypeId] = $this->contentTypeService->loadContentType($contentTypeId); - } + $contentTypeIds[] = $relation->getSourceContentInfo()->contentTypeId; } $viewParameters['reverse_relations'] = $reverseRelations; } - $viewParameters['contentTypes'] = $contentTypes; + if (!empty($contentTypeIds)) { + $viewParameters['contentTypes'] = $this->contentTypeService->loadContentTypeList(array_unique($contentTypeIds)); + } else { + $viewParameters['contentTypes'] = []; + } return $this->twig->render( '@ezdesign/content/tab/relations/tab.html.twig', diff --git a/src/lib/Tests/Validator/Constraint/LocationIsContainerValidatorTest.php b/src/lib/Tests/Validator/Constraint/LocationIsContainerValidatorTest.php index 12af4a2588..13e3a19bc3 100644 --- a/src/lib/Tests/Validator/Constraint/LocationIsContainerValidatorTest.php +++ b/src/lib/Tests/Validator/Constraint/LocationIsContainerValidatorTest.php @@ -8,8 +8,7 @@ namespace EzSystems\EzPlatformAdminUi\Tests\Validator\Constraint; -use eZ\Publish\API\Repository\ContentTypeService; -use eZ\Publish\API\Repository\Values\Content\ContentInfo; +use eZ\Publish\API\Repository\Values\Content\Content; use eZ\Publish\API\Repository\Values\Content\Location; use eZ\Publish\API\Repository\Values\ContentType\ContentType; use EzSystems\EzPlatformAdminUi\Validator\Constraints\LocationIsContainer; @@ -19,9 +18,6 @@ class LocationIsContainerValidatorTest extends TestCase { - /** @var \eZ\Publish\API\Repository\ContentTypeService|\PHPUnit\Framework\MockObject\MockObject */ - private $contentTypeService; - /** @var \Symfony\Component\Validator\Context\ExecutionContextInterface */ private $executionContext; @@ -31,29 +27,35 @@ class LocationIsContainerValidatorTest extends TestCase /** @var \eZ\Publish\API\Repository\Values\Content\Location|\PHPUnit\Framework\MockObject\MockObject */ private $location; + /** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType|\PHPUnit\Framework\MockObject\MockObject */ + private $contentType; + protected function setUp() { - $this->contentTypeService = $this->createMock(ContentTypeService::class); $this->executionContext = $this->createMock(ExecutionContextInterface::class); - $this->validator = new LocationIsContainerValidator($this->contentTypeService); + $this->validator = new LocationIsContainerValidator(); $this->validator->initialize($this->executionContext); + + $content = $this->createMock(Content::class); + $this->location = $this->createMock(Location::class); $this->location - ->method('getContentInfo') - ->willReturn( - $this->createMock(ContentInfo::class) - ); + ->method('getContent') + ->willReturn($content); + + $this->contentType = $this->createMock(ContentType::class); + + $content + ->method('getContentType') + ->willReturn($this->contentType); } public function testValid() { - $contentType = $this - ->getMockBuilder(ContentType::class) - ->setMethodsExcept(['__get']) - ->setConstructorArgs([['isContainer' => true]]) - ->getMock(); - - $this->contentTypeService->method('loadContentType')->willReturn($contentType); + $this->contentType + ->method('__get') + ->with('isContainer') + ->willReturn(true); $this->executionContext ->expects($this->never()) @@ -64,13 +66,10 @@ public function testValid() public function testInvalid() { - $contentType = $this - ->getMockBuilder(ContentType::class) - ->setMethodsExcept(['__get']) - ->setConstructorArgs([['isContainer' => false]]) - ->getMock(); - - $this->contentTypeService->method('loadContentType')->willReturn($contentType); + $this->contentType + ->method('__get') + ->with('isContainer') + ->willReturn(false); $this->executionContext ->expects($this->once()) diff --git a/src/lib/UI/Config/Provider/User.php b/src/lib/UI/Config/Provider/User.php index 6a7e47e6d0..b88f883b33 100644 --- a/src/lib/UI/Config/Provider/User.php +++ b/src/lib/UI/Config/Provider/User.php @@ -70,12 +70,7 @@ public function getConfig(): array */ private function resolveProfilePictureField(ApiUser $user): ?Field { - try { - $contentType = $this->contentTypeService->loadContentType($user->contentInfo->contentTypeId); - } catch (\Exception $e) { - return null; - } - + $contentType = $user->getContentType(); foreach ($user->getFields() as $field) { $fieldDef = $contentType->getFieldDefinition($field->fieldDefIdentifier); diff --git a/src/lib/UI/Dataset/ContentDraftsDataset.php b/src/lib/UI/Dataset/ContentDraftsDataset.php index 5abff95696..d71bfa522e 100644 --- a/src/lib/UI/Dataset/ContentDraftsDataset.php +++ b/src/lib/UI/Dataset/ContentDraftsDataset.php @@ -13,6 +13,7 @@ use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; use eZ\Publish\API\Repository\LocationService; use eZ\Publish\API\Repository\Values\Content\VersionInfo; +use eZ\Publish\API\Repository\Values\ContentType\ContentType; use eZ\Publish\API\Repository\Values\User\User; use EzSystems\EzPlatformAdminUi\UI\Value\Content\VersionId; @@ -60,16 +61,18 @@ public function load(User $user = null): self } $contentDrafts = array_filter($contentDrafts, function (VersionInfo $version) { - $contentInfo = $version->getContentInfo(); + // Filter out content that has been sent to trash + return !$version->getContentInfo()->isTrashed(); + }); - if (null === $contentInfo->mainLocationId) { - $locations = $this->locationService->loadParentLocationsForDraftContent($version); - // empty locations here means Location has been trashed and Draft should be ignored - return !empty($locations); - } + $contentTypes = $contentTypeIds = []; + foreach ($contentDrafts as $contentDraft) { + $contentTypeIds[] = $contentDraft->getContentInfo()->contentTypeId; + } - return true; - }); + if (!empty($contentTypeIds)) { + $contentTypes = $this->contentTypeService->loadContentTypeList(array_unique($contentTypeIds)); + } // ContentService::loadContentDrafts returns unsorted list of VersionInfo. // Sort results by modification date, descending. @@ -78,8 +81,11 @@ public function load(User $user = null): self }); $this->data = array_map( - function (VersionInfo $versionInfo) { - return $this->mapContentDraft($versionInfo); + function (VersionInfo $versionInfo) use ($contentTypes) { + return $this->mapContentDraft( + $versionInfo, + $contentTypes[$versionInfo->getContentInfo()->contentTypeId] + ); }, $contentDrafts ); @@ -102,12 +108,9 @@ public function getContentDrafts(): array * * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException */ - private function mapContentDraft(VersionInfo $draft): array + private function mapContentDraft(VersionInfo $draft, ContentType $contentType): array { $contentInfo = $draft->getContentInfo(); - $contentType = $this->contentTypeService->loadContentType( - $contentInfo->contentTypeId - ); return [ 'id' => new VersionId( diff --git a/src/lib/UI/Dataset/PoliciesDataset.php b/src/lib/UI/Dataset/PoliciesDataset.php index 97c3f91e97..32d285aff7 100644 --- a/src/lib/UI/Dataset/PoliciesDataset.php +++ b/src/lib/UI/Dataset/PoliciesDataset.php @@ -84,8 +84,8 @@ public function __construct( public function load(Location $location): self { $roleAssignments = []; - $content = $this->contentService->loadContentByContentInfo($location->contentInfo); - $contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId); + $content = $location->getContent(); + $contentType = $content->getContentType(); if ((new ContentTypeIsUser($this->userContentTypeIdentifier))->isSatisfiedBy($contentType)) { $user = $this->userService->loadUser($content->id); diff --git a/src/lib/UI/Dataset/RelationsDataset.php b/src/lib/UI/Dataset/RelationsDataset.php index d9d3cd36ba..82ee07503c 100644 --- a/src/lib/UI/Dataset/RelationsDataset.php +++ b/src/lib/UI/Dataset/RelationsDataset.php @@ -10,7 +10,7 @@ use eZ\Publish\API\Repository\ContentService; use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; -use eZ\Publish\API\Repository\Values\Content\VersionInfo; +use eZ\Publish\API\Repository\Values\Content\Content; use EzSystems\EzPlatformAdminUi\UI\Value\ValueFactory; use EzSystems\EzPlatformAdminUi\UI\Value as UIValue; @@ -47,16 +47,16 @@ public function __construct(ContentService $contentService, ValueFactory $valueF * * @throws UnauthorizedException */ - public function load(VersionInfo $versionInfo): self + public function load(Content $content): self { - $contentInfo = $versionInfo->getContentInfo(); + $versionInfo = $content->getVersionInfo(); foreach ($this->contentService->loadRelations($versionInfo) as $relation) { - $this->relations[] = $this->valueFactory->createRelation($relation, $contentInfo); + $this->relations[] = $this->valueFactory->createRelation($relation, $content); } foreach ($this->contentService->loadReverseRelations($versionInfo->getContentInfo()) as $reverseRelation) { - $this->reverseRelations[] = $this->valueFactory->createRelation($reverseRelation, $contentInfo); + $this->reverseRelations[] = $this->valueFactory->createRelation($reverseRelation, $content); } return $this; diff --git a/src/lib/UI/Dataset/RolesDataset.php b/src/lib/UI/Dataset/RolesDataset.php index 512dce5c46..220d812682 100644 --- a/src/lib/UI/Dataset/RolesDataset.php +++ b/src/lib/UI/Dataset/RolesDataset.php @@ -83,9 +83,10 @@ public function __construct( public function load(Location $location): self { $roleAssignment = []; - $content = $this->contentService->loadContentByContentInfo($location->contentInfo); - $contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId); + $content = $location->getContent(); + $contentType = $content->getContentType(); + // @todo $content should just have been instance of User or UserGroup direclty so we don't need to re-load data if ((new ContentTypeIsUser($this->userContentTypeIdentifier))->isSatisfiedBy($contentType)) { $user = $this->userService->loadUser($content->id); $roleAssignment = $this->roleService->getRoleAssignmentsForUser($user, true); diff --git a/src/lib/UI/Module/Subitems/ContentViewParameterSupplier.php b/src/lib/UI/Module/Subitems/ContentViewParameterSupplier.php index 02ebea2ac4..84e9a59e95 100644 --- a/src/lib/UI/Module/Subitems/ContentViewParameterSupplier.php +++ b/src/lib/UI/Module/Subitems/ContentViewParameterSupplier.php @@ -11,7 +11,6 @@ use eZ\Publish\API\Repository\ContentService; use eZ\Publish\API\Repository\ContentTypeService; use eZ\Publish\API\Repository\LocationService; -use eZ\Publish\API\Repository\Values\Content\ContentInfo; use eZ\Publish\API\Repository\Values\Content\Location; use eZ\Publish\API\Repository\Values\ContentType\ContentType; use eZ\Publish\Core\MVC\Symfony\View\ContentView; @@ -108,14 +107,13 @@ public function supply(ContentView $view) $locationChildren = $this->locationService->loadLocationChildren($location, 0, $this->subitemsLimit); foreach ($locationChildren->locations as $locationChild) { - $contentInfo = $locationChild->getContentInfo(); - $contentType = $this->contentTypeService->loadContentType($contentInfo->contentTypeId); + $contentType = $locationChild->getContent()->getContentType(); if (!isset($contentTypes[$contentType->identifier])) { $contentTypes[$contentType->identifier] = $contentType; } - $subitemsRows[] = $this->createSubitemsRow($locationChild, $contentInfo, $contentType); + $subitemsRows[] = $this->createSubitemsRow($locationChild, $contentType); } $subitemsList = new SubitemsList($subitemsRows, $childrenCount); @@ -135,7 +133,6 @@ public function supply(ContentView $view) } /** - * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo * @param \eZ\Publish\API\Repository\Values\Content\Location $location * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType * @@ -145,14 +142,13 @@ public function supply(ContentView $view) * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ private function createRestContent( - ContentInfo $contentInfo, Location $location, ContentType $contentType ): RestContent { return new RestContent( - $contentInfo, + $location->getContentInfo(), $location, - $this->contentService->loadContentByContentInfo($contentInfo), + $location->getContent(), $contentType, [] ); @@ -173,7 +169,6 @@ private function createRestLocation(Location $location): RestLocation /** * @param \eZ\Publish\API\Repository\Values\Content\Location $location - * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType * * @return \EzSystems\EzPlatformAdminUi\UI\Module\Subitems\Values\SubitemsRow @@ -183,11 +178,10 @@ private function createRestLocation(Location $location): RestLocation */ private function createSubitemsRow( Location $location, - ContentInfo $contentInfo, ContentType $contentType ): SubitemsRow { $restLocation = $this->createRestLocation($location); - $restContent = $this->createRestContent($contentInfo, $location, $contentType); + $restContent = $this->createRestContent($location, $contentType); return new SubitemsRow($restLocation, $restContent); } diff --git a/src/lib/UI/Value/ValueFactory.php b/src/lib/UI/Value/ValueFactory.php index cfeb89c57b..42aa770785 100644 --- a/src/lib/UI/Value/ValueFactory.php +++ b/src/lib/UI/Value/ValueFactory.php @@ -15,6 +15,7 @@ use eZ\Publish\API\Repository\PermissionResolver; use eZ\Publish\API\Repository\SearchService; use eZ\Publish\API\Repository\UserService; +use eZ\Publish\API\Repository\Values\Content\Content; use eZ\Publish\API\Repository\Values\Content\ContentInfo; use eZ\Publish\API\Repository\Values\Content\Language; use eZ\Publish\API\Repository\Values\Content\Location; @@ -136,23 +137,23 @@ public function createLanguage(Language $language, VersionInfo $versionInfo): UI /** * @param Relation $relation - * @param ContentInfo $contentInfo + * @param Content $content * * @return UIValue\Content\Relation * * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ - public function createRelation(Relation $relation, ContentInfo $contentInfo): UIValue\Content\Relation + public function createRelation(Relation $relation, Content $content): UIValue\Content\Relation { - $contentType = $this->contentTypeService->loadContentType($contentInfo->contentTypeId); + $contentType = $content->getContentType(); $fieldDefinition = $contentType->getFieldDefinition($relation->sourceFieldDefinitionIdentifier); return new UIValue\Content\Relation($relation, [ 'relationFieldDefinitionName' => $fieldDefinition ? $fieldDefinition->getName() : '', 'relationContentTypeName' => $contentType->getName(), - 'relationLocation' => $this->locationService->loadLocation($contentInfo->mainLocationId), - 'relationName' => $contentInfo->name, + 'relationLocation' => $this->locationService->loadLocation($content->contentInfo->mainLocationId), + 'relationName' => $content->getName(), ]); } @@ -248,10 +249,8 @@ public function createBookmark(Location $location): UIValue\Location\Bookmark return new UIValue\Location\Bookmark( $location, [ - 'contentType' => $this->contentTypeService->loadContentType($location->contentInfo->contentTypeId), - 'pathLocations' => $this->pathService->loadPathLocations( - $this->locationService->loadLocation($location->contentInfo->mainLocationId) - ), + 'contentType' => $location->getContent()->getContentType(), + 'pathLocations' => $this->pathService->loadPathLocations($location), 'userCanEdit' => $this->permissionResolver->canUser('content', 'edit', $location->contentInfo), ] ); diff --git a/src/lib/Validator/Constraints/LocationIsContainerValidator.php b/src/lib/Validator/Constraints/LocationIsContainerValidator.php index 626cade7f8..b965fce237 100644 --- a/src/lib/Validator/Constraints/LocationIsContainerValidator.php +++ b/src/lib/Validator/Constraints/LocationIsContainerValidator.php @@ -8,25 +8,12 @@ namespace EzSystems\EzPlatformAdminUi\Validator\Constraints; -use eZ\Publish\API\Repository\ContentTypeService; -use eZ\Publish\API\Repository\Exceptions\NotFoundException; use EzSystems\EzPlatformAdminUi\Specification\Location\IsContainer; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; class LocationIsContainerValidator extends ConstraintValidator { - /** @var \EzSystems\EzPlatformAdminUi\Service\ContentTypeService */ - private $contentTypeService; - - /** - * @param \EzSystems\EzPlatformAdminUi\Service\ContentTypeService $contentTypeService - */ - public function __construct(ContentTypeService $contentTypeService) - { - $this->contentTypeService = $contentTypeService; - } - /** * Checks if the passed value is valid. * @@ -41,13 +28,9 @@ public function validate($location, Constraint $constraint) return; } - $isContainer = new IsContainer($this->contentTypeService); - try { - if (!$isContainer->isSatisfiedBy($location)) { - $this->context->addViolation($constraint->message); - } - } catch (NotFoundException $e) { - $this->context->addViolation($e->getMessage()); + $isContainer = new IsContainer(); + if (!$isContainer->isSatisfiedBy($location)) { + $this->context->addViolation($constraint->message); } } } diff --git a/src/lib/View/Builder/ContentTranslateViewBuilder.php b/src/lib/View/Builder/ContentTranslateViewBuilder.php index a7e3e2e159..73d962e530 100644 --- a/src/lib/View/Builder/ContentTranslateViewBuilder.php +++ b/src/lib/View/Builder/ContentTranslateViewBuilder.php @@ -12,7 +12,6 @@ use eZ\Publish\API\Repository\Values\Content\Content; use eZ\Publish\API\Repository\Values\Content\Language; use eZ\Publish\API\Repository\Values\Content\Location; -use eZ\Publish\API\Repository\Values\ContentType\ContentType; use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; use eZ\Publish\Core\MVC\Symfony\View\Builder\ViewBuilder; use eZ\Publish\Core\MVC\Symfony\View\Configurator; @@ -79,10 +78,10 @@ public function buildView(array $parameters) $fromLanguage = $this->resolveFromLanguage($parameters); $toLanguage = $this->resolveToLanguage($parameters); - $location = $this->resolveLocation($parameters); + $location = $this->resolveLocation($parameters, $fromLanguage); $content = $this->resolveContent($parameters, $location, $fromLanguage); $contentInfo = $content->contentInfo; - $contentType = $this->loadContentType($contentInfo->contentTypeId); + $contentType = $content->getContentType(); /** @var \Symfony\Component\Form\FormInterface $form */ $form = $parameters['form']; @@ -141,15 +140,16 @@ private function loadContent(int $contentId, array $languages = [], int $version * Loads a visible Location. * * @param int $locationId + * @param array|null $languages * * @return \eZ\Publish\API\Repository\Values\Content\Location * * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException */ - private function loadLocation(int $locationId): Location + private function loadLocation(int $locationId, array $languages = null): Location { - return $this->repository->getLocationService()->loadLocation($locationId); + return $this->repository->getLocationService()->loadLocation($locationId, $languages); } /** @@ -166,20 +166,6 @@ private function loadLanguage(string $languageCode): Language return $this->repository->getContentLanguageService()->loadLanguage($languageCode); } - /** - * Loads ContentType with id $contentTypeId. - * - * @param int $contentTypeId - * - * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType - * - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - */ - private function loadContentType(int $contentTypeId): ContentType - { - return $this->repository->getContentTypeService()->loadContentType($contentTypeId); - } - /** * @param array $parameters * @@ -241,13 +227,11 @@ private function resolveContent(array $parameters, ?Location $location, ?Languag { if (isset($parameters['content'])) { return $parameters['content']; + } elseif (null !== $location) { + return $location->getContent(); } - if (isset($parameters['contentId'])) { - $contentId = $parameters['contentId']; - } elseif (null !== $location) { - $contentId = $location->contentId; - } else { + if (!isset($parameters['contentId'])) { throw new InvalidArgumentException( 'Content', 'No content could be loaded from parameters' @@ -255,27 +239,31 @@ private function resolveContent(array $parameters, ?Location $location, ?Languag } return $this->loadContent( - (int) $contentId, + (int) $parameters['contentId'], null !== $language ? [$language->languageCode] : [] ); } /** * @param array $parameters + * @param \eZ\Publish\API\Repository\Values\Content\Language|null $language * * @return \eZ\Publish\API\Repository\Values\Content\Location|null * * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException */ - private function resolveLocation(array $parameters): ?Location + private function resolveLocation(array $parameters, ?Language $language): ?Location { if (isset($parameters['location'])) { return $parameters['location']; } if (isset($parameters['locationId'])) { - return $this->loadLocation((int) $parameters['locationId']); + return $this->loadLocation( + (int) $parameters['locationId'], + null !== $language ? [$language->languageCode] : null + ); } return null; diff --git a/src/lib/View/Filter/ContentTranslateViewFilter.php b/src/lib/View/Filter/ContentTranslateViewFilter.php index 8f22cba15b..d4365158ce 100644 --- a/src/lib/View/Filter/ContentTranslateViewFilter.php +++ b/src/lib/View/Filter/ContentTranslateViewFilter.php @@ -92,7 +92,7 @@ public function handleContentTranslateForm(FilterViewBuilderParametersEvent $eve $request->attributes->get('contentId'), null !== $baseLanguageCode ? [$baseLanguageCode] : null ); - $contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId); + $contentType = $content->getContentType(); $toLanguage = $this->languageService->loadLanguage($languageCode); $fromLanguage = $baseLanguageCode ? $this->languageService->loadLanguage($baseLanguageCode) : null;