]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Iterator.php
Merge branch '0.9.x' into 1.0.x
[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      * Overrides the parent constructor to reset the internal iterator's
51      * pointer to the current item, which the parent class errantly does not
52      * do.
53      *
54      * @param Iterator $iterator Iterator to filter
55      *
56      * @return void
57      * @link http://bugs.php.net/bug.php?id=52560
58      */
59     public function __construct(Iterator $iterator)
60     {
61         parent::__construct($iterator);
62         $this->rewind();
63     }
64
65     /**
66      * Adds to a list of plugins to exclude when iterating.
67      *
68      * @param mixed $plugins String containing the short name of a single
69      *        plugin to exclude or an array of short names of multiple
70      *        plugins to exclude
71      *
72      * @return Phergie_Plugin_Iterator Provides a fluent interface
73      */
74     public function addPluginFilter($plugins)
75     {
76         if (is_array($plugins)) {
77             $this->plugins = array_unique(
78                 array_merge($this->plugins, $plugins)
79             );
80         } else {
81             $this->plugins[] = $plugins;
82         }
83         return $this;
84     }
85
86     /**
87      * Adds to a list of method names where plugins defining these methods
88      * will be excluded when iterating.
89      *
90      * @param mixed $methods String containing the name of a single method
91      *        or an array containing the name of multiple methods
92      *
93      * @return Phergie_Plugin_Iterator Provides a fluent interface
94      */
95     public function addMethodFilter($methods)
96     {
97         if (is_array($methods)) {
98             $this->methods = array_merge($this->methods, $methods);
99         } else {
100             $this->methods[]= $methods;
101         }
102         return $this;
103     }
104
105     /**
106      * Clears any existing plugin and methods filters.
107      *
108      * @return Phergie_Plugin_Iterator Provides a fluent interface
109      */
110     public function clearFilters()
111     {
112         $this->plugins = array();
113         $this->methods = array();
114     }
115
116     /**
117      * Implements FilterIterator::accept().
118      *
119      * @return boolean TRUE to include the current item in those by returned
120      *         during iteration, FALSE otherwise
121      */
122     public function accept()
123     {
124         if (!$this->plugins && !$this->methods) {
125             return true;
126         }
127
128         $current = $this->current();
129
130         if (in_array($current->getName(), $this->plugins)) {
131             return false;
132         }
133
134         foreach ($this->methods as $method) {
135             if (method_exists($current, $method)) {
136                 return false;
137             }
138         }
139
140         return true;
141     }
142 }