Skip to content

Commit

Permalink
EZP-28956: Expose lazy loaded Location->getContent() on API for bette…
Browse files Browse the repository at this point in the history
…r DX (#2328)

* Add SPI ContentHandler::loadContentList() for bulk loading content

Note on cache:
- By design here version is skipped, as use case for loading lots of content is in current version.
- So one downside for cache here is that it won't reuse cache from load() method, causing cache to be duplcated in current design.

Possible ways to remedy:
- Force having to provide versions also on loadContentList() making it less efficient for some use cases (Page builder scenarios for instance)
- Allow $version on load() to be false, or expose a loadContentInCurrentVersion() kind of method, either way adapt usage for this so cache will be shared.

* Add Location->getContent() using lazy properties

* [Integration] Add coverage for Location->getContent()

* Change SearchService->findContent() to bulk load content

Now that we have method to load several at once, take advantage in findContent().

* [ContentViewBuilder] Take advantage of Location->getContent() to avoid duplicate loading

* Refactor for LoadStruct usage in SPI

* fix review comments

* Add internal ContentInfo proxy for ContentProxy usage

* Make Trash item contain Content as well (as it extends Location)

* Fix review comments

* Don't use unset() on properties when they are no longer needed

* Fix phpdoc on Abstract(Cache)Handler
  • Loading branch information
andrerom authored and Łukasz Serwatka committed Jun 25, 2018
1 parent c8fc061 commit 63eb61c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Repository/LocationService.php
Expand Up @@ -44,10 +44,11 @@ public function copySubtree(Location $subtree, Location $targetParentLocation);
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found
*
* @param mixed $locationId
* @param string[]|null $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
*
* @return \eZ\Publish\API\Repository\Values\Content\Location
*/
public function loadLocation($locationId);
public function loadLocation($locationId, array $prioritizedLanguages = null);

/**
* Loads a location object from its $remoteId.
Expand All @@ -56,10 +57,11 @@ public function loadLocation($locationId);
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found
*
* @param string $remoteId
* @param string[]|null $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
*
* @return \eZ\Publish\API\Repository\Values\Content\Location
*/
public function loadLocationByRemoteId($remoteId);
public function loadLocationByRemoteId($remoteId, array $prioritizedLanguages = null);

/**
* Loads the locations for the given content object.
Expand All @@ -71,30 +73,33 @@ public function loadLocationByRemoteId($remoteId);
*
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
* @param \eZ\Publish\API\Repository\Values\Content\Location $rootLocation
* @param string[]|null $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
*
* @return \eZ\Publish\API\Repository\Values\Content\Location[] An array of {@link Location}
*/
public function loadLocations(ContentInfo $contentInfo, Location $rootLocation = null);
public function loadLocations(ContentInfo $contentInfo, Location $rootLocation = null, array $prioritizedLanguages = null);

/**
* Loads children which are readable by the current user of a location object sorted by sortField and sortOrder.
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
* @param int $offset the start offset for paging
* @param int $limit the number of locations returned
* @param string[]|null $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
*
* @return \eZ\Publish\API\Repository\Values\Content\LocationList
*/
public function loadLocationChildren(Location $location, $offset = 0, $limit = 25);
public function loadLocationChildren(Location $location, $offset = 0, $limit = 25, array $prioritizedLanguages = null);

/**
* Load parent Locations for Content Draft.
*
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
* @param string[]|null $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
*
* @return \eZ\Publish\API\Repository\Values\Content\Location[] List of parent Locations
*/
public function loadParentLocationsForDraftContent(VersionInfo $versionInfo);
public function loadParentLocationsForDraftContent(VersionInfo $versionInfo, array $prioritizedLanguages = null);

/**
* Returns the number of children which are readable by the current user of a location object.
Expand Down
47 changes: 47 additions & 0 deletions Repository/Tests/LocationServiceTest.php
Expand Up @@ -464,6 +464,53 @@ public function testLoadLocationStructValues(Location $location)
$location->contentInfo
);
$this->assertEquals($this->generateId('object', 4), $location->contentInfo->id);

// Check lazy loaded proxy on ->content
$this->assertInstanceOf(
Content::class,
$content = $location->getContent()
);
$this->assertEquals(4, $content->contentInfo->id);
}

public function testLoadLocationPrioritizedLanguagesFallback()
{
$repository = $this->getRepository();

// Add a language
$languageService = $repository->getContentLanguageService();
$languageStruct = $languageService->newLanguageCreateStruct();
$languageStruct->name = 'Norsk';
$languageStruct->languageCode = 'nor-NO';
$languageService->createLanguage($languageStruct);

$locationService = $repository->getLocationService();
$contentService = $repository->getContentService();
$location = $locationService->loadLocation(5);

// Translate "Users"
$draft = $contentService->createContentDraft($location->contentInfo);
$struct = $contentService->newContentUpdateStruct();
$struct->setField('name', 'Brukere', 'nor-NO');
$draft = $contentService->updateContent($draft->getVersionInfo(), $struct);
$contentService->publishVersion($draft->getVersionInfo());

// Load with prioritc language (fallback will be the old one)
$location = $locationService->loadLocation(5, ['nor-NO']);

$this->assertInstanceOf(
Location::class,
$location
);
self::assertEquals(5, $location->id);
$this->assertInstanceOf(
Content::class,
$content = $location->getContent()
);
$this->assertEquals(4, $content->contentInfo->id);

$this->assertEquals($content->getVersionInfo()->getName(), 'Brukere');
$this->assertEquals($content->getVersionInfo()->getName('eng-US'), 'Users');
}

/**
Expand Down
7 changes: 7 additions & 0 deletions Repository/Tests/TrashServiceTest.php
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\URLAliasService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location as APILocation;
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
use eZ\Publish\API\Repository\Values\Content\Query;
Expand Down Expand Up @@ -254,6 +255,12 @@ public function testLoadTrashItem()
$trashItem,
$trashItemReloaded
);

$this->assertInstanceOf(
Content::class,
$content = $trashItemReloaded->getContent()
);
$this->assertEquals($trashItem->contentId, $content->contentInfo->id);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions Repository/Values/Content/Location.php
Expand Up @@ -193,6 +193,19 @@ public function isDraft()
*/
protected $sortOrder;

/**
* @var \eZ\Publish\API\Repository\Values\Content\Content
*/
protected $content;

/**
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
public function getContent(): Content
{
return $this->content;
}

/**
* Get SortClause objects built from Locations's sort options.
*
Expand Down

0 comments on commit 63eb61c

Please sign in to comment.