/
ObjectManager.php
148 lines (132 loc) · 4.26 KB
/
ObjectManager.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
<?php
namespace Concrete\Core\Express;
use Concrete\Core\Application\Application;
use Concrete\Core\Entity\Express\Entity;
use Concrete\Core\Entity\Express\Entry;
use Concrete\Core\Entity\Package;
use Concrete\Core\Utility\Service\Validation\Numbers;
use Doctrine\ORM\EntityManagerInterface;
use Concrete\Core\Express\Entry\Manager as EntryManager;
use Concrete\Core\Express\Controller\Manager as ControllerManager;
use Symfony\Component\HttpFoundation\Request;
class ObjectManager
{
protected $entityManager;
protected $app;
public function __construct(Application $app, EntityManagerInterface $entityManager)
{
$this->app = $app;
$this->entityManager = $entityManager;
}
public function getEntities($asObject = false)
{
$r = $this->entityManager
->getRepository('Concrete\Core\Entity\Express\Entity');
if ($asObject) {
return $r;
} else {
return $r->findBy(['include_in_public_list' => true, 'is_published' => true]);
}
}
public function getList($entityHandle, $asObject = false)
{
$entity = $this->entityManager
->getRepository('Concrete\Core\Entity\Express\Entity')
->findOneByHandle($entityHandle);
$list = new EntryList($entity);
if ($asObject) {
return $list;
} else {
return $list->getResults();
}
}
public function refresh($object)
{
$this->entityManager->refresh($object);
return $object;
}
public function buildObject($handle, $plural_handle, $name, Package $pkg = null)
{
$builder = $this->app->make(ObjectBuilder::class);
$builder->createObject($name);
$builder->setHandle($handle);
$builder->setPluralHandle($plural_handle);
if ($pkg) {
$builder->setPackage($pkg);
}
return $builder;
}
public function buildEntry($entity)
{
$entity = is_string($entity) ? $this->getObjectByHandle($entity) : $entity;
if ($entity instanceof ObjectBuilder) {
$entity = $entity->getEntity();
}
$builder = $this->app->make(EntryBuilder::class);
$builder->createEntry($entity);
return $builder;
}
/**
* Entry ID may be the integer ID or the public identifier
* @param int|string $entryID
* @return object
*/
public function getEntry($entryID)
{
$numberValidator = $this->app->make(Numbers::class);
$r = $this->entityManager->getRepository('Concrete\Core\Entity\Express\Entry');
if ($numberValidator->integer($entryID)) {
return $r->findOneBy(['exEntryID' => $entryID]);
} else {
return $this->getEntryByPublicIdentifier($entryID);
}
}
/**
* @param $publicIdentifier
* @return object
*/
public function getEntryByPublicIdentifier($publicIdentifier)
{
$r = $this->entityManager->getRepository('Concrete\Core\Entity\Express\Entry');
return $r->findOneBy(['publicIdentifier' => $publicIdentifier]);
}
/**
* @param int|Entry $entry
*/
public function deleteEntry($entry)
{
if (!$entry instanceof Entry) {
$entry = $this->getEntry($entry);
}
if ($entry) {
/**
* @var $entry Entry
*/
$entity = $entry->getEntity();
if ($entity) {
$request = Request::createFromGlobals();
$controller = $this->getEntityController($entity);
$manager = $controller->getEntryManager($request);
$manager->deleteEntry($entry);
}
}
}
public function getObjectByID($entityID)
{
return $this->entityManager
->getRepository('Concrete\Core\Entity\Express\Entity')
->findOneById($entityID);
}
public function getObjectByHandle($entityHandle)
{
return $this->entityManager
->getRepository('Concrete\Core\Entity\Express\Entity')
->findOneByHandle($entityHandle);
}
public function getEntityController(Entity $entity)
{
return $this->app->make(ControllerManager::class)->driver(
$entity->getHandle()
);
}
}