-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
AssetUsageInNodePropertiesStrategy.php
101 lines (89 loc) · 3.24 KB
/
AssetUsageInNodePropertiesStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace Neos\Neos\Domain\Strategy;
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\ContentRepository\Domain\Model\NodeData;
use Neos\ContentRepository\Domain\Repository\NodeDataRepository;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\Image;
use Neos\Media\Domain\Strategy\AbstractAssetUsageStrategy;
use Neos\Neos\Controller\CreateContentContextTrait;
use Neos\Neos\Domain\Model\Dto\AssetUsageInNodeProperties;
use Neos\Neos\Domain\Service\SiteService;
use Neos\Utility\TypeHandling;
/**
* @Flow\Scope("singleton")
*/
class AssetUsageInNodePropertiesStrategy extends AbstractAssetUsageStrategy
{
use CreateContentContextTrait;
/**
* @Flow\Inject
* @var NodeDataRepository
*/
protected $nodeDataRepository;
/**
* @var array
*/
protected $firstlevelCache = [];
/**
* @Flow\Inject
* @var PersistenceManagerInterface
*/
protected $persistenceManager;
/**
* Returns an array of usage reference objects.
*
* @param AssetInterface $asset
* @return array<\Neos\Neos\Domain\Model\Dto\AssetUsageInNodeProperties>
* @throws \Neos\ContentRepository\Exception\NodeConfigurationException
*/
public function getUsageReferences(AssetInterface $asset)
{
$assetIdentifier = $this->persistenceManager->getIdentifierByObject($asset);
if (isset($this->firstlevelCache[$assetIdentifier])) {
return $this->firstlevelCache[$assetIdentifier];
}
$relatedNodes = array_map(function (NodeData $relatedNodeData) use ($asset) {
return new AssetUsageInNodeProperties(
$asset,
$relatedNodeData->getIdentifier(),
$relatedNodeData->getWorkspace()->getName(),
$relatedNodeData->getDimensionValues(),
$relatedNodeData->getNodeType()->getName()
);
}, $this->getRelatedNodes($asset));
$this->firstlevelCache[$assetIdentifier] = $relatedNodes;
return $this->firstlevelCache[$assetIdentifier];
}
/**
* Returns all nodes that use the asset in a node property.
*
* @param AssetInterface $asset
* @return array
*/
public function getRelatedNodes(AssetInterface $asset)
{
$relationMap = [];
$relationMap[TypeHandling::getTypeForValue($asset)] = [$this->persistenceManager->getIdentifierByObject($asset)];
if ($asset instanceof Image) {
foreach ($asset->getVariants() as $variant) {
$type = TypeHandling::getTypeForValue($variant);
if (!isset($relationMap[$type])) {
$relationMap[$type] = [];
}
$relationMap[$type][] = $this->persistenceManager->getIdentifierByObject($variant);
}
}
return $this->nodeDataRepository->findNodesByPathPrefixAndRelatedEntities(SiteService::SITES_ROOT_PATH, $relationMap);
}
}