PHP Classes

File: examples/autoload.php

Recommend this page to a friend!
  Classes of Jorge Castro   PHP AutoLoad One   examples/autoload.php   Download  
File: examples/autoload.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP AutoLoad One
Generate scripts to autoload classes of a project
Author: By
Last change:
Date: 4 years ago
Size: 4,684 bytes
 

Contents

Class file image Download
<?php
/**
 * This class is used for autocomplete.
 * Class _AUTOLOAD_
 * @noautoload it avoids to index this class
 * @generated by AutoLoadOne 1.17 generated 2020/01/26 10:15:47
 * @copyright Copyright Jorge Castro C - MIT License. https://github.com/EFTEC/AutoLoadOne
 */
$s5e2d910320287__debug = true;

/* @var string[] Where $_arrautoloadCustom['namespace\Class']='folder\file.php' */
$s5e2d910320287__arrautoloadCustom = [
   
'ClassFolder1' => '|1|1.php',
   
'ClassFolder2' => '|1|2.php',
   
'ClassFolder3' => '|1|3.php',
   
'MyProject\Connection' => '|2|',
   
'AnotherProject\Connection' => '|2|',
   
'MyProject\Connection2' => '|3|2.php',
   
'AnotherProject\Connection2' => '|3|2.php',
   
'F6' => '|4|/f6/F6.php',
   
'ClassWithoutNameSpace' => '|4|alt/ClassWithoutNameSpace.php',
   
'nsfolder\subnamespace\CustomClass' => '|4|alt/CustomClass.php'
];
$s5e2d910320287__arrautoloadCustomCommon = [
   
1 => '/folder/folder1234567890123456789/ClassFolder',
   
2 => '/folder/multiplenamespace.php',
   
3 => '/folder/multiplenamespace',
   
4 => '/folder/subfolder'
];

/* @var string[] Where $_arrautoload['namespace']='folder' */
$s5e2d910320287__arrautoload = [
   
'nsfolder' => '/folder',
   
'specialnamespace' => '/folder',
   
'folder_ns\subnamespace\ClassFolder1' => '|1|',
   
'folder_ns\subnamespace\ClassFolder2' => '|1|',
   
'folder_ns\subnamespace\ClassFolder3' => '|1|',
   
'nsfolder\subnamespace' => '|2|',
   
'nsfolder\subnamespace\f1' => '|2|/f1',
   
'nsfolder\subnamespace\f2' => '|2|/f2',
   
'nsfolder\subnamespace\f3' => '|2|/f3',
   
'nsfolder\subnamespace\f4' => '|2|/f4',
   
'nsfolder\subnamespace\f5' => '|2|/f5',
   
'sub\sub\sub' => '|2|/subsubfolder'
];
$s5e2d910320287__arrautoloadCommon = [
   
1 => '/folder/folder_ns_1234567890123456789',
   
2 => '/folder/subfolder'
];

/* @var boolean[] Where $_arrautoload['namespace' or 'namespace\Class']=true if it's absolute (it uses the full path) */
$s5e2d910320287__arrautoloadAbsolute = [
 
];

/**
 * @param $class_name
 * @throws Exception
 */
function s5e2d910320287__auto($class_name)
{
   
// its called only if the class is not loaded.
   
$ns = dirname($class_name); // without trailing
   
$ns = ($ns == '.') ? '' : $ns;
   
$cls = basename($class_name);
   
// special cases
   
if (isset($GLOBALS['s5e2d910320287__arrautoloadCustom'][$class_name])) {
       
s5e2d910320287__loadIfExists($GLOBALS['s5e2d910320287__arrautoloadCustom'][$class_name]
            ,
$class_name,'s5e2d910320287__arrautoloadCustomCommon');
        return;
    }
   
// normal (folder) cases
   
if (isset($GLOBALS['s5e2d910320287__arrautoload'][$ns])) {
       
s5e2d910320287__loadIfExists($GLOBALS['s5e2d910320287__arrautoload'][$ns] . '/' . $cls . '.php'
           
, $ns,'s5e2d910320287__arrautoloadCommon');
        return;
    }
}

/**
 * We load the file.
 * @param string $filename
 * @param string $key key of the class it could be the full class name or only the namespace
 * @param string $arrayName [optional] it's the name of the arrayname used to replaced |n| values.
 * @throws Exception
 */
function s5e2d910320287__loadIfExists($filename, $key,$arrayName='')
{
    if (isset(
$GLOBALS['s5e2d910320287__arrautoloadAbsolute'][$key])) {
       
$fullFile = $filename; // its an absolute path
       
if (strpos($fullFile, '../') === 0) { // Or maybe, not, it's a remote-relative path.
           
$oldDir = getcwd(); // we copy the current url
           
chdir(__DIR__);
        }
    } else {
       
$fullFile = __DIR__ . "/" . s5e2d910320287__replaceCurlyVariable($filename,$arrayName); // its relative to this path
   
}
   
/** @noinspection PhpIncludeInspection */
   
if ((@include $fullFile) === false) {
        if (
$GLOBALS['s5e2d910320287__debug']) {
            throw new
Exception("AutoLoadOne Error: Loading file [" . __DIR__ . "/" . $filename . "] for class [" . basename($filename) . "]");
        } else {
            throw new
Exception("AutoLoadOne Error: No file found.");
        }
    } else {
        if (isset(
$oldDir)) {
           
chdir($oldDir);
        }
    }
}
function
s5e2d910320287__replaceCurlyVariable($string,$arrayName) {
    if(
strpos($string,'|')===false) return $string; // nothing to replace.
   
return preg_replace_callback('/\\|\s?(\w+)\s?\\|/u', function ($matches) use ($arrayName) {
       
$item = is_array($matches) ? substr($matches[0], 1, strlen($matches[0]) - 2)
            :
substr($matches, 1, strlen($matches) - 2);
        return
$GLOBALS[$arrayName][$item];
    },
$string);
}

spl_autoload_register(function ($class_name) {
   
s5e2d910320287__auto($class_name);
});
// autorun
@include __DIR__.'/folder/runme.php';
@include
__DIR__.'/folder/RunMeClass.php';
@include
__DIR__.'/folder/runmelast.php';