Skip to content

Commit 80c4185

Browse files
committed
Initial and Tested
1 parent 27bb52c commit 80c4185

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

autoload.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* smartload.php
4+
*
5+
* Autoloading Class
6+
*
7+
* @author Olubodun Agbalaya
8+
* @copyright 2017
9+
* @license http://www.php.net/license/3_0.txt PHP License 3.0
10+
* @version GIT: 1.0.0
11+
* @link http://pear.php.net/package/PackageName
12+
*/
13+
14+
15+
//A simple file to auto-load namespaced and non-namespaced classes in your project
16+
//You'll only need to include this file in all your project files to use autoloaded classes
17+
//simply change the $classes_root variable to point to the folder where your classes reside
18+
//this autoloader expects your files end in .php, modify line 31 if your case is different
19+
//this utility doesnt however stop you from decaring namespacs as declared in classes your are using (of course)!
20+
21+
spl_autoload_register(function($class_name) {
22+
23+
$classes_root = "./App"; //change to reflect your classes root folder, i normally just use App/
24+
//see if we have a chache and the queried class file path is already inc cache
25+
if (\function_exists('apcu_add')) {
26+
27+
28+
if (\apcu_exists($class_name) && \apcu_fetch($class_name)) {
29+
30+
require_once apcu_fetch($class_name);
31+
return;
32+
}
33+
}
34+
35+
$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($classes_root));
36+
37+
38+
// Filter directories, i dont need the result, so cast to NULL
39+
(unset) $files = new CallbackFilterIterator($dir, function ($current, $key, $iterator) use ($class_name) {
40+
41+
if ($current->isFile()) {
42+
43+
//here we remove namespace prefix(es) , leaving only the base class name
44+
if (\strrpos($class_name, "\\") !== FALSE) {
45+
$cur_file = substr($class_name, \strrpos($class_name, "\\") + 1);
46+
} else {
47+
$cur_file = $class_name; //else just leace the class name as-is
48+
}
49+
50+
51+
if ($current->getBasename('.php') === trim($cur_file)) {//stript out .php and compare file name to required class
52+
if (\function_exists('apcu_add')) {
53+
54+
\apcu_store($class_name, $current->getRealPath());
55+
}
56+
57+
require_once $current->getRealPath();
58+
}
59+
}
60+
});
61+
62+
63+
64+
65+
foreach ($files as $c)
66+
; //note the trick, just dummy iterating makes the function (the FilterIterator Above)
67+
68+
69+
70+
71+
72+
//yeild required file names else the file names never gets generated/included
73+
});

smartloader

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 27bb52c30cbbf5778fe3fee9baa301c66c16cc60

0 commit comments

Comments
 (0)