Skip to content
This repository was archived by the owner on Jan 24, 2019. It is now read-only.

Commit 25b6168

Browse files
committed
initial commit
0 parents  commit 25b6168

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

Autoload.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/*
3+
* Autoload
4+
*
5+
* Simple and Concise PHP Autoloader
6+
* PSR-4 convention - for details: see http://www.php-fig.org/psr/psr-4/
7+
*
8+
* @package Autoload
9+
* @version 1.0
10+
* @link http://github.com/surikat/Autoload/
11+
* @author Jo Surikat <jo@surikat.pro>
12+
* @website http://wildsurikat.com
13+
*/
14+
namespace Wild\Autoload;
15+
class Autoload{
16+
protected $namespaces = [];
17+
protected $checked = [];
18+
private static $instance;
19+
static function register($base_dir,$prefix=''){
20+
return self::getInstance()->addNamespace($prefix,$base_dir)->splRegister();
21+
}
22+
static function getInstance(){
23+
if(!isset(self::$instance))
24+
self::$instance = new self;
25+
return self::$instance;
26+
}
27+
function addNamespaces($a){
28+
foreach($a as $prefix=>$base_dir){
29+
$this->addNamespace($prefix,$base_dir);
30+
}
31+
return $this;
32+
}
33+
function addNamespace($prefix, $base_dir, $prepend = false){
34+
if(is_array($base_dir)){
35+
foreach($base_dir as $dir){
36+
$this->addNamespace($prefix, $dir, $prepend);
37+
}
38+
}
39+
else{
40+
$prefix = trim($prefix, '\\').'\\';
41+
$base_dir = rtrim($base_dir, '/').'/';
42+
if(!isset($this->namespaces[$prefix]))
43+
$this->namespaces[$prefix] = [];
44+
if ($prepend)
45+
array_unshift($this->namespaces[$prefix], $base_dir);
46+
else
47+
array_push($this->namespaces[$prefix], $base_dir);
48+
}
49+
return $this;
50+
}
51+
protected function loadFile($file,$class){
52+
if(file_exists($file)){
53+
require $file;
54+
if(!class_exists($class,false)&&!interface_exists($class,false)&&!trait_exists($class,false))
55+
throw new \Exception('Class "'.$class.'" not found as expected in "'.$file.'"');
56+
$this->checked[] = $class;
57+
return true;
58+
}
59+
return false;
60+
}
61+
function findClass($class,$relative_class,$prefix){
62+
if(isset($this->namespaces[$prefix])){
63+
foreach($this->namespaces[$prefix] as $base_dir){
64+
$file = $base_dir.str_replace('\\', '/', $relative_class).'.php';
65+
if($this->loadFile($file,$class))
66+
return true;
67+
}
68+
}
69+
}
70+
function classLoad($class){
71+
if(in_array($class,$this->checked))
72+
return;
73+
$prefix = $class;
74+
while($prefix!='\\'){
75+
$prefix = rtrim($prefix, '\\');
76+
$pos = strrpos($prefix, '\\');
77+
if($pos!==false){
78+
$prefix = substr($class, 0, $pos + 1);
79+
$relative_class = substr($class, $pos + 1);
80+
}
81+
else{
82+
$prefix = '\\';
83+
$relative_class = $class;
84+
}
85+
if($this->findClass($class,$relative_class,$prefix))
86+
return;
87+
}
88+
}
89+
function __invoke($class){
90+
return $this->classLoad($class);
91+
}
92+
function splRegister(){
93+
spl_autoload_register($this);
94+
return $this;
95+
}
96+
}

0 commit comments

Comments
 (0)