http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 26 LOC: 410 Statements: 80

Source file Statements Methods Total coverage
Orm.php 97.5% 100.0% 98.1%
   
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: Orm.php 199 2008-01-19 21:54:40Z doublecompile $
15
 */
16
/**
17
 * @see Xyster_Orm_WorkUnit
18
 */
19 1
require_once 'Xyster/Orm/WorkUnit.php';
20
/**
21
 * @see Xyster_Orm_Manager
22
 */
23 1
require_once 'Xyster/Orm/Manager.php';
24
/**
25
 * The main front-end for the ORM package
26
 *
27
 * @category  Xyster
28
 * @package   Xyster_Orm
29
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
30
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
31
 */
32
class Xyster_Orm
33
{
34
    /**
35
     * The singleton instance of this class
36
     *
37
     * @var Xyster_Orm
38
     */
39
    static protected $_instance;
40
41
    /**
42
     * The orm manager
43
     *
44
     * @var Xyster_Orm_Manager
45
     */
46
    protected $_manager;
47
48
    /**
49
     * The "unit of work" to hold our pending transactions
50
     *
51
     * @var Xyster_Orm_WorkUnit
52
     */
53
    protected $_work;
54
55
    /**
56
     * Creates a new Xyster_Orm object, hide from userland
57
     */
58
    protected function __construct()
59
    {
60 1
        $this->_manager = new Xyster_Orm_Manager();
61
    }
62
63
    /**
64
     * Gets an instance of Xyster_Orm
65
     *
66
     * @return Xyster_Orm
67
     */
68
    static public function getInstance()
69
    {
70 15
        if (! self::$_instance instanceof self ) {
71 1
            self::$_instance = new self;
72 1
        }
73 15
        return self::$_instance;
74
    }
75
76
    /**
77
     * Abandons the current session
78
     *
79
     * This will unload the repository and unset the singleton instance.  The
80
     * next call to {@link getInstance} will return a new session.
81
     */
82
    public function clear()
83
    {
84 1
        $this->_manager->clear();
85 1
        $this->_getWorkUnit()->rollback();
86 1
        self::$_instance = null;
87
    }
88
89
    /**
90
     * Commits pending operations to the data store
91
     *
92
     */
93
    public function commit()
94
    {
95 1
        $wu = $this->_getWorkUnit();
96 1
        $repo = $this->_getRepository();
97
98 1
        foreach( $repo->getClasses() as $class ) {
99 1
            foreach( $repo->getAll($class) as $entity ) {
100 1
                if ( $entity->isDirty() ) {
101
                    try {
102 1
                        $wu->registerDirty($entity);
103 1
                    } catch ( Xyster_Orm_Exception $thrown ) {
104
                        // do nothing - the entity was pending delete
105
                    }
106 1
                }
107 1
            }
108 1
        }
109
110 1
        $wu->commit($this->_manager);
111
    }
112
113
    /**
114
     * Gets the first entity found matching a set of criteria
115
     *
116
     * @param string $className
117
     * @param array $criteria
118
     * @return Xyster_Orm_Entity The entity found or null if none
119
     */
120
    public function find( $className, array $criteria )
121
    {
122 1
        return $this->_manager->find($className, $criteria);
123
    }
124
125
    /**
126
     * Finds all entities matching a given criteria
127
     *
128
     * @param string $className
129
     * @param mixed $criteria {@link Xyster_Data_Criterion} or associative array
130
     * @param mixed $sorts Array of {@link Xyster_Data_Sort} objects
131
     */
132
    public function findAll( $className, $criteria, $sorts = null )
133
    {
134 1
        return $this->_manager->findAll($className, $criteria, $sorts);
135
    }
136
137
    /**
138
     * Gets an entity by class and primary key
139
     *
140
     * @param string $className
141
     * @param mixed $id
142
     * @return Xyster_Orm_Entity
143
     */
144
    public function get( $className, $id )
145
    {
146 5
        return $this->_manager->get($className, $id);
147
    }
148
149
    /**
150
     * Gets all entities from the data source or a subset if given the keys
151
     *
152
     * @param string $className
153
     * @param array $ids
154
     * @return Xyster_Orm_Set
155
     */
156
    public function getAll( $className, array $ids = null )
157
    {
158 1
        return $this->_manager->getAll($className, $ids);
159
    }
160
161
    /**
162
     * Gets the factory for entity mappers
163
     *
164
     * @return Xyster_Orm_Mapper_Factory_Interface
165
     */
166
    public function getMapperFactory()
167
    {
168 15
        return $this->_manager->getMapperFactory();
169
    }
170
171
    /**
172
     * Gets an entity by class and primary key, throws exception if not found
173
     *
174
     * @param string $className
175
     * @param mixed $id
176
     * @return Xyster_Orm_Entity
177
     * @throws Xyster_Orm_Exception
178
     */
179
    public function getOrFail( $className, $id )
180
    {
181 1
        $entity = $this->get($className, $id);
182 1
        if ( $entity instanceof Xyster_Orm_Entity ) {
183 1
            return $entity;
184 0
        }
185
186 1
        $printId = $id;
187 1
        if ( is_array($id) ) {
188 1
            $printId = print_r($id, true);
189 1
        }
190 1
        require_once 'Xyster/Orm/Exception.php';
191 1
        throw new Xyster_Orm_Exception("The '" . $className . "' with ID ''"
192 1
            . $printId . "' was not found");
193
    }
194
195
    /**
196
     * Retrieve a plugin or plugins by class
197
     *
198
     * @param  string $class Class name of plugin(s) desired
199
     * @return Xyster_Orm_Plugin_Abstract|array|false False if none, the plugin if only one, and array of plugins if multiple of same class
200
     */
201
    public function getPlugin( $class )
202
    {
203 1
        return $this->_manager->getPluginBroker()->getPlugin($class);
204
    }
205
206
    /**
207
     * Retrieve all plugins
208
     *
209
     * @return array
210
     */
211
    public function getPlugins()
212
    {
213 1
        return $this->_manager->getPluginBroker()->getPlugins();
214
    }
215
216
    /**
217
     * Gets the secondary cache for storing entities
218
     *
219
     * @return Zend_Cache_Core
220
     */
221
    public function getSecondaryCache()
222
    {
223 2
        return $this->_manager->getSecondaryCache();
224
    }
225
226
    /**
227
     * Checks whether a plugin of a particular class is registered
228
     *
229
     * @param string $class
230
     * @return boolean
231
     */
232
    public function hasPlugin( $class )
233
    {
234 1
        return $this->_manager->getPluginBroker()->hasPlugin($class);
235
    }
236
237
    /**
238
     * Sets an entity to be added to the data store
239
     *
240
     * @param Xyster_Orm_Entity $entity
241
     * @throws Xyster_Orm_Exception if the entity is already persisted
242
     */
243
    public function persist( Xyster_Orm_Entity $entity )
244
    {
245 2
        $key = $entity->getPrimaryKey(true);
246 2
        if ( count($key) && current($key) ) {
247 1
            require_once 'Xyster/Orm/Exception.php';
248 1
            throw new Xyster_Orm_Exception('This entity is already persisted');
249 0
        }
250 1
        $this->_getWorkUnit()->registerNew($entity);
251
    }
252
253
    /**
254
     * Creates a query object to return entities
255
     *
256
     * @param string $className The entity class name
257
     * @param string $xsql The XSQL expression to use
258
     * @return Xyster_Orm_Query The query object
259
     */
260
    public function query( $className, $xsql = null )
261
    {
262 1
        require_once 'Xyster/Orm/Query.php';
263 1
        require_once 'Xyster/Orm/Query/Parser.php';
264
265 1
        $query = new Xyster_Orm_Query($className, $this->_manager);
266
267 1
        if ( $xsql ) {
268 1
            $parser = new Xyster_Orm_Query_Parser($this->getMapperFactory());
269 1
            $parser->parseQuery($query, $xsql);
270 1
        }
271
272 1
        return $query;
273
    }
274
275
    /**
276
     * Refreshes the values of an entity
277
     */
278
    public function refresh( Xyster_Orm_Entity $entity )
279
    {
280 1
        $this->_manager->refresh($entity);
281
    }
282
283
    /**
284
     * Register a plugin
285
     *
286
     * @param Xyster_Orm_Plugin_Abstract $plugin
287
     * @param int $stackIndex
288
     * @return Xyster_Orm provides a fluent interface
289
     */
290
    public function registerPlugin( Xyster_Orm_Plugin_Abstract $plugin, $stackIndex = null )
291
    {
292 3
        $this->_manager->getPluginBroker()->registerPlugin($plugin, $stackIndex);
293 3
        return $this;
294
    }
295
296
    /**
297
     * Sets an entity to be removed
298
     *
299
     * @param Xyster_Orm_Entity $entity
300
     */
301
    public function remove( Xyster_Orm_Entity $entity )
302
    {
303 1
        $this->_getWorkUnit()->registerRemoved($entity);
304
    }
305
306
    /**
307
     * Creates a report query object to return a data set
308
     *
309
     * @param string $className The entity class name
310
     * @param string $xsql The XSQL expression to use
311
     * @return Xyster_Orm_Query_Report The report query object
312
     */
313
    public function reportQuery( $className, $xsql = null )
314
    {
315 1
        require_once 'Xyster/Orm/Query/Report.php';
316 1
        require_once 'Xyster/Orm/Query/Parser.php';
317
318 1
        $query = new Xyster_Orm_Query_Report($className, $this->_manager);
319
320 1
        if ( $xsql ) {
321 1
            $parser = new Xyster_Orm_Query_Parser($this->getMapperFactory());
322 1
            $parser->parseReportQuery($query, $xsql);
323 1
        }
324
325 1
        return $query;
326
    }
327
328
    /**
329
     * Sets the factory for entity mappers
330
     *
331
     * @param Xyster_Orm_Mapper_Factory_Interface $mapFactory
332
     * @return Xyster_Orm provides a fluent interface
333
     */
334
    public function setMapperFactory( Xyster_Orm_Mapper_Factory_Interface $mapFactory )
335
    {
336 15
        $this->_manager->setMapperFactory($mapFactory);
337 15
        return $this;
338
    }
339
340
    /**
341
     * Sets the secondary cache for storing entities
342
     *
343
     * If $cache is null, then no secondary cache is used.
344
     *
345
     * @param mixed $cache Either a Cache object, or a string naming a Registry key
346
     * @return Xyster_Orm provides a fluent interface
347
     */
348
    public function setSecondaryCache($cache = null)
349
    {
350 15
        $this->_manager->setSecondaryCache($cache);
351 15
        return $this;
352
    }
353
354
    /**
355
     * Makes sure all classes and metadata are defined for a type of entity
356
     *
357
     * This method should be called if you want to instantiate a new, blank
358
     * type of entity and you haven't retrieved any from the data store.
359
     *
360
     * For instance, if you've used {@link findAll} to pull out a set of
361
     * entities, the classes should be defined and the metadata should be
362
     * loaded.  If you haven't done any interaction with the backend yet, it's
363
     * necessary to call this method.
364
     *
365
     * @param string $className
366
     */
367
    public function setup( $className )
368
    {
369
        // this will load the 'entity' and 'mapper' classes, as well as look up
370
        // the metadata
371 15
        $map = $this->getMapperFactory()->get($className);
372
        // this will load the 'set' class
373 15
        $map->getSet();
374
    }
375
376
    /**
377
     * Unregister a plugin.
378
     *
379
     * @param string|Xyster_Orm_Plugin_Abstract $plugin Plugin object or class name
380
     * @return Xyster_Orm provides a fluent interface
381
     */
382
    public function unregisterPlugin( $plugin )
383
    {
384 1
        $this->_manager->getPluginBroker()->unregisterPlugin($plugin);
385 1
        return $this;
386
    }
387
388
    /**
389
     * Gets the entity repository
390
     *
391
     * @return Xyster_Orm_Repository
392
     */
393
    protected function _getRepository()
394
    {
395 1
        return $this->_manager->getRepository();
396
    }
397
398
    /**
399
     * Gets the work unit
400
     *
401
     * @return Xyster_Orm_WorkUnit
402
     */
403
    protected function _getWorkUnit()
404
    {
405 2
        if ( !$this->_work ) {
406 2
            $this->_work = new Xyster_Orm_WorkUnit();
407 2
        }
408 2
        return $this->_work;
409
    }
410
}


Report generated at 2008-01-20T12:13:39-05:00