-
Notifications
You must be signed in to change notification settings - Fork 17
/
BeanFactoryConfiguration.php
185 lines (165 loc) · 5.29 KB
/
BeanFactoryConfiguration.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/*
* This file is part of the Disco package.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types = 1);
namespace bitExpert\Disco;
use bitExpert\Disco\Store\BeanStore;
use bitExpert\Disco\Store\SerializableBeanStore;
use InvalidArgumentException;
use ProxyManager\Autoloader\AutoloaderInterface;
use ProxyManager\Configuration;
use ProxyManager\FileLocator\FileLocator;
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
use ProxyManager\GeneratorStrategy\GeneratorStrategyInterface;
use RuntimeException;
/**
* BeanFactory configuration class.
*
* @api
*/
class BeanFactoryConfiguration
{
/**
* @var BeanStore
*/
protected $sessionBeanStore;
/**
* @var string
*/
protected $proxyTargetDir;
/**
* @var GeneratorStrategyInterface
*/
protected $proxyWriterGenerator;
/**
* @var AutoloaderInterface
*/
protected $proxyAutoloader;
/**
* Creates a new {@link \bitExpert\Disco\BeanFactoryConfiguration}.
*
* @param string $proxyTargetDir
* @throws InvalidArgumentException
*/
public function __construct($proxyTargetDir)
{
try {
$proxyFileLocator = new FileLocator($proxyTargetDir);
} catch (\Exception $e) {
throw new InvalidArgumentException(
sprintf(
'Proxy target directory "%s" does not exist!',
$proxyTargetDir
),
$e->getCode(),
$e
);
}
$this->setProxyTargetDir($proxyTargetDir);
$this->setSessionBeanStore(new SerializableBeanStore());
$this->setProxyWriterGenerator(new FileWriterGeneratorStrategy($proxyFileLocator));
}
/**
* Sets the {@link \bitExpert\Disco\Store\BeanStore} instance used to store the
* session-aware beans.
*
* @param BeanStore $sessionBeanStore
*/
public function setSessionBeanStore(BeanStore $sessionBeanStore)
{
$this->sessionBeanStore = $sessionBeanStore;
}
/**
* Sets the directory in which ProxyManager will store the generated proxy classes in.
*
* @param string $proxyTargetDir
* @throws InvalidArgumentException
*/
public function setProxyTargetDir(string $proxyTargetDir)
{
if (!is_dir($proxyTargetDir)) {
throw new InvalidArgumentException(
sprintf(
'Proxy target directory "%s" does not exist!',
$proxyTargetDir
)
);
}
if (!is_writable($proxyTargetDir)) {
throw new InvalidArgumentException(
sprintf(
'Proxy target directory "%s" is not writable!',
$proxyTargetDir
)
);
}
$this->proxyTargetDir = $proxyTargetDir;
}
/**
* Sets the {@link \ProxyManager\GeneratorStrategy\GeneratorStrategyInterface} which
* ProxyManager will use the generate the proxy classes.
*
* @param GeneratorStrategyInterface $writergenerator
*/
public function setProxyWriterGenerator(GeneratorStrategyInterface $writergenerator)
{
$this->proxyWriterGenerator = $writergenerator;
}
/**
* Sets the {@link \ProxyManager\Autoloader\AutoloaderInterface} that should be
* used by ProxyManager to load the generated classes.
*
* @param AutoloaderInterface $autoloader
* @throws \RuntimeException
*/
public function setProxyAutoloader(AutoloaderInterface $autoloader)
{
if ($this->proxyAutoloader instanceof AutoloaderInterface) {
if (!spl_autoload_unregister($this->proxyAutoloader)) {
throw new RuntimeException(
sprintf('Cannot unregister autoloader "%s"', get_class($this->proxyAutoloader))
);
}
}
$this->proxyAutoloader = $autoloader;
if (!spl_autoload_register($this->proxyAutoloader, false)) {
throw new RuntimeException(
sprintf('Cannot register autoloader "%s"', get_class($this->proxyAutoloader))
);
}
}
/**
* Returns the ProxyManager configuration based on the current
* {@link \bitExpert\Disco\BeanFactoryConfiguration}.
*
* @return Configuration
*/
public function getProxyManagerConfiguration() : Configuration
{
$proxyManagerConfiguration = new Configuration();
$proxyManagerConfiguration->setProxiesTargetDir($this->proxyTargetDir);
if ($this->proxyWriterGenerator instanceof GeneratorStrategyInterface) {
$proxyManagerConfiguration->setGeneratorStrategy($this->proxyWriterGenerator);
}
if ($this->proxyAutoloader instanceof AutoloaderInterface) {
$proxyManagerConfiguration->setProxyAutoloader($this->proxyAutoloader);
}
return $proxyManagerConfiguration;
}
/**
* Returns the configured {@link \bitExpert\Disco\Store\BeanStore} used to store
* the session-aware beans in.
*
* @return BeanStore
*/
public function getSessionBeanStore() : BeanStore
{
return $this->sessionBeanStore;
}
}