Permalink
Browse files

Klass can now call class level methods

  • Loading branch information...
1 parent c596621 commit fe2c3bd68886abb158ca4f5e5d6b20028825e821 @shuber committed Mar 1, 2011
Showing with 32 additions and 1 deletion.
  1. +21 −1 lib/eigenclass.php
  2. +11 −0 test/unit/eigenclass_test.php
View
@@ -16,15 +16,35 @@ function __include($modules, $instance = false) {
return call_user_func_array($method, $modules);
}
+ function ancestors() {
+ if ($this->is_class()) {
+ $ancestors = array($this->_object);
+ if ($this->_object->superclass()) {
+ $ancestors = array_merge($ancestors, $this->_object->superclass()->__class()->ancestors());
+ } else {
+ $ancestors = array_merge($ancestors, Klass::instance(__CLASS__)->ancestors());
+ }
+ $ancestors = array_reverse(array_unique(array_reverse($ancestors), SORT_REGULAR));
+ } else {
+ $ancestors = parent::ancestors();
+ }
+ return $ancestors;
+ }
+
function callee($method, &$caller = null) {
$ancestors = $this->ancestors();
if ($caller && in_array($caller, $ancestors)) $ancestors = array_slice($ancestors, array_search($caller, $ancestors) + 1);
foreach ($ancestors as $ancestor) {
- $methods = $ancestor->reflection()->instance_methods(false);
+ $methods_type = $this->is_class() && ($this->_object == $ancestor || in_array($ancestor->name(), class_parents($this->_object->name()))) ? 'class' : 'instance';
+ $methods = $ancestor->reflection()->{$methods_type.'_methods'}(false);
if (isset($methods[$method])) return $methods[$method];
}
}
+ function is_class() {
+ return is_a($this->_object, get_parent_class(__CLASS__));
+ }
+
function object() {
return $this->_object;
}
@@ -51,5 +51,16 @@ function test_should_return_object() {
assert_identical($this->user_class, $this->user_class->__class()->object());
}
+ function test_should_return_ancestors_for_class() {
+ $ancestors = array('EigenclassTest\User', 'Eigenclass', 'Klass\InstanceMethods', 'Klass', 'Object\InstanceMethods', 'Object');
+ foreach ($ancestors as $index => $ancestor) $ancestors[$index] = Klass::instance($ancestor);
+ assert_equal($ancestors, $this->user_class->__class()->ancestors());
+ }
+
+ function test_should_return_is_class() {
+ ensure($this->user_class->__class()->is_class());
+ ensure(!$this->user->__class()->is_class());
+ }
+
}
}

0 comments on commit fe2c3bd

Please sign in to comment.