|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Fwk |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2011-2015, Julien Ballestracci <[email protected]>. |
|
6
|
|
|
* All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
12
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
13
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
14
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
15
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
16
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
17
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
18
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
19
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
20
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
21
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
22
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
23
|
|
|
* |
|
24
|
|
|
* PHP Version 5.3 |
|
25
|
|
|
* |
|
26
|
|
|
* @category DependencyInjection |
|
27
|
|
|
* @package Fwk\Di |
|
28
|
|
|
* @author Julien Ballestracci <[email protected]> |
|
29
|
|
|
* @copyright 2011-2015 Julien Ballestracci <[email protected]> |
|
30
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
31
|
|
|
* @link http://www.nitronet.org/fwk |
|
32
|
|
|
*/ |
|
33
|
|
|
namespace Fwk\Di\Definitions; |
|
34
|
|
|
|
|
35
|
|
|
use Fwk\Di\Container; |
|
36
|
|
|
use ProxyManager\Configuration; |
|
37
|
|
|
use ProxyManager\Factory\LazyLoadingValueHolderFactory; |
|
38
|
|
|
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy; |
|
39
|
|
|
use ProxyManager\Proxy\LazyLoadingInterface; |
|
40
|
|
|
use ProxyManager\Proxy\VirtualProxyInterface; |
|
41
|
|
|
use Fwk\Di\Exceptions; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* LazyClassDefinition |
|
45
|
|
|
* |
|
46
|
|
|
* Represents a Definition returning an Proxied instance of some class, |
|
47
|
|
|
* using the Proxy Pattern / ProxyManager |
|
48
|
|
|
* |
|
49
|
|
|
* @category Definition |
|
50
|
|
|
* @package Fwk\Di |
|
51
|
|
|
* @author Julien Ballestracci <[email protected]> |
|
52
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
53
|
|
|
* @link http://www.nitronet.org/fwk |
|
54
|
|
|
*/ |
|
55
|
|
|
class LazyClassDefinition extends ClassDefinition |
|
56
|
|
|
{ |
|
57
|
|
|
/** |
|
58
|
|
|
* Instanciates $this->className and return the instance. |
|
59
|
|
|
* |
|
60
|
|
|
* @param Container $container The Di Container |
|
61
|
|
|
* @param null|string $name Name of the current definition (if any) |
|
62
|
|
|
* |
|
63
|
|
|
* @return VirtualProxyInterface |
|
64
|
|
|
* @throws Exceptions\InvalidClassDefinitionException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function invoke(Container $container, $name = null) |
|
67
|
|
|
{ |
|
68
|
|
|
$proxy = $this->getProxyFactory()->createProxy( |
|
69
|
|
|
$container->propertizeString($this->className), |
|
70
|
|
|
function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($container, $name) { |
|
71
|
|
|
$wrappedInstance = parent::invoke($container, $name); |
|
72
|
|
|
$proxy->setProxyInitializer(null); |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
return $proxy; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return LazyLoadingValueHolderFactory |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getProxyFactory() |
|
84
|
|
|
{ |
|
85
|
|
|
$config = new Configuration(); |
|
86
|
|
|
$config->setGeneratorStrategy(new EvaluatingGeneratorStrategy()); |
|
87
|
|
|
|
|
88
|
|
|
return new LazyLoadingValueHolderFactory($config); |
|
89
|
|
|
} |
|
90
|
|
|
} |