1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alchemy\Phraseanet; |
4
|
|
|
|
5
|
|
|
use PhraseanetSDK\Application; |
6
|
|
|
use PhraseanetSDK\AbstractRepository; |
7
|
|
|
use PhraseanetSDK\Orders\OrderRepository; |
8
|
|
|
use PhraseanetSDK\Search\SearchRepository; |
9
|
|
|
use ProxyManager\Configuration; |
10
|
|
|
use ProxyManager\Factory\LazyLoadingValueHolderFactory; |
11
|
|
|
use ProxyManager\Proxy\LazyLoadingInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
13
|
|
|
|
14
|
|
|
class EntityManagerFactory |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Application |
18
|
|
|
*/ |
19
|
|
|
private $application; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var TokenProvider |
23
|
|
|
*/ |
24
|
|
|
private $tokenProvider; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string Annotation cache directory |
28
|
|
|
*/ |
29
|
|
|
private $annotationCacheDirectory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string Proxy cache directory |
33
|
|
|
*/ |
34
|
|
|
private $proxyCacheDirectory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Application $phraseanetApplication |
38
|
|
|
* @param TokenProvider $tokenProvider |
39
|
|
|
*/ |
40
|
8 |
|
public function __construct( |
41
|
|
|
Application $phraseanetApplication, |
42
|
|
|
TokenProvider $tokenProvider |
43
|
|
|
) { |
44
|
8 |
|
$this->application = $phraseanetApplication; |
45
|
8 |
|
$this->tokenProvider = $tokenProvider; |
46
|
8 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $path |
50
|
|
|
*/ |
51
|
1 |
|
public function setAnnotationCacheDirectory($path) |
52
|
|
|
{ |
53
|
1 |
|
$this->annotationCacheDirectory = $path; |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $path |
58
|
|
|
*/ |
59
|
1 |
|
public function setProxyCacheDirectory($path) |
60
|
|
|
{ |
61
|
1 |
|
$this->proxyCacheDirectory = $path; |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return \PhraseanetSDK\EntityManager |
66
|
|
|
*/ |
67
|
8 |
|
public function getEntityManager() |
68
|
|
|
{ |
69
|
8 |
|
$token = $this->tokenProvider->getToken(); |
70
|
8 |
|
$options = $this->getOptions(); |
71
|
|
|
|
72
|
8 |
|
if ($token !== null) { |
73
|
7 |
|
return $this->application->getEntityManager($token, $options); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
throw new \RuntimeException('A user token or an application token is required.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $name |
81
|
|
|
* @return \PhraseanetSDK\AbstractRepository |
82
|
|
|
*/ |
83
|
3 |
|
public function getRepository($name) |
84
|
|
|
{ |
85
|
3 |
|
$configuration = $this->getProxyFactoryConfiguration(); |
86
|
|
|
|
87
|
3 |
|
$factory = new LazyLoadingValueHolderFactory($configuration); |
88
|
3 |
|
$initializer = function ( |
89
|
|
|
& $wrappedObject, |
90
|
|
|
LazyLoadingInterface $proxy, |
91
|
|
|
$method, |
92
|
|
|
array $parameters, |
93
|
|
|
& $initializer |
94
|
|
|
) use ($name) { |
95
|
3 |
|
$initializer = null; |
96
|
3 |
|
$wrappedObject = $this->getEntityManager()->getRepository($name); |
97
|
|
|
|
98
|
3 |
|
return true; |
99
|
3 |
|
}; |
100
|
|
|
|
101
|
3 |
|
$class = 'PhraseanetSDK\Repository\\' . ucfirst($name); |
102
|
|
|
|
103
|
3 |
|
if ($name == 'search') { |
104
|
|
|
$class = SearchRepository::class; |
105
|
|
|
} |
106
|
|
|
|
107
|
3 |
|
if ($name == 'orders') { |
108
|
|
|
$class = OrderRepository::class; |
109
|
|
|
} |
110
|
|
|
|
111
|
3 |
|
return $factory->createProxy($class, $initializer); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Builds the options array used to build SDK entity managers |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
8 |
|
private function getOptions() |
120
|
|
|
{ |
121
|
8 |
|
$options = array(); |
122
|
|
|
|
123
|
8 |
|
if ($this->proxyCacheDirectory) { |
124
|
1 |
|
$this->ensureDirectoryExists($this->proxyCacheDirectory); |
125
|
|
|
|
126
|
1 |
|
$options['proxy.path'] = $this->proxyCacheDirectory; |
127
|
|
|
} |
128
|
|
|
|
129
|
8 |
|
if ($this->annotationCacheDirectory) { |
130
|
1 |
|
$this->ensureDirectoryExists($this->annotationCacheDirectory); |
131
|
|
|
|
132
|
1 |
|
$options['annotation.path'] = $this->annotationCacheDirectory; |
133
|
|
|
} |
134
|
|
|
|
135
|
8 |
|
return $options; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Creates a directory matching requested path if it does not exist |
140
|
|
|
* |
141
|
|
|
* @param string $path |
142
|
|
|
*/ |
143
|
2 |
|
private function ensureDirectoryExists($path) |
144
|
|
|
{ |
145
|
2 |
|
if (!file_exists($path)) { |
146
|
1 |
|
mkdir($path, 0775, true); |
147
|
|
|
} |
148
|
2 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Creates a ProxyManager configuration instance |
152
|
|
|
* |
153
|
|
|
* @return Configuration |
154
|
|
|
*/ |
155
|
3 |
|
private function getProxyFactoryConfiguration() |
156
|
|
|
{ |
157
|
3 |
|
$configuration = new Configuration(); |
158
|
|
|
|
159
|
3 |
|
if ($this->proxyCacheDirectory) { |
160
|
1 |
|
$this->ensureDirectoryExists($this->proxyCacheDirectory); |
161
|
1 |
|
$configuration->setProxiesTargetDir($this->proxyCacheDirectory); |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
return $configuration; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|