Skip to content

Instantly share code, notes, and snippets.

@konrados
Created July 20, 2016 12:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save konrados/74e361223c4ac6731910bee935bf7899 to your computer and use it in GitHub Desktop.
<?php
class Autoload{
private $namespaceAliases=[];
function __construct(){
$this->namespaceAliases=require_once(App::$frameworkDir.'config/NamespaceAliases.php');
spl_autoload_register([$this,'load'],true,true);
}
public function load($class){
$projectDir=App::$projectDir;//ended with a slash
$path=str_replace('\\', DIRECTORY_SEPARATOR,$class);
$path=str_replace('/', DIRECTORY_SEPARATOR,$path);
$path.='.php';
if(strpos($path,'system/')===0){
$path=str_replace('system/','vendor/sayri/sayri/core/',$path);
};
$namespaceAliasFound=false;
foreach($this->namespaceAliases as $shortcut=>$namespaceAlias){
$namespaceAlias=str_replace('\\', DIRECTORY_SEPARATOR,$namespaceAlias);
$shortcut=str_replace('\\', DIRECTORY_SEPARATOR,$shortcut);
//echo "replacing $shortcut with $namespaceAlias in $path <br>";
if(strpos($path,$shortcut)===0){
$path=str_replace($shortcut,$namespaceAlias.DIRECTORY_SEPARATOR,$path);
$namespaceAliasFound=true;
break;
}
}
$fullPath=$path;
if(!$namespaceAliasFound)
$fullPath=$projectDir.$fullPath;
//echo 'autoloading full path: '.$fullPath;
$dirs=[''];
if(file_exists($fullPath)){
//echo ' (OK, including it)'.'<br>';
include($fullPath);
}else{
//echo ' (File doesn\'t exists)<br>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment