1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage autoloader
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2, or later
7 */
8
9 defined('FOF_INCLUDED') or die();
10
11 /**
12 * The main class autoloader for FOF itself
13 *
14 * @package FrameworkOnFramework
15 * @subpackage autoloader
16 * @since 2.1
17 */
18 class FOFAutoloaderFof
19 {
20 /**
21 * An instance of this autoloader
22 *
23 * @var FOFAutoloaderFof
24 */
25 public static $autoloader = null;
26
27 /**
28 * The path to the FOF root directory
29 *
30 * @var string
31 */
32 public static $fofPath = null;
33
34 /**
35 * Initialise this autoloader
36 *
37 * @return FOFAutoloaderFof
38 */
39 public static function init()
40 {
41 if (self::$autoloader == null)
42 {
43 self::$autoloader = new self;
44 }
45
46 return self::$autoloader;
47 }
48
49 /**
50 * Public constructor. Registers the autoloader with PHP.
51 */
52 public function __construct()
53 {
54 self::$fofPath = realpath(__DIR__ . '/../');
55
56 spl_autoload_register(array($this,'autoload_fof_core'));
57 }
58
59 /**
60 * The actual autoloader
61 *
62 * @param string $class_name The name of the class to load
63 *
64 * @return void
65 */
66 public function autoload_fof_core($class_name)
67 {
68 // Make sure the class has a FOF prefix
69 if (substr($class_name, 0, 3) != 'FOF')
70 {
71 return;
72 }
73
74 // Remove the prefix
75 $class = substr($class_name, 3);
76
77 // Change from camel cased (e.g. ViewHtml) into a lowercase array (e.g. 'view','html')
78 $class = preg_replace('/(\s)+/', '_', $class);
79 $class = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $class));
80 $class = explode('_', $class);
81
82 // First try finding in structured directory format (preferred)
83 $path = self::$fofPath . '/' . implode('/', $class) . '.php';
84
85 if (@file_exists($path))
86 {
87 include_once $path;
88 }
89
90 // Then try the duplicate last name structured directory format (not recommended)
91
92 if (!class_exists($class_name, false))
93 {
94 reset($class);
95 $lastPart = end($class);
96 $path = self::$fofPath . '/' . implode('/', $class) . '/' . $lastPart . '.php';
97
98 if (@file_exists($path))
99 {
100 include_once $path;
101 }
102 }
103
104 // If it still fails, try looking in the legacy folder (used for backwards compatibility)
105
106 if (!class_exists($class_name, false))
107 {
108 $path = self::$fofPath . '/legacy/' . implode('/', $class) . '.php';
109
110 if (@file_exists($path))
111 {
112 include_once $path;
113 }
114 }
115 }
116 }
117