My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

require_once 'Panda/Loader/Interface.php';
require_once 'Panda/Configurable/Interface.php';

/**
* A generic source code loader
*
* @package Panda_Loader
* @author Michael Girouard (mikeg@lovemikeg.com)
* @license The New BSD License (http://pandaphp.org/license.html)
*/
class Panda_Loader_Abstract
implements Panda_Loader_Interface,
Panda_Configurable_Interface
{
/**
* The namespace to use while loading source
*
* @var string
*/
protected $namespace;

/**
* The base directory
*
* @var string
*/
protected $baseDir;

/**
* Configures the loader
*
* Configurable values are: <br />
* [namespace] string The namespace to use <br />
* [baseDir] string The base directory to use <br />
* [load] string|array A class or array of classes to automatically load <br />
*
* @param array $configuration
*/
public function configure(array $configuration = array())
{
if (array_key_exists('namespace', $configuration)) {
$this->setNamespace($configuration['namespace']);
}

if (array_key_exists('baseDir', $configuration)) {
$this->setBaseDir($configuration['baseDir']);
}

if (array_key_exists('load', $configuration)) {
if (is_string($configuration['load'])) {
$this->load($configuration['load']);
}
elseif (is_array($configuration['load'])) {
foreach ($configuration['load'] as $className) {
$this->load($className);
}
}
}
}

/**
* Returns the current namespace
*
* @return string
*/
public function getNamespace()
{
return $this->namespace;
}

/**
* Sets the namespace
*
* @param string $namespace
*/
public function setNamespace($namespace)
{
$this->namespace = $namespace;
}

/**
* Returns the base directory to load from
*
* @return string
*/
public function getBaseDir()
{
return $this->baseDir;
}

/**
* Sets the base directory to load from
*
* @param string $baseDir
*/
public function setBaseDir($baseDir)
{
$this->baseDir = $baseDir;
}

/**
* Returns a fully namespaced class name
*
* @param string $className
* @return string
*/
public function getClassName($className)
{
// If it's already namespaced, just return it back out
if (preg_match("/^(Panda|$this->namespace)/i", $className)) {
return $className;
}

return sprintf('%s_%s', $this->namespace, $className);
}

/**
* Returns a full path and file name of the given class
*
* @param string $className
* @return string
*/
public function getFileName($className)
{
$className = $this->getClassName($className);

return sprintf('%s%s%s.php',
$this->baseDir,
DIRECTORY_SEPARATOR,
str_replace('_', DIRECTORY_SEPARATOR, $className)
);
}

/**
* Loads a class file in from a class name
*
* @param string $className
* @return boolean True if the file was loaded, false otherwise.
*/
public function load($className)
{
$fileName = $this->getFileName($className);

if (is_file($fileName)) {
require_once $fileName;
return true;
}
else {
return false;
}
}

/**
* Loads a class file and returns an instance from the provided class name
*
* @param string $className
* @param array $parameters
* @param object The instance of the class
*/
public function loadInstance($className, array $parameters = array())
{
if ($this->load($className)) {
$Class = new ReflectionClass( $this->getClassName($className) );

if ($Class->hasMethod('__construct')) {
return $Class->newInstanceArgs($parameters);
}
else {
return $Class->newInstance();
}
}
}
}
Show details Hide details

Change log

r127 by mgirouard on Aug 03, 2008   Diff
Added line breaks to help generated
documentation.
Go to: 
Project members, sign in to write a code review

Older revisions

r125 by mgirouard on Aug 03, 2008   Diff
Copied configure method from the
incubator.
r92 by mgirouard on Apr 28, 2008   Diff
Updated license in docblocks and
cleaned up the require.
r76 by mgirouard on Apr 23, 2008   Diff
Moving loader files into new root.
All revisions of this file

File info

Size: 4250 bytes, 175 lines
Hosted by Google Code