-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
SiteRepository.php
157 lines (139 loc) · 4.82 KB
/
SiteRepository.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/*
* 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.
*/
declare(strict_types=1);
namespace Neos\Neos\Domain\Repository;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\QueryInterface;
use Neos\Flow\Persistence\QueryResultInterface;
use Neos\Flow\Persistence\Repository;
use Neos\Neos\Domain\Exception as NeosException;
use Neos\Neos\Domain\Model\Site;
use Neos\Neos\Domain\Model\SiteNodeName;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;
use Neos\Neos\Domain\Service\SiteNodeUtility;
use Neos\Neos\Utility\NodeTypeWithFallbackProvider;
/**
* The Site Repository
*
* @Flow\Scope("singleton")
* @api
* @method QueryResultInterface|Site[] findByNodeName(string $nodeName)
* @method QueryResultInterface|Site[] findByState(int $state)
*/
class SiteRepository extends Repository
{
use NodeTypeWithFallbackProvider;
#[Flow\Inject]
protected ContentRepositoryRegistry $contentRepositoryRegistry;
/**
* @var array<string,string>
*/
protected $defaultOrderings = [
'name' => QueryInterface::ORDER_ASCENDING,
'nodeName' => QueryInterface::ORDER_ASCENDING
];
/**
* @Flow\InjectConfiguration(package="Neos.Neos", path="defaultSiteNodeName")
* @var string
*/
protected $defaultSiteNodeName;
/**
* Finds the first site
*
* @return Site The first site or NULL if none exists
* @api
*/
public function findFirst(): ?Site
{
/** @var ?Site $result */
$result = $this->createQuery()->execute()->getFirst();
return $result;
}
/**
* Find all sites with status "online"
*
* @return QueryResultInterface<Site>
*/
public function findOnline(): QueryResultInterface
{
return $this->findByState(Site::STATE_ONLINE);
}
/**
* Find first site with status "online"
*/
public function findFirstOnline(): ?Site
{
/** @var ?Site $site */
$site = $this->findOnline()->getFirst();
return $site;
}
public function findOneByNodeName(string|SiteNodeName $nodeName): ?Site
{
$query = $this->createQuery();
/** @var ?Site $site */
$site = $query->matching(
$query->equals('nodeName', $nodeName)
)
->execute()
->getFirst();
return $site;
}
/**
* Finds a given site by site node.
*
* To find the correct site node by its descended child node leverage `findClosestNode`:
* ```php
* $siteNode = $subgraph->findClosestNode(
* $node->nodeAggregateId,
* FindClosestNodeFilter::create(nodeTypes: NodeTypeNameFactory::NAME_SITE)
* );
* ```
*
* To resolve the SiteNode by a Site use {@see SiteNodeUtility::findSiteNodeBySite()}
*
* @throws \Neos\Neos\Domain\Exception in case the passed $siteNode is not a real site node or no site matches this site node.
*/
public function findSiteBySiteNode(Node $siteNode): Site
{
if (!$this->getNodeType($siteNode)->isOfType(NodeTypeNameFactory::NAME_SITE)) {
throw new \Neos\Neos\Domain\Exception(sprintf('Node %s is not a site node. Site nodes must be of type "%s".', $siteNode->aggregateId->value, NodeTypeNameFactory::NAME_SITE), 1697108987);
}
if ($siteNode->name === null) {
throw new \Neos\Neos\Domain\Exception(sprintf('Site node "%s" is unnamed', $siteNode->aggregateId->value), 1681286146);
}
return $this->findOneByNodeName(SiteNodeName::fromNodeName($siteNode->name))
?? throw new \Neos\Neos\Domain\Exception(sprintf('No site found for nodeNodeName "%s"', $siteNode->name->value), 1677245517);
}
/**
* Find the site that was specified in the configuration ``defaultSiteNodeName``
*
* If the defaultSiteNodeName-setting is null the first active site is returned
* If the site is not found or not active an exception is thrown
*
* @throws NeosException
*/
public function findDefault(): ?Site
{
if ($this->defaultSiteNodeName === null) {
return $this->findFirstOnline();
}
$defaultSite = $this->findOneByNodeName($this->defaultSiteNodeName);
if (!$defaultSite instanceof Site || $defaultSite->getState() !== Site::STATE_ONLINE) {
throw new NeosException(sprintf(
'DefaultSiteNode %s not found or not active',
$this->defaultSiteNodeName
), 1476374818);
}
return $defaultSite;
}
}