4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Doubler;
17 * Cached class doubler.
18 * Prevents mirroring/creation of the same structure twice.
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
22 class CachedDoubler extends Doubler
24 private $classes = array();
29 public function registerClassPatch(ClassPatch\ClassPatchInterface $patch)
31 $this->classes[] = array();
33 parent::registerClassPatch($patch);
39 protected function createDoubleClass(ReflectionClass $class = null, array $interfaces)
41 $classId = $this->generateClassId($class, $interfaces);
42 if (isset($this->classes[$classId])) {
43 return $this->classes[$classId];
46 return $this->classes[$classId] = parent::createDoubleClass($class, $interfaces);
50 * @param ReflectionClass $class
51 * @param ReflectionClass[] $interfaces
55 private function generateClassId(ReflectionClass $class = null, array $interfaces)
58 if (null !== $class) {
59 $parts[] = $class->getName();
61 foreach ($interfaces as $interface) {
62 $parts[] = $interface->getName();
66 return md5(implode('', $parts));