Skip to content

Commit

Permalink
Some changes to the interface of Object and additional tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan committed Sep 14, 2009
1 parent 0219163 commit c7a1eef
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
52 changes: 21 additions & 31 deletions recess/recess/lang/ClassDescriptor.class.php
Expand Up @@ -17,27 +17,41 @@ class ClassDescriptor {
protected $attachedMethods = array();

/**
* Return a RecessAttachedMethod for given name, or return false.
* Attach a method to a class. The result of this static method is the ability to
* call, on any instance of $attachOnClassName, a method named $attachedMethodAlias
* which delegates that method call to $callable.
*
* @param string $attachedMethodAlias
* @param callable $callable
*/
function attach($alias, $callable) {
$attachedMethod = new AttachedMethod($alias, $callable);
$this->attachedMethods[$alias] = $attachedMethod;
return $callable;
}

/**
* Return the callable of an attached method for given name or false.
*
* @param string $methodName Method name.
* @return RecessAttachedMethod on success, false on failure.
*/
function getAttachedMethod($methodName) {
function attached($methodName) {
if(isset($this->attachedMethods[$methodName]))
return $this->attachedMethods[$methodName];
return $this->attachedMethods[$methodName]->callable;
else
return false;
}

/**
* Return a RecessAttachedMethod for given name, or return false.
* Return a RecessAttachedMethod for given name or false.
*
* @param string $methodName Method name.
* @return RecessAttachedMethod on success, false on failure.
*/
function attached($methodName) {
function getAttachedMethod($methodName) {
if(isset($this->attachedMethods[$methodName]))
return $this->attachedMethods[$methodName]->callable;
return $this->attachedMethods[$methodName];
else
return false;
}
Expand All @@ -50,29 +64,5 @@ function attached($methodName) {
function getAttachedMethods() {
return $this->attachedMethods;
}

/**
* Add an attached method with given methodName alias.
*
* @param string $methodName
* @param AttachedMethod $attachedMethod
*/
function addAttachedMethod($methodName, AttachedMethod $attachedMethod) {
$this->attachedMethods[$methodName] = $attachedMethod;
}

/**
* Attach a method to a class. The result of this static method is the ability to
* call, on any instance of $attachOnClassName, a method named $attachedMethodAlias
* which delegates that method call to $callable.
*
* @param string $attachedMethodAlias
* @param callable $callable
*/
function attachMethod($attachedMethodAlias, $callable) {
$attachedMethod = new AttachedMethod($attachedMethodAlias, $callable);
$this->addAttachedMethod($attachedMethodAlias, $attachedMethod);
return $callable;
}


}
12 changes: 5 additions & 7 deletions recess/recess/lang/Object.class.php
Expand Up @@ -66,7 +66,7 @@ abstract class Object {
* @return callable $callable Returns the callable for further chaining.
*/
static function attach($alias, $callable) {
return static::getClassDescriptor()->attachMethod($alias, $callable);
return static::getClassDescriptor()->attach($alias, $callable);
}

/**
Expand All @@ -86,10 +86,8 @@ static function attached($alias) {
* @param array $arguments
* @return variant
*/
final function __call($name, $arguments) {
$classDescriptor = static::getClassDescriptor($this);

$attachedMethod = $classDescriptor->getAttachedMethod($name);
final function __call($name, $arguments) {
$attachedMethod = static::getClassDescriptor($this)->getAttachedMethod($name);
if($attachedMethod !== false) {
array_unshift($arguments, $this);
return call_user_func_array($attachedMethod->callable, $arguments);
Expand Down Expand Up @@ -127,7 +125,7 @@ final static function getClassDescriptor($classNameOrInstance = false) {
// Cache::set($cache_key, $descriptor);
self::$descriptors[$class] = $descriptor;
} else {
throw new Exception('Class descriptors only exist on classes derived from recess\lang\Object. Class of type "' . $class . '" given.', get_defined_vars());
throw new \Exception('Class descriptors only exist on classes derived from recess\lang\Object. Class of type "' . $class . '" given.');
}
} /* else {
self::$descriptors[$class] = $descriptor;
Expand Down Expand Up @@ -215,7 +213,7 @@ protected static function buildClassDescriptor() {
try {
$reflection = new ReflectionClass($class);
} catch(\ReflectionException $e) {
throw new Exception('Class "' . $class . '" has not been declared.', get_defined_vars());
throw new \Exception('Class "' . $class . '" has not been declared.');
}

foreach ($reflection->getAnnotations() as $annotation) {
Expand Down
10 changes: 10 additions & 0 deletions recess/test/recess/lang/ObjectTest.php
Expand Up @@ -23,6 +23,16 @@ function testAttach() {
$this->assertTrue($anObject->returnTrue());
}

function testAttached() {
$this->assertFalse(AnObject::attached('returnTrue'));
$provider = new IsTrueProvider();
$callable = array($provider,'returnTrue');
AnObject::attach('returnTrue',$callable);
$anObject = new AnObject();
$this->assertTrue($anObject->returnTrue());
$this->assertEquals($callable,AnObject::attached('returnTrue'));
}

function testAttachPlainFunction() {
AnObject::attach('returnTrue','returnTruePlain');
$anObject = new AnObject();
Expand Down

0 comments on commit c7a1eef

Please sign in to comment.