fof.php
2.42 KB
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
<?php
/**
* @package FrameworkOnFramework
* @subpackage autoloader
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
* @license GNU General Public License version 2, or later
*/
defined('FOF_INCLUDED') or die();
/**
* The main class autoloader for FOF itself
*
* @package FrameworkOnFramework
* @subpackage autoloader
* @since 2.1
*/
class FOFAutoloaderFof
{
/**
* An instance of this autoloader
*
* @var FOFAutoloaderFof
*/
public static $autoloader = null;
/**
* The path to the FOF root directory
*
* @var string
*/
public static $fofPath = null;
/**
* Initialise this autoloader
*
* @return FOFAutoloaderFof
*/
public static function init()
{
if (self::$autoloader == null)
{
self::$autoloader = new self;
}
return self::$autoloader;
}
/**
* Public constructor. Registers the autoloader with PHP.
*/
public function __construct()
{
self::$fofPath = realpath(__DIR__ . '/../');
spl_autoload_register(array($this,'autoload_fof_core'));
}
/**
* The actual autoloader
*
* @param string $class_name The name of the class to load
*
* @return void
*/
public function autoload_fof_core($class_name)
{
// Make sure the class has a FOF prefix
if (substr($class_name, 0, 3) != 'FOF')
{
return;
}
// Remove the prefix
$class = substr($class_name, 3);
// Change from camel cased (e.g. ViewHtml) into a lowercase array (e.g. 'view','html')
$class = preg_replace('/(\s)+/', '_', $class);
$class = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $class));
$class = explode('_', $class);
// First try finding in structured directory format (preferred)
$path = self::$fofPath . '/' . implode('/', $class) . '.php';
if (@file_exists($path))
{
include_once $path;
}
// Then try the duplicate last name structured directory format (not recommended)
if (!class_exists($class_name, false))
{
reset($class);
$lastPart = end($class);
$path = self::$fofPath . '/' . implode('/', $class) . '/' . $lastPart . '.php';
if (@file_exists($path))
{
include_once $path;
}
}
// If it still fails, try looking in the legacy folder (used for backwards compatibility)
if (!class_exists($class_name, false))
{
$path = self::$fofPath . '/legacy/' . implode('/', $class) . '.php';
if (@file_exists($path))
{
include_once $path;
}
}
}
}