PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/core/app/Object.php

http://zoop.googlecode.com/
PHP | 142 lines | 134 code | 8 blank | 0 comment | 5 complexity | 04ec376fb434ac22ab8c263de35a2c9e MD5 | raw file
  1<?php
  2class Object
  3{
  4	private $mixins = array();
  5	private $getters = array();
  6	private $setters = array();
  7	private $mixinOwner;
  8	protected $allowAdhocAttributes = true;
  9	private $adhocAttributes = array();
 10	
 11	protected function addGetter($name)
 12	{
 13		$this->getters[$name] = $name;
 14	}
 15	
 16	protected function addSetter($name)
 17	{
 18		$this->setters[$name] = $name;		
 19	}
 20	
 21	protected function addGetSetter($name)
 22	{
 23		$this->addGetter($name);
 24		$this->addSetter($name);
 25	}
 26	
 27	public function __isset($name)
 28	{
 29		if(isset($this->getters[$name]))
 30			return true;
 31		
 32		if($this->mixinOwner && property_exists($this->mixinOwner, $name))
 33			return true;
 34		
 35		if($this->allowAdhocAttributes && isset($this->adhocAttributes[$name]))
 36			return true;
 37		
 38		return false;
 39	}
 40	
 41	public function __unset($name)
 42	{
 43		trigger_error("I'm not even sure what this should do yet?  Do getters and setters count here or just adhoc attributes");
 44	}
 45	
 46	public function &__get($name)
 47	{
 48		if(isset($this->getters[$name]))
 49		{
 50			$funcName = "get$name";
 51			return $this->$funcName();
 52		}
 53		
 54		if($this->mixinOwner && property_exists($this->mixinOwner, $name))
 55			return $this->mixinOwner->$name;
 56		
 57		if($this->allowAdhocAttributes && isset($this->adhocAttributes[$name]))
 58			return $this->adhocAttributes[$name];
 59		else
 60			trigger_error("attributes $name does not exist");
 61	}
 62	
 63	public function __set($name, $value)
 64	{
 65		if(isset($this->setters[$name]))
 66		{
 67			$funcName = "set$Name";
 68			$this->$funcName($value);
 69			return;
 70		}
 71		
 72		if($this->mixinOwner && property_exists($this->mixinOwner, $name))
 73		    return $this->mixinOwner->$name = $value;
 74		
 75		if(isset($this->getters[$name]))
 76			trigger_error("attributes $name is read only");
 77		else
 78		{
 79			if($this->allowAdhocAttributes)
 80				$this->adhocAttributes[$name] = $value;
 81			else
 82				trigger_error("attributes $name does not exist");
 83		}
 84	}
 85	
 86	protected function mixin($className, $params = NULL)
 87	{
 88		if(isset($this->mixins[$className]))
 89			return;
 90		
 91		$this->mixins[$className] = new $className();
 92		$this->mixins[$className]->mixedInto($this);
 93		if(method_exists($this->mixins[$className], 'init'))
 94		    $this->mixins[$className]->init($params);
 95		return $this->mixins[$className];
 96	}
 97	
 98	protected function mixedInto($mixer)
 99	{
100		$this->mixinOwner = $mixer;
101		$this->mixins[get_class($mixer)] = $mixer;
102	}
103	
104	protected function getMixinOwner()
105	{
106		return $this->mixinOwner;
107	}
108	
109	public function _methodExists($methodName)
110	{
111		if(method_exists($this, $methodName))
112			return true;
113		
114		foreach($this->mixins as $className => $thisMixin)
115		{
116			$class = new ReflectionClass($className);
117			if($class->hasMethod($methodName))
118				return true;
119		}
120		
121		return false;
122	}
123	
124	public function __call($methodName, $args)
125	{
126		// $t = microtime(1);
127		// foreach($this->mixins as $className => $thisMixin)
128		// 	echo get_class($this) . " $className $methodName $t<br>";
129		
130		foreach($this->mixins as $className => $thisMixin)
131		{
132			//	what is faster, ReflectionClass or method_exists
133			$class = new ReflectionClass($className);
134			if($class->hasMethod($methodName))
135			{
136				return call_user_func_array(array($thisMixin, $methodName), $args);
137			}
138		}
139		
140		trigger_error("Method '$methodName' not found");
141	}
142}