cvs: pear /HTML_Progress2/Progress2 Monitor.php

From: Date: Mon, 01 Aug 2005 08:17:21 +0000
Subject: cvs: pear /HTML_Progress2/Progress2 Monitor.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-33495@lists.php.net to get a copy of this message
farell Mon Aug 1 04:17:21 2005 EDT Modified files: /pear/HTML_Progress2/Progress2 Monitor.php Log: - same listener interface that in progress meter - adds display() method - QF default renderer should be used only with toHtml()

http://cvs.php.net/diff.php/pear/HTML_Progress2/Progress2/Monitor.php?r1=1.2&r2=1.3&ty=u Index: pear/HTML_Progress2/Progress2/Monitor.php diff -u pear/HTML_Progress2/Progress2/Monitor.php:1.2 pear/HTML_Progress2/Progress2/Monitor.php:1.3 --- pear/HTML_Progress2/Progress2/Monitor.php:1.2 Tue Jul 26 12:37:18 2005 +++ pear/HTML_Progress2/Progress2/Monitor.php Mon Aug 1 04:17:20 2005 @@ -16,7 +16,7 @@ * @author Laurent Laville <pear@laurent-laville.org> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: Monitor.php,v 1.2 2005/07/26 16:37:18 farell Exp $ + * @version CVS: $Id: Monitor.php,v 1.3 2005/08/01 08:17:20 farell Exp $ * @link http://pear.php.net/package/HTML_Progress2 */ @@ -96,6 +96,26 @@ */ var $_form; + /** + * Stores the event dispatcher which handles notifications + * + * @var array + * @since 2.0.0RC2 + * @access protected + */ + var $dispatcher; + + /** + * Count the number of observer registered. + * The Event_Dispatcher will be add on first observer registration, and + * will be removed with the last observer. + * + * @var integer + * @since 2.0.0RC2 + * @access private + */ + var $_observerCount; + /** * Constructor (ZE1) @@ -161,6 +181,7 @@ } $this->_id = md5(microtime()); + $this->_observerCount = 0; $this->_form = new HTML_QuickForm($formName); $this->_form->removeAttribute('name'); // XHTML compliance @@ -203,7 +224,7 @@ /** * Adds a new observer to the Event Dispatcher that will listen - * for all messages emitted by this HTML_Progress2_Monitor instance. + * for all messages emitted by this HTML_Progress2 instance. * * @param mixed $callback PHP callback that will act as listener * @@ -211,20 +232,22 @@ * @since 2.0.0 * @access public * @throws HTML_PROGRESS2_ERROR_INVALID_CALLBACK - * @see getListeners(), removeListener() + * @see removeListener() * @tutorial monitor.addlistener.pkg */ function addListener($callback) { if (!is_callable($callback)) { - return $this->raiseError(HTML_PROGRESS2_ERROR_INVALID_CALLBACK, 'warning', + return $this->raiseError(HTML_PROGRESS2_ERROR_INVALID_CALLBACK, 'exception', array('var' => '$callback', 'element' => 'valid Class-Method/Function', 'was' => 'callback', 'paramnum' => 1)); } - $this->_progress->dispatcher->addObserver($callback, null, get_class($this)); + $this->dispatcher =& Event_Dispatcher::getInstance(); + $this->dispatcher->addObserver($callback); + $this->_observerCount++; } /** @@ -235,26 +258,29 @@ * @return bool True if observer was removed, false otherwise * @since 2.0.0 * @access public - * @see getListeners(), addListener() + * @throws HTML_PROGRESS2_ERROR_INVALID_CALLBACK + * @see addListener() * @tutorial monitor.removelistener.pkg */ function removeListener($callback) { - return $this->_progress->dispatcher->removeObserver($callback, null, get_class($this)); - } + if (!is_callable($callback)) { + return $this->raiseError(HTML_PROGRESS2_ERROR_INVALID_CALLBACK, 'exception', + array('var' => '$callback', + 'element' => 'valid Class-Method/Function', + 'was' => 'callback', + 'paramnum' => 1)); + } - /** - * Returns an array of all the listeners attached to this progress monitor. - * - * @return array - * @since 2.0.0 - * @access public - * @see addListener(), removeListener() - * @tutorial monitor.getlisteners.pkg - */ - function getListeners() - { - return $this->_progress->dispatcher->_ro; + $result = $this->dispatcher->removeObserver($callback); + + if ($result) { + $this->_observerCount--; + if ($this->_observerCount == 0) { + unsset($this->dispatcher); + } + } + return $result; } /** @@ -284,7 +310,7 @@ $action = $this->_form->getSubmitValues(); $canceled = isset($action['cancel']); if ($canceled) { - $this->_progress->dispatcher->post($this, 'onCancel', array('time' => microtime())); + $this->_postNotification('onCancel'); } return $canceled; } @@ -301,21 +327,10 @@ { if ($this->isStarted() && !$this->isCanceled()) { $this->_progress->_status = 'show'; - $this->_progress->dispatcher->post($this, 'onSubmit', array('time' => microtime())); - do { - $this->_progress->process(); - if ($this->_progress->getPercentComplete() == 1) { - if ($this->_progress->isIndeterminate()) { - $this->_progress->setValue(0); - } else { - break; - } - } - $this->_progress->moveNext(); - } while(1); - - $this->_progress->dispatcher->post($this, 'onLoad', array('time' => microtime())); + $this->_postNotification('onSubmit'); + $this->_progress->run(); + $this->_postNotification('onLoad'); } } @@ -340,21 +355,7 @@ 'expected' => 'HTML_Progress2 object', 'paramnum' => 1)); } - $this->_progress =& $bar; - - $barAttr = $bar->getProgressAttributes(); - $width = $barAttr['width'] + 70; - - $formTpl = "\n<form{attributes}>" - . "\n<div>" - . "\n{hidden}" - . "<table width=\"{$width}\" border=\"0\">" - . "\n{content}" - . "\n</table>" - . "\n</div>" - . "\n</form>"; - $renderer =& $this->_form->defaultRenderer(); - $renderer->setFormTemplate($formTpl); + $this->_progress = $bar; $bar =& $this->_form->getElement('progressBar'); $bar->setText( $this->_progress->toHtml() ); @@ -433,10 +434,37 @@ */ function toHtml() { + $barAttr = $this->_progress->getProgressAttributes(); + $width = $barAttr['width'] + 70; + + $formTpl = "\n<form{attributes}>" + . "\n<div>" + . "\n{hidden}" + . "<table width=\"{$width}\" border=\"0\">" + . "\n{content}" + . "\n</table>" + . "\n</div>" + . "\n</form>"; + $renderer =& $this->_form->defaultRenderer(); + $renderer->setFormTemplate($formTpl); + return $this->_form->toHtml(); } /** + * + * + * @return void + * @since 2.0.0RC2 + * @access public + * @tutorial monitor.display.pkg + */ + function display() + { + echo $this->toHtml(); + } + + /** * Accepts a renderer * * @param object $renderer An HTML_QuickForm_Renderer object @@ -499,9 +527,28 @@ foreach($args as $name => $value) { $caption = str_replace("%$name%", $value, $caption); } - $this->_progress->setLabelAttributes($this->caption['id'], - array('value' => $caption) - ); + $this->_progress->setLabelAttributes($this->caption['id'], array('value' => $caption)); + } + + /** + * Post a new notification to all observers registered. + * This notification occured only if a dispatcher exists. That means if + * at least one observer was registered. + * + * @param string $event Name of the notification handler + * @param array $info (optional) Additional information about the notification + * + * @return void + * @since 2.0.0RC2 + * @access private + */ + function _postNotification($event, $info = array()) + { + if (isset($this->dispatcher)) { + $info['sender'] = get_class($this); + $info['time'] = microtime(); + $this->dispatcher->post($this, $event, $info); + } } } ?> \ No newline at end of file
« previous php.pear.cvs (#33495) next »