1: <?php
2:
3: namespace Deimos\DI;
4:
5: class Instance
6: {
7:
8: /**
9: * @var DI
10: */
11: protected $container;
12:
13: /**
14: * @var string
15: */
16: protected $class;
17:
18: /**
19: * @var Argument
20: */
21: protected $arguments;
22:
23: /**
24: * any object
25: *
26: * @var mixed
27: */
28: protected $object;
29:
30: /**
31: * Instance constructor.
32: *
33: * @param DI $container
34: * @param string $class
35: * @param Argument $arguments
36: */
37: public function __construct(DI $container, $class, Argument $arguments)
38: {
39: $this->container = $container;
40: $this->class = $class;
41: $this->arguments = $arguments;
42: }
43:
44: /**
45: * @return mixed
46: */
47: protected function instance()
48: {
49: $class = new \ReflectionClass($this->class);
50:
51: if ($class->getConstructor() === null)
52: {
53: return $class->newInstance();
54: }
55:
56: return $class->newInstanceArgs($this->arguments->get());
57: }
58:
59: /**
60: * @return mixed
61: */
62: public function get()
63: {
64: if (!$this->object)
65: {
66: $this->object = $this->instance();
67: }
68:
69: return $this->object;
70: }
71:
72: }