]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Iterator.php
Merge in Phergie changes
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Iterator.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
9  * This source file is subject to the new BSD license that is bundled
10  * with this package in the file LICENSE.
11  * It is also available through the world-wide-web at this URL:
12  * http://phergie.org/license
13  *
14  * @category  Phergie
15  * @package   Phergie
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie
20  */
21
22 /**
23  * Implements a filtering iterator for limiting executing of methods across
24  * a group of plugins.
25  *
26  * @category Phergie
27  * @package  Phergie
28  * @author   Phergie Development Team <team@phergie.org>
29  * @license  http://phergie.org/license New BSD License
30  * @link     http://pear.phergie.org/package/Phergie
31  */
32 class Phergie_Plugin_Iterator extends FilterIterator
33 {
34     /**
35      * List of short names of plugins to exclude when iterating
36      *
37      * @var array
38      */
39     protected $plugins = array();
40
41     /**
42      * List of method names where plugins with these methods will be
43      * excluded when iterating
44      *
45      * @var array
46      */
47     protected $methods = array();
48
49     /**
50      * Adds to a list of plugins to exclude when iterating.
51      *
52      * @param mixed $plugins String containing the short name of a single
53      *        plugin to exclude or an array of short names of multiple
54      *        plugins to exclude
55      *
56      * @return Phergie_Plugin_Iterator Provides a fluent interface
57      */
58     public function addPluginFilter($plugins)
59     {
60         if (is_array($plugins)) {
61             $this->plugins = array_unique(
62                 array_merge($this->plugins, $plugins)
63             );
64         } else {
65             $this->plugins[] = $plugins;
66         }
67         return $this;
68     }
69
70     /**
71      * Adds to a list of method names where plugins defining these methods
72      * will be excluded when iterating.
73      *
74      * @param mixed $methods String containing the name of a single method
75      *        or an array containing the name of multiple methods
76      *
77      * @return Phergie_Plugin_Iterator Provides a fluent interface
78      */
79     public function addMethodFilter($methods)
80     {
81         if (is_array($methods)) {
82             $this->methods = array_merge($this->methods, $methods);
83         } else {
84             $this->methods[]= $methods;
85         }
86         return $this;
87     }
88
89     /**
90      * Clears any existing plugin and methods filters.
91      *
92      * @return Phergie_Plugin_Iterator Provides a fluent interface
93      */
94     public function clearFilters()
95     {
96         $this->plugins = array();
97         $this->methods = array();
98     }
99
100     /**
101      * Implements FilterIterator::accept().
102      *
103      * @return boolean TRUE to include the current item in those by returned
104      *         during iteration, FALSE otherwise
105      */
106     public function accept()
107     {
108         if (!$this->plugins && !$this->methods) {
109             return true;
110         }
111
112         $current = $this->current();
113
114         if (in_array($current->getName(), $this->plugins)) {
115             return false;
116         }
117
118         foreach ($this->methods as $method) {
119             if (method_exists($current, $method)) {
120                 return false;
121             }
122         }
123
124         return true;
125     }
126 }