http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 21 LOC: 359 Statements: 74

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


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