Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Prophecy.
|
Chris@0
|
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
6 * Marcello Duarte <marcello.duarte@gmail.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Prophecy\Doubler;
|
Chris@0
|
13
|
Chris@0
|
14 use Doctrine\Instantiator\Instantiator;
|
Chris@0
|
15 use Prophecy\Doubler\ClassPatch\ClassPatchInterface;
|
Chris@0
|
16 use Prophecy\Doubler\Generator\ClassMirror;
|
Chris@0
|
17 use Prophecy\Doubler\Generator\ClassCreator;
|
Chris@0
|
18 use Prophecy\Exception\InvalidArgumentException;
|
Chris@0
|
19 use ReflectionClass;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Cached class doubler.
|
Chris@0
|
23 * Prevents mirroring/creation of the same structure twice.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
26 */
|
Chris@0
|
27 class Doubler
|
Chris@0
|
28 {
|
Chris@0
|
29 private $mirror;
|
Chris@0
|
30 private $creator;
|
Chris@0
|
31 private $namer;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * @var ClassPatchInterface[]
|
Chris@0
|
35 */
|
Chris@0
|
36 private $patches = array();
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * @var \Doctrine\Instantiator\Instantiator
|
Chris@0
|
40 */
|
Chris@0
|
41 private $instantiator;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Initializes doubler.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @param ClassMirror $mirror
|
Chris@0
|
47 * @param ClassCreator $creator
|
Chris@0
|
48 * @param NameGenerator $namer
|
Chris@0
|
49 */
|
Chris@0
|
50 public function __construct(ClassMirror $mirror = null, ClassCreator $creator = null,
|
Chris@0
|
51 NameGenerator $namer = null)
|
Chris@0
|
52 {
|
Chris@0
|
53 $this->mirror = $mirror ?: new ClassMirror;
|
Chris@0
|
54 $this->creator = $creator ?: new ClassCreator;
|
Chris@0
|
55 $this->namer = $namer ?: new NameGenerator;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Returns list of registered class patches.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @return ClassPatchInterface[]
|
Chris@0
|
62 */
|
Chris@0
|
63 public function getClassPatches()
|
Chris@0
|
64 {
|
Chris@0
|
65 return $this->patches;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Registers new class patch.
|
Chris@0
|
70 *
|
Chris@0
|
71 * @param ClassPatchInterface $patch
|
Chris@0
|
72 */
|
Chris@0
|
73 public function registerClassPatch(ClassPatchInterface $patch)
|
Chris@0
|
74 {
|
Chris@0
|
75 $this->patches[] = $patch;
|
Chris@0
|
76
|
Chris@0
|
77 @usort($this->patches, function (ClassPatchInterface $patch1, ClassPatchInterface $patch2) {
|
Chris@0
|
78 return $patch2->getPriority() - $patch1->getPriority();
|
Chris@0
|
79 });
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Creates double from specific class or/and list of interfaces.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @param ReflectionClass $class
|
Chris@0
|
86 * @param ReflectionClass[] $interfaces Array of ReflectionClass instances
|
Chris@0
|
87 * @param array $args Constructor arguments
|
Chris@0
|
88 *
|
Chris@0
|
89 * @return DoubleInterface
|
Chris@0
|
90 *
|
Chris@0
|
91 * @throws \Prophecy\Exception\InvalidArgumentException
|
Chris@0
|
92 */
|
Chris@0
|
93 public function double(ReflectionClass $class = null, array $interfaces, array $args = null)
|
Chris@0
|
94 {
|
Chris@0
|
95 foreach ($interfaces as $interface) {
|
Chris@0
|
96 if (!$interface instanceof ReflectionClass) {
|
Chris@0
|
97 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
98 "[ReflectionClass \$interface1 [, ReflectionClass \$interface2]] array expected as\n".
|
Chris@0
|
99 "a second argument to `Doubler::double(...)`, but got %s.",
|
Chris@0
|
100 is_object($interface) ? get_class($interface).' class' : gettype($interface)
|
Chris@0
|
101 ));
|
Chris@0
|
102 }
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 $classname = $this->createDoubleClass($class, $interfaces);
|
Chris@0
|
106 $reflection = new ReflectionClass($classname);
|
Chris@0
|
107
|
Chris@0
|
108 if (null !== $args) {
|
Chris@0
|
109 return $reflection->newInstanceArgs($args);
|
Chris@0
|
110 }
|
Chris@0
|
111 if ((null === $constructor = $reflection->getConstructor())
|
Chris@0
|
112 || ($constructor->isPublic() && !$constructor->isFinal())) {
|
Chris@0
|
113 return $reflection->newInstance();
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 if (!$this->instantiator) {
|
Chris@0
|
117 $this->instantiator = new Instantiator();
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 return $this->instantiator->instantiate($classname);
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * Creates double class and returns its FQN.
|
Chris@0
|
125 *
|
Chris@0
|
126 * @param ReflectionClass $class
|
Chris@0
|
127 * @param ReflectionClass[] $interfaces
|
Chris@0
|
128 *
|
Chris@0
|
129 * @return string
|
Chris@0
|
130 */
|
Chris@0
|
131 protected function createDoubleClass(ReflectionClass $class = null, array $interfaces)
|
Chris@0
|
132 {
|
Chris@0
|
133 $name = $this->namer->name($class, $interfaces);
|
Chris@0
|
134 $node = $this->mirror->reflect($class, $interfaces);
|
Chris@0
|
135
|
Chris@0
|
136 foreach ($this->patches as $patch) {
|
Chris@0
|
137 if ($patch->supports($node)) {
|
Chris@0
|
138 $patch->apply($node);
|
Chris@0
|
139 }
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 $this->creator->create($name, $node);
|
Chris@0
|
143
|
Chris@0
|
144 return $name;
|
Chris@0
|
145 }
|
Chris@0
|
146 }
|