/
Manager.php
161 lines (140 loc) · 4.15 KB
/
Manager.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
158
159
160
161
<?php
namespace Mongolid;
use Illuminate\Container\Container;
use Mongolid\Connection\Connection;
use Mongolid\Connection\Pool;
use Mongolid\Container\Ioc;
use Mongolid\DataMapper\DataMapper;
use Mongolid\Event\EventTriggerInterface;
use Mongolid\Event\EventTriggerService;
use Mongolid\Schema\Schema;
use Mongolid\Util\CacheComponent;
use Mongolid\Util\CacheComponentInterface;
/**
* Wraps the Mongolid initialization. The main purpose of the Manager is to make
* it easy to use without any framework.
*
* With the Mongolid\Manager, you can start using Mongolid with pure PHP by
* simply calling the addConnection method.
*
* @example
* (new Mongolid\Manager)->addConnection(new Connection);
* // And then start persisting and querying your models.
*/
class Manager
{
/**
* Singleton instance of the manager.
*
* @var Manager
*/
protected static $singleton;
/**
* Container being used by Mongolid.
*
* @var \Illuminate\Contracts\Container\Container
*/
public $container;
/**
* Mongolid connection pool being object.
*
* @var Pool
*/
public $connectionPool;
/**
* Mongolid cache component object.
*
* @var CacheComponent
*/
public $cacheComponent;
/**
* Stores the schemas that have been registered for later use. This may be
* useful when using Mongolid DataMapper pattern.
*
* @var array
*/
protected $schemas = [];
/**
* Main entry point to openning a connection and start using Mongolid in
* pure PHP. After adding a connection into the Manager you are ready to
* persist and query your models.
*
* @param Connection $connection connection instance to be used in database interactions
*
* @return bool Success
*/
public function addConnection(Connection $connection): bool
{
$this->init();
$this->connectionPool->addConnection($connection);
return true;
}
/**
* Get the raw MongoDB connection.
*
* @return \MongoDB\Client
*/
public function getConnection()
{
$this->init();
return $this->connectionPool->getConnection()->getRawConnection();
}
public function getSchemas(): array
{
return $this->schemas;
}
/**
* Sets the event trigger for Mongolid events.
*
* @param EventTriggerInterface $eventTrigger external event trigger
*/
public function setEventTrigger(EventTriggerInterface $eventTrigger)
{
$this->init();
$eventService = new EventTriggerService();
$eventService->registerEventDispatcher($eventTrigger);
$this->container->instance(EventTriggerService::class, $eventService);
}
/**
* Allow document Schemas to be registered for later use.
*
* @param Schema $schema schema being registered
*/
public function registerSchema(Schema $schema)
{
$this->schemas[$schema->entityClass] = $schema;
}
/**
* Retrieves a DataMapper for the given $entityClass. This can only be done
* if the Schema for that entity has been previously registered with
* registerSchema() method.
*
* @param string $entityClass class of the entity that needs to be mapped
*
* @return DataMapper|null dataMapper configured for the $entityClass
*/
public function getMapper(string $entityClass)
{
if (isset($this->schemas[$entityClass])) {
$dataMapper = Ioc::make(DataMapper::class);
$dataMapper->setSchema($this->schemas[$entityClass] ?? null);
return $dataMapper;
}
}
/**
* Initializes the Mongolid manager.
*/
protected function init()
{
if ($this->container) {
return;
}
$this->container = new Container();
$this->connectionPool = new Pool();
$this->cacheComponent = new CacheComponent();
$this->container->instance(Pool::class, $this->connectionPool);
$this->container->instance(CacheComponentInterface::class, $this->cacheComponent);
Ioc::setContainer($this->container);
static::$singleton = $this;
}
}