http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 5 LOC: 154 Statements: 46

Source file Statements Methods Total coverage
Loader.php 84.8% 100.0% 86.3%
   
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: Loader.php 202 2008-01-20 16:20:09Z doublecompile $
15
 */
16
/**
17
 * @see Zend_Loader
18
 */
19 1
require_once 'Zend/Loader.php';
20
/**
21
 * The entity/mapper/collection class loader for the Orm layer
22
 *
23
 * @category  Xyster
24
 * @package   Xyster_Orm
25
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
26
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
27
 */
28
class Xyster_Orm_Loader
29
{
30
    /**
31
     * Paths where entities, mappers, and sets are stored
32
     *
33
     * @var array
34
     */
35
    static protected $_paths = array();
36
37
    /**
38
     * Adds a path to where class files for entities can be found
39
     *
40
     * @param string $path
41
     */
42
    public static function addPath( $path )
43
    {
44 299
        $path        = rtrim($path, '/');
45 299
        $path        = rtrim($path, '\\');
46 299
        $path       .= DIRECTORY_SEPARATOR;
47
48 299
        if ( @!is_dir($path) ) {
49 1
            require_once 'Xyster/Orm/Exception.php';
50 1
            throw new Xyster_Orm_Exception("The path '$path' does not exist'");
51
        }
52
53 299
        self::$_paths[$path] = $path; // no need for dups
54
    }
55
56
    /**
57
     * Tries to load the class in one of the paths defined for entities
58
     *
59
     * @param string $className
60
     * @return string the class name loaded
61
     * @throws Xyster_Orm_Exception if the class file cannot be loaded
62
     * @throws Xyster_Orm_Exception if the file is found but no class with the name given is defined
63
     */
64
    public static function loadClass( $className )
65
    {
66 293
        if ( class_exists($className, false) ) {
67 291
            return $className;
68 0
        }
69
70 8
        $dirs = self::$_paths;
71 8
        $file = $className . '.php';
72
73
        try {
74 8
            require_once 'Zend/Loader.php';
75 8
            Zend_Loader::loadFile($file, $dirs, true);
76 8
        } catch (Zend_Exception $e) {
77 0
            require_once 'Xyster/Orm/Exception.php';
78 0
            throw new Xyster_Orm_Exception('Cannot load class "' . $className . '"');
79
        }
80
81 8
        if (!class_exists($className,false)) {
82 1
            require_once 'Xyster/Orm/Exception.php';
83 1
            throw new Xyster_Orm_Exception('Invalid class ("' . $className . '")');
84 0
        }
85
86 7
        return $className;
87
    }
88
89
    /**
90
     * Loads the class and makes sure it's a Xyster_Orm_Entity
91
     *
92
     * @param string $className
93
     * @throws Xyster_Orm_Exception if the class loaded is not a derivitive of Xyster_Orm_Entity
94
     */
95
    public static function loadEntityClass( $className )
96
    {
97 290
        self::loadClass($className);
98
99 290
        if (!($className instanceof Xyster_Orm_Entity) &&
100 290
            !is_subclass_of($className, 'Xyster_Orm_Entity')) {
101 1
            require_once 'Xyster/Orm/Exception.php';
102 1
            throw new Xyster_Orm_Exception("'" . $className . "' is not a subclass of Xyster_Orm_Entity");
103 0
        }
104
    }
105
106
    /**
107
     * Loads the mapper class for the entity class given
108
     *
109
     * To load the 'PersonMapper' class, the $className parameter should just be
110
     * 'Person'.
111
     *
112
     * @param string $className the name of the entity class
113
     * @return string the class name
114
     * @throws Xyster_Orm_Exception if the class loaded is not a derivitive of Xyster_Orm_Mapper_Interface
115
     */
116
    public static function loadMapperClass( $className )
117
    {
118 288
        self::loadEntityClass($className);
119
120 288
        $mapper = $className . 'Mapper';
121
122 288
        self::loadClass($mapper);
123
124 288
        if (!($mapper instanceof Xyster_Orm_Mapper_Interface) &&
125 288
            !is_subclass_of($mapper, 'Xyster_Orm_Mapper_Abstract')) {
126 1
            require_once 'Xyster/Orm/Exception.php';
127 1
            throw new Xyster_Orm_Exception("'" . $mapper . "' is not a subclass of Xyster_Orm_Mapper_Interface");
128 0
        }
129
130 287
        return $mapper;
131
    }
132
133
    /**
134
     * Loads the set class for the entity class given
135
     *
136
     * @param string $className the name of the set class
137
     * @param boolean $autoSuffix whether to append 'Set' to the end of the class
138
     * @throws Xyster_Orm_Exception if the class loaded is not a derivitive of Xyster_Orm_Set
139
     * @return string the class name
140
     */
141
    public static function loadSetClass( $className, $autoSuffix = true )
142
    {
143 288
        $set = ( $autoSuffix ) ? $className . 'Set' : $className;
144 288
        self::loadClass($set);
145
146 288
        if (!($set instanceof Xyster_Orm_Set) &&
147 288
            !is_subclass_of($set, 'Xyster_Orm_Set')) {
148 1
            require_once 'Xyster/Orm/Exception.php';
149 1
            throw new Xyster_Orm_Exception("'" . $set . "' is not a subclass of Xyster_Orm_Set");
150 0
        }
151
152 287
        return $set;
153
    }
154
}


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