From dc2f24a850d312c77958fa28ddec6f5cb9d2ed22 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 29 Jul 2013 23:21:29 -0400 Subject: [PATCH] Simplify WeakInterface by replacing the MethodImp with a hash. --- base/weak_interface.php | 77 +++++++++-------------------------------- 1 file changed, 17 insertions(+), 60 deletions(-) diff --git a/base/weak_interface.php b/base/weak_interface.php index d292715..e2fab25 100644 --- a/base/weak_interface.php +++ b/base/weak_interface.php @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License along with // this program. If not, see . -namespace hoplite\base { +namespace hoplite\base; /*! A WeakInterface is a class that implements some interface and then can bind @@ -28,7 +28,7 @@ namespace hoplite\base { */ class WeakInterface { - /*! @var array Map of MethodImps keyed by the method name. */ + /*! @var array Map of method imps hashes, keyed by the method name. */ private $imps = array(); /*! @var object The object to which this interface is bound. */ @@ -47,7 +47,11 @@ class WeakInterface $methods = $reflector->GetMethods(); foreach ($methods as $method) { $name = $method->name; - $this->imps[$name] = new internal\MethodImp($this, $method); + $imp = array( + 'method' => $method, + 'required' => strpos($method->GetDocComment(), '@required') !== FALSE, + ); + $this->imps[$name] = $imp; } } @@ -81,7 +85,14 @@ class WeakInterface return; } - return $this->imps[$meth]->Invoke($this->object_reflector, $args); + try { + $imp = $this->object_reflector->GetMethod($meth); + } catch (\ReflectionException $e) { + if ($this->imps[$meth]['required']) + throw $e; + return; + } + return $imp->InvokeArgs($this->object, $args); } /*! Ensures that the bound object properly implements the interface. */ @@ -95,67 +106,13 @@ class WeakInterface } foreach ($this->imps as $name => $imp) { - if ($imp->IsRequired() && !isset($by_name[$name])) + if ($imp['required'] && !isset($by_name[$name])) throw new WeakInterfaceException($reflector->name . ' does not implement required interface method ' . $name); - if (isset($by_name[$name]) && !$imp->Matches($by_name[$name])) + if (isset($by_name[$name]) && $imp['method']->GetParameters() != $by_name[$name]->GetParameters()) throw new WeakInterfaceException($reflector->name . '::' . $name . ' does not match interface definition'); } } } class WeakInterfaceException extends \Exception {} - -} // namespace hoplite\base - -namespace hoplite\base\internal { - -/*! - An encapsulation of a method implementation. -*/ -class MethodImp -{ - /*! @var WeakInterface The interface to which this method belongs. */ - private $interface = NULL; - - /*! @var ReflectionMethod The method of the actual interface that this is implementing. */ - private $method = NULL; - - /*! @var bool Whether or not this is required. */ - private $required = FALSE; - - public function __construct(\hoplite\base\WeakInterface $interface, - \ReflectionMethod $method) - { - $this->interface = $interface; - $this->method = $method; - $this->required = strpos($this->method->GetDocComment(), '@required') !== FALSE; - } - - /*! Forwards a method call. */ - public function Invoke($reflector, $args) - { - try { - $impl = $reflector->GetMethod($this->method->name); - } catch (\ReflectionException $e) { - if ($this->required) - throw $e; - return; - } - return $impl->InvokeArgs($this->interface->get(), $args); - } - - /*! Performs method signature validation. */ - public function Matches(\ReflectionMethod $method) - { - return $method->GetParameters() == $this->method->GetParameters(); - } - - /*! Checks if a method is marked as required. */ - public function IsRequired() - { - return $this->required; - } -} - -} // namespace hoplite\base\internal -- 2.22.5