http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 12 LOC: 237 Statements: 77

Source file Statements Methods Total coverage
Broker.php 94.8% 100.0% 95.5%
   
1
<?php
2
/**
3
 * Xyster Framework
4
 *
5
 * This source file is subject to the new BSD license that is bundled
6
 * with this package in the file LICENSE.txt.
7
 * It is also available through the world-wide-web at this URL:
8
 * http://www.opensource.org/licenses/bsd-license.php
9
 *
10
 * @category  Xyster
11
 * @package   Xyster_Orm
12
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
13
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
14
 * @version   $Id: Broker.php 202 2008-01-20 16:20:09Z doublecompile $
15
 */
16
/**
17
 * A broker for plugins
18
 *
19
 * @category  Xyster
20
 * @package   Xyster_Orm
21
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
22
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
23
 */
24
class Xyster_Orm_Plugin_Broker
25
{
26
    /**
27
     * Array of Xyster_Orm_Plugin_Abstract objects
28
     *
29
     * @var array
30
     */
31
    protected $_plugins = array();
32
33
    /**
34
     * Retrieve a plugin or plugins by class
35
     *
36
     * @param  string $class Class name of plugin(s) desired
37
     * @return Xyster_Orm_Plugin_Abstract|array|false False if none, the plugin if only one, and array of plugins if multiple of same class
38
     */
39
    public function getPlugin( $class )
40
    {
41 2
        $found = array();
42 2
        foreach ($this->_plugins as $plugin) {
43 2
            if ( $class == get_class($plugin) ) {
44 2
                $found[] = $plugin;
45 2
            }
46 2
        }
47
48 2
        switch (count($found)) {
49 2
            case 0:
50 1
                return false;
51 2
            case 1:
52 2
                return $found[0];
53 1
            default:
54 1
        }
55
56 1
        return $found;
57
    }
58
59
    /**
60
     * Retrieve all plugins
61
     *
62
     * @return array
63
     */
64
    public function getPlugins()
65
    {
66 2
        return $this->_plugins;
67
    }
68
69
    /**
70
     * Checks whether a plugin of a particular class is registered
71
     *
72
     * @param string $class
73
     * @return boolean
74
     */
75
    public function hasPlugin( $class )
76
    {
77 4
        foreach ($this->_plugins as $plugin) {
78 4
            if ( $class == get_class($plugin) ) {
79 4
                return true;
80 0
            }
81 2
        }
82
83 4
        return false;
84
    }
85
86
    /**
87
     * Called prior to an entity being deleted
88
     *
89
     * @param Xyster_Orm_Entity $entity
90
     */
91
    public function postDelete( Xyster_Orm_Entity $entity )
92
    {
93 14
        foreach( $this->_plugins as $plugin ) {
94
            /* @var $plugin Xyster_Orm_Plugin_Abstract */
95 2
            $plugin->postDelete($entity);
96 2
        }
97
    }
98
99
    /**
100
     * Called prior to an entity being inserted
101
     *
102
     * @param Xyster_Orm_Entity $entity
103
     */
104
    public function postInsert( Xyster_Orm_Entity $entity )
105
    {
106 17
        foreach( $this->_plugins as $plugin ) {
107
            /* @var $plugin Xyster_Orm_Plugin_Abstract */
108 2
            $plugin->postInsert($entity);
109 2
        }
110
    }
111
112
    /**
113
     * Called after a new entity is loaded with values
114
     *
115
     * @param Xyster_Orm_Entity $entity
116
     */
117
    public function postLoad( Xyster_Orm_Entity $entity )
118
    {
119 104
        foreach( $this->_plugins as $plugin ) {
120
            /* @var $plugin Xyster_Orm_Plugin_Abstract */
121 4
            $plugin->postLoad($entity);
122 4
        }
123
    }
124
125
    /**
126
     * Called prior to an entity being updated
127
     *
128
     * @param Xyster_Orm_Entity $entity
129
     */
130
    public function postUpdate( Xyster_Orm_Entity $entity )
131
    {
132 30
        foreach( $this->_plugins as $plugin ) {
133
            /* @var $plugin Xyster_Orm_Plugin_Abstract */
134 2
            $plugin->postUpdate($entity);
135 2
        }
136
    }
137
138
    /**
139
     * Called prior to an entity being deleted
140
     *
141
     * @param Xyster_Orm_Entity $entity
142
     */
143
    public function preDelete( Xyster_Orm_Entity $entity )
144
    {
145 14
        foreach( $this->_plugins as $plugin ) {
146
            /* @var $plugin Xyster_Orm_Plugin_Abstract */
147 2
            $plugin->preDelete($entity);
148 2
        }
149
    }
150
151
    /**
152
     * Called prior to an entity being inserted
153
     *
154
     * @param Xyster_Orm_Entity $entity
155
     */
156
    public function preInsert( Xyster_Orm_Entity $entity )
157
    {
158 17
        foreach( $this->_plugins as $plugin ) {
159
            /* @var $plugin Xyster_Orm_Plugin_Abstract */
160 2
            $plugin->preInsert($entity);
161 2
        }
162
    }
163
164
    /**
165
     * Called prior to an entity being updated
166
     *
167
     * @param Xyster_Orm_Entity $entity
168
     */
169
    public function preUpdate( Xyster_Orm_Entity $entity )
170
    {
171 30
        foreach( $this->_plugins as $plugin ) {
172
            /* @var $plugin Xyster_Orm_Plugin_Abstract */
173 2
            $plugin->preUpdate($entity);
174 2
        }
175
    }
176
177
    /**
178
     * Register a plugin
179
     *
180
     * @param Xyster_Orm_Plugin_Abstract $plugin
181
     * @param int $stackIndex
182
     * @return Xyster_Orm_Plugin_Broker provides a fluent interface
183
     */
184
    public function registerPlugin( Xyster_Orm_Plugin_Abstract $plugin, $stackIndex = null )
185
    {
186 22
        if ( in_array($plugin, $this->_plugins, true) ) {
187 1
            require_once 'Xyster/Orm/Exception.php';
188 1
            throw new Xyster_Orm_Exception('Plugin already registered');
189 0
        }
190
191 22
        $stackIndex = (int) $stackIndex;
192
193 22
        if ($stackIndex) {
194 2
            if ( isset($this->_plugins[$stackIndex]) ) {
195 1
                require_once 'Xyster/Orm/Exception.php';
196 1
                throw new Xyster_Orm_Exception('Plugin with stackIndex "' . $stackIndex . '" already registered');
197 0
            }
198 2
            $this->_plugins[$stackIndex] = $plugin;
199 2
        } else {
200 22
            $stackIndex = count($this->_plugins);
201 22
            while ( isset($this->_plugins[$stackIndex]) ) {
202 1
                ++$stackIndex;
203 1
            }
204 22
            $this->_plugins[$stackIndex] = $plugin;
205
        }
206
207 22
        ksort($this->_plugins);
208
209 22
        return $this;
210
    }
211
212
    /**
213
     * Unregister a plugin.
214
     *
215
     * @param string|Xyster_Orm_Plugin_Abstract $plugin Plugin object or class name
216
     * @return Xyster_Orm_Plugin_Broker provides a fluent interface
217
     */
218
    public function unregisterPlugin( $plugin )
219
    {
220 5
        if ( $plugin instanceof Xyster_Orm_Plugin_Abstract ) {
221 4
            $key = array_search($plugin, $this->_plugins, true);
222 4
            if ( $key === false ) {
223 1
                require_once 'Xyster/Orm/Exception.php';
224 1
                throw new Xyster_Orm_Exception('Plugin not registered');
225 0
            }
226 3
            unset($this->_plugins[$key]);
227 4
        } else if ( is_string($plugin) ) {
228 1
            foreach( $this->_plugins as $key => $_plugin ) {
229 1
                $type = get_class($_plugin);
230 1
                if ($plugin == $type) {
231 1
                    unset($this->_plugins[$key]);
232 1
                }
233 1
            }
234 1
        }
235 4
        return $this;
236
    }
237
}


Report generated at 2008-03-05T18:27:43-05:00