http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 12 LOC: 242 Statements: 77

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


Report generated at 2007-10-08T19:32:23-05:00