| @@ 7-69 (lines=63) @@ | ||
| 4 | /** |
|
| 5 | * @author Tomasz Kowalczyk <[email protected]> |
|
| 6 | */ |
|
| 7 | final class HydratorContainer implements HydratorContainerInterface |
|
| 8 | { |
|
| 9 | private $handlers = array(); |
|
| 10 | private $interfaces = array(); |
|
| 11 | private $aliases = array(); |
|
| 12 | ||
| 13 | public function add($class, $root, $handler) |
|
| 14 | { |
|
| 15 | if(false === is_callable($handler)) { |
|
| 16 | throw new \RuntimeException(sprintf('Invalid handler for class %s!', $class)); |
|
| 17 | } |
|
| 18 | ||
| 19 | if(class_exists($class)) { |
|
| 20 | $this->aliases[$class] = $root; |
|
| 21 | $this->handlers[$class] = $handler; |
|
| 22 | } elseif(interface_exists($class)) { |
|
| 23 | $this->aliases[$class] = $root; |
|
| 24 | $this->interfaces[$class] = $handler; |
|
| 25 | } else { |
|
| 26 | throw new \RuntimeException(sprintf('Given value %s is neither class nor interface name!', $class)); |
|
| 27 | } |
|
| 28 | } |
|
| 29 | ||
| 30 | public function addAlias($alias, $class) |
|
| 31 | { |
|
| 32 | $handler = $this->getHandler($class); |
|
| 33 | ||
| 34 | if(null === $handler) { |
|
| 35 | throw new \RuntimeException(sprintf('Handler for class %s does not exist!', $class)); |
|
| 36 | } |
|
| 37 | ||
| 38 | $this->handlers[$alias] = $handler; |
|
| 39 | $this->aliases[$alias] = $this->aliases[$class]; |
|
| 40 | } |
|
| 41 | ||
| 42 | public function getRoot($class) |
|
| 43 | { |
|
| 44 | return $this->aliases[$class]; |
|
| 45 | } |
|
| 46 | ||
| 47 | public function getHandler($class) |
|
| 48 | { |
|
| 49 | if(array_key_exists($class, $this->handlers)) { |
|
| 50 | return $this->handlers[$class]; |
|
| 51 | } |
|
| 52 | ||
| 53 | $parents = array_intersect(array_keys($this->handlers), class_parents($class)); |
|
| 54 | if($parents) { |
|
| 55 | return $this->handlers[array_pop($parents)]; |
|
| 56 | } |
|
| 57 | ||
| 58 | $interfaces = array_intersect(array_keys($this->interfaces), array_values(class_implements($class))); |
|
| 59 | if($interfaces) { |
|
| 60 | if(count($interfaces) > 1) { |
|
| 61 | throw new \RuntimeException(sprintf('Class %s implements interfaces with colliding handlers!', $class)); |
|
| 62 | } |
|
| 63 | ||
| 64 | return $this->interfaces[$interfaces[0]]; |
|
| 65 | } |
|
| 66 | ||
| 67 | return null; |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||
| @@ 7-69 (lines=63) @@ | ||
| 4 | /** |
|
| 5 | * @author Tomasz Kowalczyk <[email protected]> |
|
| 6 | */ |
|
| 7 | final class NormalizerContainer implements NormalizerContainerInterface |
|
| 8 | { |
|
| 9 | private $handlers = array(); |
|
| 10 | private $interfaces = array(); |
|
| 11 | private $aliases = array(); |
|
| 12 | ||
| 13 | public function add($class, $root, $handler) |
|
| 14 | { |
|
| 15 | if(false === is_callable($handler)) { |
|
| 16 | throw new \RuntimeException(sprintf('Invalid handler for class %s!', $class)); |
|
| 17 | } |
|
| 18 | ||
| 19 | if(class_exists($class)) { |
|
| 20 | $this->aliases[$class] = $root; |
|
| 21 | $this->handlers[$class] = $handler; |
|
| 22 | } elseif(interface_exists($class)) { |
|
| 23 | $this->aliases[$class] = $root; |
|
| 24 | $this->interfaces[$class] = $handler; |
|
| 25 | } else { |
|
| 26 | throw new \RuntimeException(sprintf('Given value %s is neither class nor interface name!', $class)); |
|
| 27 | } |
|
| 28 | } |
|
| 29 | ||
| 30 | public function addAlias($alias, $class) |
|
| 31 | { |
|
| 32 | $handler = $this->getHandler($class); |
|
| 33 | ||
| 34 | if(null === $handler) { |
|
| 35 | throw new \RuntimeException(sprintf('Handler for class %s does not exist!', $class)); |
|
| 36 | } |
|
| 37 | ||
| 38 | $this->handlers[$alias] = $handler; |
|
| 39 | $this->aliases[$alias] = $this->aliases[$class]; |
|
| 40 | } |
|
| 41 | ||
| 42 | public function getRoot($class) |
|
| 43 | { |
|
| 44 | return $this->aliases[$class]; |
|
| 45 | } |
|
| 46 | ||
| 47 | public function getHandler($class) |
|
| 48 | { |
|
| 49 | if(array_key_exists($class, $this->handlers)) { |
|
| 50 | return $this->handlers[$class]; |
|
| 51 | } |
|
| 52 | ||
| 53 | $parents = array_intersect(array_keys($this->handlers), class_parents($class)); |
|
| 54 | if($parents) { |
|
| 55 | return $this->handlers[array_pop($parents)]; |
|
| 56 | } |
|
| 57 | ||
| 58 | $interfaces = array_intersect(array_keys($this->interfaces), array_values(class_implements($class))); |
|
| 59 | if($interfaces) { |
|
| 60 | if(count($interfaces) > 1) { |
|
| 61 | throw new \RuntimeException(sprintf('Class %s implements interfaces with colliding handlers!', $class)); |
|
| 62 | } |
|
| 63 | ||
| 64 | return $this->interfaces[$interfaces[0]]; |
|
| 65 | } |
|
| 66 | ||
| 67 | return null; |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||