Re: PEAR_DelegateOwner
| From: | LingWitt at insightbb dot com | Date: | Fri, 07 Nov 2003 02:46:49 +0000 |
| Subject: | Re: PEAR_DelegateOwner | ||
| References: | 1 2 3 4 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-23316@lists.php.net to get a copy of this message | ||
I would at least appreciate Your work is shit! Give me something. Has no one looked at this? Here are the files:
in DelegateInterface.php:
interface PEAR_DelegateInterface
{
public function setOwner($owner);
public function getOwner();
}
in DelegateOwner.php:
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Herr Witten <LingWitt@yahoo.com> |
// | |
// +----------------------------------------------------------------------+
//
// $Id$
require_once "PEAR.php";
require_once "PEAR/DelegateInterface.php";
class PEAR_DelegateOwner extends PEAR
{
var $_delegates = array();
var $_method_map = array();
public function addDelegate($variableRequired)
{
$args = array_values(func_get_args());
foreach ($args as $delegate)
{
if (!is_object($arg))
$delegate = new $delegate;
if (!($delegate instanceof PEAR_DelegateInterface))
continue;
$delegate->setOwner($this);
$this->_delegates[get_class($delegate)] = $delegate;
}
}
public function &getDelegates()
{
return $this->_delegates;
}
//Classnames
public function getDelegate($variableRequired)
{
$args = func_get_args();
foreach ($args as $arg)
{
foreach ($this->_delegates as $delegate)
{
if ($delegate instanceof $arg)
{
$results[$arg] = $delegate;
}
}
}
return $results;
}
//Classnames
public function getDelegateExact($classname)
{
$classname = strtolower($classname);
if (array_key_exists($classname, $this->_delegates))
{
return $this->_delegates[$classname];
}
else
{
return null;
}
}
public function hasDelegate($delegate)
{
if (is_string($delegate) && $this->getDelegate($delegate))
{
return true;
}
else if (is_object($delegate))
{
foreach ($this->_delegates as $delegateObject)
{
if ($delegateObject === $delegate)
return true;
}
}
return false;
}
//Classnames
public function hasDelegateExact($classname)
{
if ($this->getDelegateExact($classname))
{
return true;
}
return false;
}
public function removeDelegates()
{
unset($this->_method_map);
unset($this->_delegates);
}
private function filterMethodMapWithDelegate(PEAR_DelegateInterface $filterDelegate)
{
$result = array();
$method_map_keys = array_keys($this->_method_map);
$method_map_values = array_values($this->_method_map);
for ($i = 0, $count = count($method_map_values); $i < $count; $i++)
{
$delegate = $this->_method_map[$i];
if ($delegate === $filterDelegate)
{
continue;
}
$result[$method_map_keys[$i]] = $delegate;
}
return $result;
}
//Classnames
public function removeDelegate($variableRequired)
{
$args = func_get_args();
foreach ($args as $arg)
{
if ($delegates = $this->getDelegate($arg))
{
foreach ($delegates as $delegate)
{
unset($this->_delegates[get_class($delegate)]);
$this->_delegates = $this->filterMethodMapWithDelegate($delegate);
}
}
}
}
//Classnames
public function removeDelegateExact($variableRequired)
{
$args = func_get_args();
foreach ($args as $arg)
{
if ($delegate = $this->getDelegateExact($arg))
{
unset($this->_delegates[get_class($delegate)]);
$this->_delegates = $this->filterMethodMapWithDelegate($delegate);
}
}
}
protected function cacheMethod($method)
{
foreach ($this->_delegates as $delegate)
{
if (method_exists($delegate, $method))
{
$this->_method_map[$method] = $delegate;
return;
}
}
throw new Exception("No Such Method: $method()");
}
public function __call($method, $args)
{
$method = strtolower($method);
if (!array_key_exists($method, $this->_method_map))
{
try {$this->cacheMethod($method);}
catch(Exception $exception)
{
die ("Fatal: " . $exception->getMessage());
}
}
return call_user_func_array(array($this->_method_map[$method], $method), $args);
}
}
?>
On 5 Nov 2003, at 8:04 PM, LingWitt@insightbb.com wrote:
Any comments? interface PEAR_DelegateInterface {public function setOwner($owner); public function getOwner();} On 4 Nov 2003, at 4:44 PM, LingWitt@insightbb.com wrote: <DelegateOwner.php>Erg! I apologize, there seems to be a problem. I will just write it: interface PEAR_DelegateInterface {--PEAR Development Mailing List (http://pear.php.net/) To unsubscribe, visit: http://www.php.net/unsub.phppublic function setOwner($owner); public function getOwner();} On 4 Nov 2003, at 4:41 PM, LingWitt@insightbb.com wrote:Apparently, only one files was sent. I have included the other. On 4 Nov 2003, at 4:35 PM, LingWitt@insightbb.com wrote:--PEAR Development Mailing List (http://pear.php.net/) To unsubscribe, visit: http://www.php.net/unsub.phpI have sent my work on the delegation classes. There are two files. One contains the PEAR_DelegateInterface adopted by all objects that intend to offer themselves for delegation. The other contains the PEAR_DelegateOwner class from which all classes that intend to have this capability inherit (though I think these methods would be better off in the PEAR class). The scheme is as follows: You add either a delegate object or a delegate class (string or class name from which the object is created) to a delegate owner, which records the delegate along with its class. When a non-native method is called on a delegate owner, the owner checks to see whether or not one of its delegates can handle it. If none can, then it dies with an error (the format of which probably needs to be modified). If one can, the method-delegate relationship is cached for future reference, thereby making the process as efficient as possible. I would like to have some feedback, so please take a look at it and tell me what you think. Thanks. NOTE: I have not yet included any documentation. <DelegateOwner.php>-- PEAR Development Mailing List (http://pear.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php--PEAR Development Mailing List (http://pear.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php