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