3
* Flight: An extensible micro-framework.
5
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
6
* @license MIT, http://flightphp.com/license
12
* The Loader class is responsible for loading objects. It maintains
13
* a list of reusable class instances and can generate a new class
14
* instances with custom initialization parameters. It also performs
23
protected $classes = array();
30
protected $instances = array();
33
* Autoload directories.
37
protected static $dirs = array();
42
* @param string $name Registry name
43
* @param string|callable $class Class name or function to instantiate class
44
* @param array $params Class initialization parameters
45
* @param callback $callback Function to call after object instantiation
47
public function register($name, $class, array $params = array(), $callback = null) {
48
unset($this->instances[$name]);
50
$this->classes[$name] = array($class, $params, $callback);
54
* Unregisters a class.
56
* @param string $name Registry name
58
public function unregister($name) {
59
unset($this->classes[$name]);
63
* Loads a registered class.
65
* @param string $name Method name
66
* @param bool $shared Shared instance
67
* @return object Class instance
69
public function load($name, $shared = true) {
72
if (isset($this->classes[$name])) {
73
list($class, $params, $callback) = $this->classes[$name];
75
$exists = isset($this->instances[$name]);
79
$this->getInstance($name) :
80
$this->newInstance($class, $params);
83
$this->instances[$name] = $obj;
87
$obj = $this->newInstance($class, $params);
90
if ($callback && (!$shared || !$exists)) {
92
call_user_func_array($callback, $ref);
100
* Gets a single instance of a class.
102
* @param string $name Instance name
103
* @return object Class instance
105
public function getInstance($name) {
106
return isset($this->instances[$name]) ? $this->instances[$name] : null;
110
* Gets a new instance of a class.
112
* @param string|callable $class Class name or callback function to instantiate class
113
* @param array $params Class initialization parameters
114
* @return object Class instance
116
public function newInstance($class, array $params = array()) {
117
if (is_callable($class)) {
118
return call_user_func_array($class, $params);
121
switch (count($params)) {
125
return new $class($params[0]);
127
return new $class($params[0], $params[1]);
129
return new $class($params[0], $params[1], $params[2]);
131
return new $class($params[0], $params[1], $params[2], $params[3]);
133
return new $class($params[0], $params[1], $params[2], $params[3], $params[4]);
135
$refClass = new \ReflectionClass($class);
136
return $refClass->newInstanceArgs($params);
141
* Resets the object to the initial state.
143
public function reset() {
144
$this->classes = array();
145
$this->instances = array();
148
/*** Autoloading Functions ***/
151
* Starts/stops autoloader.
153
* @param bool $enabled Enable/disable autoloading
154
* @param mixed $dirs Autoload directories
156
public static function autoload($enabled = true, $dirs = array()) {
158
spl_autoload_register(array(__CLASS__, 'loadClass'));
161
spl_autoload_unregister(array(__CLASS__, 'loadClass'));
165
self::addDirectory($dirs);
172
* @param string $class Class name
174
public static function loadClass($class) {
175
$class_file = str_replace(array('\\', '_'), '/', $class).'.php';
177
foreach (self::$dirs as $dir) {
178
$file = $dir.'/'.$class_file;
179
if (file_exists($file)) {
187
* Adds a directory for autoloading classes.
189
* @param mixed $dir Directory path
191
public static function addDirectory($dir) {
192
if (is_array($dir) || is_object($dir)) {
193
foreach ($dir as $value) {
194
self::addDirectory($value);
197
else if (is_string($dir)) {
198
if (!in_array($dir, self::$dirs)) self::$dirs[] = $dir;