]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/event.php
Introduced common_location_shared() to check if location sharing is always,
[quix0rs-gnu-social.git] / lib / event.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * utilities for defining and running event handlers
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Event
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2008 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Class for events
36  *
37  * This "class" two static functions for managing events in the StatusNet code.
38  *
39  * @category Event
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  *
45  * @todo     Define a system for using Event instances
46  */
47
48 class Event {
49
50     /* Global array of hooks, mapping eventname => array of callables */
51
52     protected static $_handlers = array();
53
54     /**
55      * Add an event handler
56      *
57      * To run some code at a particular point in StatusNet processing.
58      * Named events include receiving an XMPP message, adding a new notice,
59      * or showing part of an HTML page.
60      *
61      * The arguments to the handler vary by the event. Handlers can return
62      * two possible values: false means that the event has been replaced by
63      * the handler completely, and no default processing should be done.
64      * Non-false means successful handling, and that the default processing
65      * should succeed. (Note that this only makes sense for some events.)
66      *
67      * Handlers can also abort processing by throwing an exception; these will
68      * be caught by the closest code and displayed as errors.
69      *
70      * @param string   $name    Name of the event
71      * @param callable $handler Code to run
72      *
73      * @return void
74      */
75
76     public static function addHandler($name, $handler) {
77         if (array_key_exists($name, Event::$_handlers)) {
78             Event::$_handlers[$name][] = $handler;
79         } else {
80             Event::$_handlers[$name] = array($handler);
81         }
82     }
83
84     /**
85      * Handle an event
86      *
87      * Events are any point in the code that we want to expose for admins
88      * or third-party developers to use.
89      *
90      * We pass in an array of arguments (including references, for stuff
91      * that can be changed), and each assigned handler gets run with those
92      * arguments. Exceptions can be thrown to indicate an error.
93      *
94      * @param string $name Name of the event that's happening
95      * @param array  $args Arguments for handlers
96      *
97      * @return boolean flag saying whether to continue processing, based
98      *                 on results of handlers.
99      */
100
101     public static function handle($name, array $args=array()) {
102         $result = null;
103         if (array_key_exists($name, Event::$_handlers)) {
104             foreach (Event::$_handlers[$name] as $handler) {
105                 $result = call_user_func_array($handler, $args);
106                 if ($result === false) {
107                     break;
108                 }
109             }
110         }
111         return ($result !== false);
112     }
113
114     /**
115      * Check to see if an event handler exists
116      *
117      * Look to see if there's any handler for a given event, or narrow
118      * by providing the name of a specific plugin class.
119      *
120      * @param string $name Name of the event to look for
121      * @param string $plugin Optional name of the plugin class to look for
122      *
123      * @return boolean flag saying whether such a handler exists
124      *
125      */
126
127     public static function hasHandler($name, $plugin=null) {
128         if (array_key_exists($name, Event::$_handlers)) {
129             if (isset($plugin)) {
130                 foreach (Event::$_handlers[$name] as $handler) {
131                     if (get_class($handler[0]) == $plugin) {
132                         return true;
133                     }
134                 }
135             } else {
136                 return true;
137             }
138         }
139         return false;
140     }
141
142     public static function getHandlers($name)
143     {
144         return Event::$_handlers[$name];
145     }
146
147     /**
148      * Disables any and all handlers that have been set up so far;
149      * use only if you know it's safe to reinitialize all plugins.
150      */
151     public static function clearHandlers() {
152         Event::$_handlers = array();
153     }
154 }