summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Phergie/Plugin/Iterator.php
blob: d6109629865bba99e8f26d107c77322d1e90f5a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
 * Phergie
 *
 * PHP version 5
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://phergie.org/license
 *
 * @category  Phergie
 * @package   Phergie
 * @author    Phergie Development Team <team@phergie.org>
 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
 * @license   http://phergie.org/license New BSD License
 * @link      http://pear.phergie.org/package/Phergie
 */

/**
 * Implements a filtering iterator for limiting executing of methods across
 * a group of plugins.
 *
 * @category Phergie
 * @package  Phergie
 * @author   Phergie Development Team <team@phergie.org>
 * @license  http://phergie.org/license New BSD License
 * @link     http://pear.phergie.org/package/Phergie
 */
class Phergie_Plugin_Iterator extends FilterIterator
{
    /**
     * List of short names of plugins to exclude when iterating
     *
     * @var array
     */
    protected $plugins = array();

    /**
     * List of method names where plugins with these methods will be
     * excluded when iterating
     *
     * @var array
     */
    protected $methods = array();

    /**
     * Overrides the parent constructor to reset the internal iterator's
     * pointer to the current item, which the parent class errantly does not
     * do.
     *
     * @param Iterator $iterator Iterator to filter
     *
     * @return void
     * @link http://bugs.php.net/bug.php?id=52560
     */
    public function __construct(Iterator $iterator)
    {
        parent::__construct($iterator);
        $this->rewind();
    }

    /**
     * Adds to a list of plugins to exclude when iterating.
     *
     * @param mixed $plugins String containing the short name of a single
     *        plugin to exclude or an array of short names of multiple
     *        plugins to exclude
     *
     * @return Phergie_Plugin_Iterator Provides a fluent interface
     */
    public function addPluginFilter($plugins)
    {
        if (is_array($plugins)) {
            $this->plugins = array_unique(
                array_merge($this->plugins, $plugins)
            );
        } else {
            $this->plugins[] = $plugins;
        }
        return $this;
    }

    /**
     * Adds to a list of method names where plugins defining these methods
     * will be excluded when iterating.
     *
     * @param mixed $methods String containing the name of a single method
     *        or an array containing the name of multiple methods
     *
     * @return Phergie_Plugin_Iterator Provides a fluent interface
     */
    public function addMethodFilter($methods)
    {
        if (is_array($methods)) {
            $this->methods = array_merge($this->methods, $methods);
        } else {
            $this->methods[]= $methods;
        }
        return $this;
    }

    /**
     * Clears any existing plugin and methods filters.
     *
     * @return Phergie_Plugin_Iterator Provides a fluent interface
     */
    public function clearFilters()
    {
        $this->plugins = array();
        $this->methods = array();
    }

    /**
     * Implements FilterIterator::accept().
     *
     * @return boolean TRUE to include the current item in those by returned
     *         during iteration, FALSE otherwise
     */
    public function accept()
    {
        if (!$this->plugins && !$this->methods) {
            return true;
        }

        $current = $this->current();

        if (in_array($current->getName(), $this->plugins)) {
            return false;
        }

        foreach ($this->methods as $method) {
            if (method_exists($current, $method)) {
                return false;
            }
        }

        return true;
    }
}