Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding project

  • Loading branch information...
commit 1a6a07f5aee82798a2c19dc9c72c16bc6b4cdc46 0 parents
Dav Glass authored November 12, 2008
30  LICENSE
... ...
@@ -0,0 +1,30 @@
  1
+Software License Agreement (BSD License)
  2
+
  3
+Copyright (c) 2007, Dav Glass <dav.glass@yahoo.com>.
  4
+All rights reserved.
  5
+
  6
+Redistribution and use of this software in source and binary forms, with or without modification, are
  7
+permitted provided that the following conditions are met:
  8
+
  9
+* Redistributions of source code must retain the above
  10
+  copyright notice, this list of conditions and the
  11
+  following disclaimer.
  12
+
  13
+* Redistributions in binary form must reproduce the above
  14
+  copyright notice, this list of conditions and the
  15
+  following disclaimer in the documentation and/or other
  16
+  materials provided with the distribution.
  17
+
  18
+* The name of Dav Glass may not be used to endorse or promote products
  19
+  derived from this software without specific prior
  20
+  written permission of Dav Glass.
  21
+
  22
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  23
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  24
+PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  25
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  28
+TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30
+
5  README
... ...
@@ -0,0 +1,5 @@
  1
+This is a simple prototype of porting the YUI Custom Event model to PHP.
  2
+
  3
+This example supports scope changing but doesn't yet support stopping or bubbling.
  4
+
  5
+When I have time, I'll revisit it and finish it up.
64  comp.php
... ...
@@ -0,0 +1,64 @@
  1
+<?php
  2
+class Comp {
  3
+    function Comp() {
  4
+        $this->init();
  5
+    }
  6
+    function init() {
  7
+        echo('Comp Init<br>');
  8
+        $fName = new firstName();
  9
+        $fName->onError->subscribe('doerror', $this);
  10
+        $fName->init();
  11
+    }
  12
+
  13
+    function getChildren() {
  14
+        echo('Comp: getChildren');
  15
+        
  16
+    }
  17
+    function doError() {
  18
+        echo('<strong>Comp::doError fired in class ('.get_class($this).').</strong><br>');
  19
+    }
  20
+    function __toString() {
  21
+        return 'Class: ['.get_class($this).']';
  22
+    }
  23
+}
  24
+class firstName  {
  25
+    function firstName() {
  26
+        $this->onError = new CustomEvent('onerror', $this);
  27
+        echo('TEST: '.$this->onError.'<br>');
  28
+    }
  29
+    function init() {
  30
+        echo('firstName Init<br>');
  31
+        $inputFN = new inputFirstName();
  32
+        $inputFN->onError->subscribe('doerror', $this);
  33
+        $inputFN->test();
  34
+    }
  35
+    function doError($info1, $info2, $info3, $info4) {
  36
+        echo('firstName::doError Fired in class ('.get_class($this).')<br>');
  37
+        echo('Args1: '.get_class($info1).'<br>');
  38
+        echo('Args2: '.$info2.'<br>');
  39
+        echo('Args3: '.$info3.'<br>');
  40
+        echo('Args3: '.$info4.'<br>');
  41
+        echo('Firing firstName::onError Event<br>');
  42
+        $this->onError->fire();
  43
+    }
  44
+    function __toString() {
  45
+        return 'Class: ['.get_class($this).']';
  46
+    }
  47
+}
  48
+class inputFirstName {
  49
+    function inputFirstName() {
  50
+        $this->onError = new CustomEvent('onerror', $this);
  51
+        $this->init();
  52
+    }
  53
+    function init() {
  54
+        echo('inputFirstName Init<br>');
  55
+    }
  56
+    function test() {
  57
+        echo('Firing inputFirstName::onError Event<br>');
  58
+        $this->onError->fire('arg1', 'arg2', 'arg3');
  59
+    }
  60
+    function __toString() {
  61
+        return 'Class: ['.get_class($this).']';
  62
+    }
  63
+}
  64
+?>
68  customevent.php
... ...
@@ -0,0 +1,68 @@
  1
+<?php
  2
+class CustomEvent {
  3
+    private $subscribers = Array();
  4
+    private $name;
  5
+    private $scope;
  6
+
  7
+    public function __construct($name, $scope=false) {
  8
+        $this->name = $name;
  9
+        $this->scope = $scope;
  10
+        echo('<em>New Event Created ('.$name.') in scope ('.get_class($this->scope).')</em><br>');
  11
+    }
  12
+
  13
+    public function subscribe($func, $scope='') {
  14
+        $scope = ((is_object($scope)) ? $scope : $this->scope);
  15
+        echo('$scope: '.get_class($scope).'<br>');
  16
+        $this->subscribers[$func] =  array('function' => $func, 'scope' => $scope);
  17
+        echo('<em>Added Event to: '.$this->name.' ('.$func.')</em><br>');
  18
+    }
  19
+
  20
+    public function fire() {
  21
+        $passArgs = array($this->scope);
  22
+        $numArgs = func_num_args();
  23
+        echo('Number of arguments: '.$numArgs.'<br>');
  24
+        if ($numArgs) {
  25
+            $args = func_get_args();
  26
+            $passArgs = array_merge($passArgs, $args);
  27
+            for ($i = 0; $i <= $numArgs; $i++) {
  28
+                echo "Argument $i is: " . $passArgs[$i] . "<br />\n";
  29
+            }
  30
+        }
  31
+ 
  32
+        reset($this->subscribers);
  33
+        //echo('<pre>'.print_r($this, 1).'</pre>');
  34
+        foreach ($this->subscribers as $event) {
  35
+            echo('<em>Checking Fire Event: '.$event['function'].' in scope ('.get_class($event['scope']).')</em><br>');
  36
+            if (is_object($event['scope'])) {
  37
+                if (is_callable(array($event['scope'], $event['function']))) {
  38
+                    echo('<em>Fire Event: '.$event['function'].' in scope ('.get_class($event['scope']).')</em><br>');
  39
+                    call_user_func_array(array($event['scope'], $event['function']), $passArgs);
  40
+                }
  41
+            } else {
  42
+                if (is_callable($event['function'])) {
  43
+                    echo('<em>Fire Event: '.$event['function'].'</em><br>');
  44
+                    call_user_func_array($event['function'], $passArgs);
  45
+                }
  46
+            }
  47
+        }
  48
+    }
  49
+
  50
+    public function unsubscribe($func) {
  51
+        echo('<em>Event Removed: '.$func.'</em><br>');
  52
+        unset($this->subscribers[$func]);
  53
+    }
  54
+    
  55
+    public function unsubscribeAll() {
  56
+        reset($this->subscribers);
  57
+        foreach ($this->subscribers as $event) {
  58
+            echo('<em>Event Removed: '.$event['function'].'</em><br>');
  59
+            unset($this->subscribers[$event['function']]);
  60
+        }
  61
+    }
  62
+    
  63
+    public function __toString() {
  64
+        return 'Custom Event ['.$this->name.'] (subscribers: '.count($this->subscribers).')';
  65
+    }
  66
+}
  67
+
  68
+?>
36  index.php
... ...
@@ -0,0 +1,36 @@
  1
+<?php
  2
+@include('customevent.php');
  3
+@include('comp.php');
  4
+?>
  5
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  6
+<html>
  7
+<head>
  8
+    <title>YUI: CustomEvent ported to PHP.</title>
  9
+    <link rel="stylesheet" href="http://blog.davglass.com/files/yui/css/yuicss.css" type="text/css">
  10
+    <link rel="stylesheet" href="http://blog.davglass.com/wp-content/themes/davglass/style.css" type="text/css">
  11
+    <style type="text/css" media="screen">
  12
+        p, h2 {
  13
+            margin: 1em;
  14
+        }
  15
+	</style>
  16
+</head>
  17
+<body>
  18
+<div id="davdoc" class="yui-t7">
  19
+    <div id="hd"><h1 id="header"><a href="">YUI: CustomEvent ported to PHP.</a></h1></div>
  20
+    <div id="bd">
  21
+    <h2>YUI CustomEvent utility ported to PHP</h2>
  22
+    <p>Sources: <a href="source.php?file=1">index.php</a> -
  23
+    <a href="source.php?file=2">customevent.php</a> -
  24
+    <a href="source.php?file=3">comp.php</a></p>
  25
+
  26
+    <p>
  27
+        <strong>Start Execution</strong><br>
  28
+        <?php
  29
+            $comp = new Comp();
  30
+        ?>
  31
+    </p>
  32
+    </div>
  33
+    <div id="ft">&nbsp;</div>
  34
+</div>
  35
+</body>
  36
+</html>
52  source.php
... ...
@@ -0,0 +1,52 @@
  1
+<?php
  2
+
  3
+$files = array();
  4
+$files[1] = 'index.php';
  5
+$files[2] = 'customevent.php';
  6
+$files[3] = 'comp.php';
  7
+
  8
+$str_file = '';
  9
+
  10
+if ($files[$_GET['file']]) {
  11
+    $file = file('./'.$files[$_GET['file']]);
  12
+    while (list($na, $line) = each($file)) {
  13
+        $str_file .= $line;
  14
+    }
  15
+}
  16
+
  17
+?>
  18
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  19
+<html>
  20
+<head>
  21
+    <title>YUI: PHP Custom Events</title>
  22
+    <link rel="stylesheet" href="http://blog.davglass.com/files/yui/css/yuicss.css" type="text/css">
  23
+    <link rel="stylesheet" href="http://blog.davglass.com/wp-content/themes/davglass/style.css" type="text/css">
  24
+    <link rel="stylesheet" type="text/css" href="http://blog.davglass.com/files/yui/css/dpSyntaxHighlighter.css">
  25
+    <style type="text/css" media="screen">
  26
+        p {
  27
+            padding: .25em;
  28
+        }
  29
+	</style>
  30
+</head>
  31
+<body>
  32
+<div id="davdoc" class="yui-t7">
  33
+    <div id="hd"><h1 id="header"><a href="http://blog.davglass.com/">YUI: Custom Events</a></h1></div>
  34
+    <div id="bd">
  35
+    <p><a href="index.php">&lt; &lt; Back</a></p>
  36
+    <?php highlight_string($str_file); ?>
  37
+</div>
  38
+<div id="ft">&nbsp;</div>
  39
+</div>
  40
+    
  41
+<script type="text/javascript" src="../js/yui-012.js"></script>
  42
+<script src="http://blog.davglass.com/files/yui/js/dpSyntaxHighlighter.js"></script>
  43
+<script type="text/javascript">
  44
+function init() {
  45
+    dp.SyntaxHighlighter.HighlightAll('code'); 
  46
+}
  47
+
  48
+YAHOO.util.Event.addListener(window, 'load', init);
  49
+
  50
+</script>
  51
+</body>
  52
+</html>

0 notes on commit 1a6a07f

Please sign in to comment.
Something went wrong with that request. Please try again.