7 changed files with 298 additions and 5 deletions
-
2ext/spl/config.m4
-
2ext/spl/config.w32
-
4ext/spl/php_spl.c
-
50ext/spl/spl.php
-
88ext/spl/spl_observer.c
-
41ext/spl/spl_observer.h
-
116ext/spl/tests/observer_001.phpt
@ -0,0 +1,88 @@ |
|||
/* |
|||
+----------------------------------------------------------------------+ |
|||
| PHP Version 5 | |
|||
+----------------------------------------------------------------------+ |
|||
| Copyright (c) 1997-2004 The PHP Group | |
|||
+----------------------------------------------------------------------+ |
|||
| This source file is subject to version 3.0 of the PHP license, | |
|||
| that is bundled with this package in the file LICENSE, and is | |
|||
| available through the world-wide-web at the following url: | |
|||
| http://www.php.net/license/3_0.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: Marcus Boerger <helly@php.net> | |
|||
+----------------------------------------------------------------------+ |
|||
*/ |
|||
|
|||
/* $Id$ */ |
|||
|
|||
#ifdef HAVE_CONFIG_H |
|||
# include "config.h" |
|||
#endif |
|||
|
|||
#include "php.h" |
|||
#include "php_ini.h" |
|||
#include "ext/standard/info.h" |
|||
#include "zend_interfaces.h" |
|||
#include "zend_exceptions.h" |
|||
|
|||
#include "php_spl.h" |
|||
#include "spl_functions.h" |
|||
#include "spl_engine.h" |
|||
#include "spl_observer.h" |
|||
|
|||
SPL_METHOD(Observer, update); |
|||
SPL_METHOD(Subject, attach); |
|||
SPL_METHOD(Subject, detach); |
|||
SPL_METHOD(Subject, notify); |
|||
|
|||
static |
|||
ZEND_BEGIN_ARG_INFO(arginfo_Observer_update, 0) |
|||
ZEND_ARG_OBJ_INFO(0, subject, Subject, 0) |
|||
ZEND_END_ARG_INFO(); |
|||
|
|||
static zend_function_entry spl_funcs_Observer[] = { |
|||
SPL_ABSTRACT_ME(Observer, update, arginfo_Observer_update) |
|||
{NULL, NULL, NULL} |
|||
}; |
|||
|
|||
static |
|||
ZEND_BEGIN_ARG_INFO(arginfo_Subject_attach, 0) |
|||
ZEND_ARG_OBJ_INFO(0, observer, Observer, 0) |
|||
ZEND_END_ARG_INFO(); |
|||
|
|||
/*static |
|||
ZEND_BEGIN_ARG_INFO_EX(arginfo_Subject_notify, 0, 0, 1) |
|||
ZEND_ARG_OBJ_INFO(0, ignore, Observer, 1) |
|||
ZEND_END_ARG_INFO();*/ |
|||
|
|||
static zend_function_entry spl_funcs_Subject[] = { |
|||
SPL_ABSTRACT_ME(Subject, attach, arginfo_Subject_attach) |
|||
SPL_ABSTRACT_ME(Subject, detach, arginfo_Subject_attach) |
|||
SPL_ABSTRACT_ME(Subject, notify, NULL) |
|||
{NULL, NULL, NULL} |
|||
}; |
|||
|
|||
PHPAPI zend_class_entry *spl_ce_Observer; |
|||
PHPAPI zend_class_entry *spl_ce_Subject; |
|||
|
|||
/* {{{ PHP_MINIT_FUNCTION(spl_observer) */ |
|||
PHP_MINIT_FUNCTION(spl_observer) |
|||
{ |
|||
REGISTER_SPL_INTERFACE(Observer); |
|||
REGISTER_SPL_INTERFACE(Subject); |
|||
|
|||
return SUCCESS; |
|||
} |
|||
/* }}} */ |
|||
|
|||
/* |
|||
* Local variables: |
|||
* tab-width: 4 |
|||
* c-basic-offset: 4 |
|||
* End: |
|||
* vim600: fdm=marker |
|||
* vim: noet sw=4 ts=4 |
|||
*/ |
|||
@ -0,0 +1,41 @@ |
|||
/* |
|||
+----------------------------------------------------------------------+ |
|||
| PHP Version 5 | |
|||
+----------------------------------------------------------------------+ |
|||
| Copyright (c) 1997-2004 The PHP Group | |
|||
+----------------------------------------------------------------------+ |
|||
| This source file is subject to version 3.0 of the PHP license, | |
|||
| that is bundled with this package in the file LICENSE, and is | |
|||
| available through the world-wide-web at the following url: | |
|||
| http://www.php.net/license/3_0.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: Marcus Boerger <helly@php.net> | |
|||
+----------------------------------------------------------------------+ |
|||
*/ |
|||
|
|||
/* $Id$ */ |
|||
|
|||
#ifndef SPL_OBSERVER_H |
|||
#define SPL_OBSERVER_H |
|||
|
|||
#include "php.h" |
|||
#include "php_spl.h" |
|||
|
|||
extern PHPAPI zend_class_entry *spl_ce_Observer; |
|||
extern PHPAPI zend_class_entry *spl_ce_Subject; |
|||
|
|||
PHP_MINIT_FUNCTION(spl_observer); |
|||
|
|||
#endif /* SPL_OBSERVER_H */ |
|||
|
|||
/* |
|||
* Local Variables: |
|||
* c-basic-offset: 4 |
|||
* tab-width: 4 |
|||
* End: |
|||
* vim600: fdm=marker |
|||
* vim: noet sw=4 ts=4 |
|||
*/ |
|||
@ -0,0 +1,116 @@ |
|||
--TEST-- |
|||
SPL: Observer and Subject (empty notify) |
|||
--FILE-- |
|||
<?php |
|||
|
|||
class ObserverImpl implements Observer |
|||
{ |
|||
protected $name = ''; |
|||
|
|||
function __construct($name = 'obj') |
|||
{ |
|||
$this->name = '$' . $name; |
|||
} |
|||
|
|||
function update(Subject $subject) |
|||
{ |
|||
echo $this->name . '->' . __METHOD__ . '(' . $subject->getName() . ");\n"; |
|||
} |
|||
|
|||
function getName() |
|||
{ |
|||
return $this->name; |
|||
} |
|||
} |
|||
|
|||
class SubjectImpl implements Subject |
|||
{ |
|||
protected $name = ''; |
|||
protected $observers = array(); |
|||
|
|||
function __construct($name = 'sub') |
|||
{ |
|||
$this->name = '$' . $name; |
|||
} |
|||
|
|||
function attach(Observer $observer) |
|||
{ |
|||
echo '$sub->' . __METHOD__ . '(' . $observer->getName() . ");\n"; |
|||
if (!in_array($observer, $this->observers)) |
|||
{ |
|||
$this->observers[] = $observer; |
|||
} |
|||
} |
|||
|
|||
function detach(Observer $observer) |
|||
{ |
|||
echo '$sub->' . __METHOD__ . '(' . $observer->getName() . ");\n"; |
|||
$idx = array_search($observer, $this->observers); |
|||
if ($idx !== false) |
|||
{ |
|||
unset($this->observers[$idx]); |
|||
} |
|||
} |
|||
|
|||
function notify() |
|||
{ |
|||
echo '$sub->' . __METHOD__ . "();\n"; |
|||
foreach($this->observers as $observer) |
|||
{ |
|||
$observer->update($this); |
|||
} |
|||
} |
|||
|
|||
function getName() |
|||
{ |
|||
return $this->name; |
|||
} |
|||
} |
|||
|
|||
$sub = new SubjectImpl; |
|||
|
|||
$ob1 = new ObserverImpl("ob1"); |
|||
$ob2 = new ObserverImpl("ob2"); |
|||
$ob3 = new ObserverImpl("ob3"); |
|||
|
|||
$sub->attach($ob1); |
|||
$sub->attach($ob1); |
|||
$sub->attach($ob2); |
|||
$sub->attach($ob3); |
|||
|
|||
$sub->notify(); |
|||
|
|||
$sub->detach($ob3); |
|||
|
|||
$sub->notify(); |
|||
|
|||
$sub->detach($ob2); |
|||
$sub->detach($ob1); |
|||
|
|||
$sub->notify(); |
|||
|
|||
$sub->attach($ob3); |
|||
|
|||
$sub->notify(); |
|||
?> |
|||
===DONE=== |
|||
--EXPECT-- |
|||
$sub->SubjectImpl::attach($ob1); |
|||
$sub->SubjectImpl::attach($ob1); |
|||
$sub->SubjectImpl::attach($ob2); |
|||
$sub->SubjectImpl::attach($ob3); |
|||
$sub->SubjectImpl::notify(); |
|||
$ob1->ObserverImpl::update($sub); |
|||
$ob2->ObserverImpl::update($sub); |
|||
$ob3->ObserverImpl::update($sub); |
|||
$sub->SubjectImpl::detach($ob3); |
|||
$sub->SubjectImpl::notify(); |
|||
$ob1->ObserverImpl::update($sub); |
|||
$ob2->ObserverImpl::update($sub); |
|||
$sub->SubjectImpl::detach($ob2); |
|||
$sub->SubjectImpl::detach($ob1); |
|||
$sub->SubjectImpl::notify(); |
|||
$sub->SubjectImpl::attach($ob3); |
|||
$sub->SubjectImpl::notify(); |
|||
$ob3->ObserverImpl::update($sub); |
|||
===DONE=== |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue