[limb-svn] r5813 - in 3.x/trunk/limb/core: src tests/cases

svn at limb-project.com svn at limb-project.com
Sat May 5 16:24:24 MSD 2007


Author: pachanga
Date: 2007-05-05 16:24:24 +0400 (Sat, 05 May 2007)
New Revision: 5813
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5813

Modified:
   3.x/trunk/limb/core/src/lmbMixable.class.php
   3.x/trunk/limb/core/tests/cases/lmbMixableTest.class.php
Log:
-- lmbMixable :: setOwner($obj) added, by default owner is mixable itself, however sometimes it's not a good practice to extend lmbMixable but rather delegate to it

Modified: 3.x/trunk/limb/core/src/lmbMixable.class.php
===================================================================
--- 3.x/trunk/limb/core/src/lmbMixable.class.php	2007-05-05 11:06:29 UTC (rev 5812)
+++ 3.x/trunk/limb/core/src/lmbMixable.class.php	2007-05-05 12:24:24 UTC (rev 5813)
@@ -12,6 +12,7 @@
 
 class lmbMixable
 {
+  protected $owner;
   protected $mixins = array();
   protected $mixins_loaded = false;
   protected $mixins_signatures = array();
@@ -21,6 +22,11 @@
     $this->mixins[] = $mixin;
   }
 
+  function setOwner($owner)
+  {
+    $this->owner = $owner;
+  }
+
   function __call($method, $args)
   {
     $this->_ensureSignatures();
@@ -33,7 +39,7 @@
     return call_user_func_array(array($this->mixins_signatures[$method], $method), $args);
   }
 
-  function get($name)
+  function _get($name)
   {
     if(isset($this->$name))
       return $this->$name;
@@ -44,6 +50,8 @@
     if($this->mixins_loaded)
       return;
 
+    $owner = $this->owner ? $this->owner : $this;
+
     foreach($this->mixins as $mixin)
     {
       if(is_object($mixin))
@@ -56,7 +64,7 @@
         $obj = new $mixin();
         $class = $mixin;
       }
-      $obj->setOwner($this);
+      $obj->setOwner($owner);
 
       foreach(get_class_methods($class) as $method)
         $this->mixins_signatures[$method] = $obj;

Modified: 3.x/trunk/limb/core/tests/cases/lmbMixableTest.class.php
===================================================================
--- 3.x/trunk/limb/core/tests/cases/lmbMixableTest.class.php	2007-05-05 11:06:29 UTC (rev 5812)
+++ 3.x/trunk/limb/core/tests/cases/lmbMixableTest.class.php	2007-05-05 12:24:24 UTC (rev 5813)
@@ -28,7 +28,7 @@
   }
 }
 
-class MixingCallingOwnerMethod extends lmbMixin
+class MixinCallingOwnerMethod extends lmbMixin
 {
   function ownerMy()
   {
@@ -40,7 +40,7 @@
 {
   function ownerVar()
   {
-    return $this->owner->get('var');
+    return $this->owner->_get('var');
   }
 }
 
@@ -56,7 +56,7 @@
 {
   protected $var = 'var';
 
-  function __construct($mixins)
+  function __construct($mixins = array())
   {
     $this->mixins = $mixins;
   }
@@ -67,6 +67,14 @@
   }
 }
 
+class MixedTestStub
+{
+  function my()
+  {
+    return 'stub';
+  }
+}
+
 class lmbMixableTest extends UnitTestCase
 {
   function testMixinObjects()
@@ -87,6 +95,14 @@
     $this->assertEqual($mixed->bar(), 'bar');
   }
 
+  function testSetOwner()
+  {
+    $mixed = new lmbMixable();
+    $mixed->setOwner(new MixedTestStub());
+    $mixed->mixin('MixinCallingOwnerMethod');
+    $this->assertEqual($mixed->ownerMy(), 'stub');
+  }
+
   function testOwnerMethodInvokation()
   {
     $mixed = new MixableTestVersion(array('MixinFoo', 'MixinBar'));
@@ -97,13 +113,13 @@
 
   function testCallOwnerFromMixinForObjects()
   {
-    $mixed = new MixableTestVersion(array(new MixingCallingOwnerMethod()));
+    $mixed = new MixableTestVersion(array(new MixinCallingOwnerMethod()));
     $this->assertEqual($mixed->ownerMy(), 'my');
   }
 
   function testCallOwnerFromMixinForClasses()
   {
-    $mixed = new MixableTestVersion(array('MixingCallingOwnerMethod'));
+    $mixed = new MixableTestVersion(array('MixinCallingOwnerMethod'));
     $this->assertEqual($mixed->ownerMy(), 'my');
   }
 



More information about the limb-svn mailing list