Advertisement
Shadowpawn

Generic Autoloader - PHP

Apr 8th, 2021
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.32 KB | None | 0 0
  1. <?php
  2.  
  3.     function createNameSpaceCollection(){
  4.  
  5.         $nameSpaceCollection = new nameSpaceCollection();
  6.         $nameSpaceCollection->addTo(dirname( __FILE__));
  7.         $nameSpaceCollection->createNameSpaceCollection();
  8.         return $nameSpaceCollection;
  9.     }
  10.  
  11.  
  12.     class nameSpaceCollection{
  13.  
  14.         public $namespaceArray = [
  15.  
  16.             'fileNamePath' => [
  17.  
  18.             ],
  19.             "namespace" => [
  20.  
  21.             ],
  22.            
  23.  
  24.         ];
  25.  
  26.         private $ignoreList = [
  27.             '.git',
  28.             '.htaccess',
  29.             'assets',
  30.             'node_modules',
  31.             'package-lock.json',
  32.             'autoloader.php',
  33.             'index.php',
  34.             '.',
  35.             '..'
  36.         ];
  37.  
  38.        public function nameSpaceCollection(){
  39.  
  40.             return $this;
  41.  
  42.         }
  43.  
  44.         public function addTo( $path ){
  45.  
  46.             if( is_file( $path ) ){
  47.  
  48.                 if(!array_search($path, $this->namespaceArray['fileNamePath'], true)){
  49.                     array_push( $this->namespaceArray['fileNamePath'], $path );
  50.                 }
  51.  
  52.             } else if( is_dir( $path ) ) {
  53.                 $this->findSubDir( $path );
  54.             }
  55.  
  56.         }
  57.  
  58.         public function findSubDir( $path ){
  59.  
  60.  
  61.             if( is_dir( $path ) ){
  62.  
  63.                 if( $dr = opendir( $path ) ){
  64.  
  65.                     foreach(scandir( $path ) as $idx => $p ){
  66.  
  67.                         if( !in_array( $p, $this->ignoreList ) ){
  68.                            
  69.                             $this->findSubDir( $path.'\\'.$p );
  70.                         }
  71.  
  72.                     }
  73.  
  74.                     closedir($dr);
  75.  
  76.                 }
  77.  
  78.  
  79.             } else if( is_file( $path ) ) {
  80.  
  81.                 $this->addTo($path);
  82.  
  83.             }
  84.  
  85.         }
  86.  
  87.         public function createNameSpaceCollection(){
  88.  
  89.             foreach( $this->namespaceArray['fileNamePath'] as $key => $value ){
  90.  
  91.                 if($value != ''){
  92.                     include_once($value);
  93.                     $value = explode( '\\', $value );
  94.                     $value = substr( $value[ count($value)-1 ], 0, count($value[ count($value)-1 ])-5);
  95.                     $this->namespaceArray['namespace'][$value] = str_replace( '.', '\\', $value );    
  96.                 }
  97.             }
  98.  
  99.         }
  100.  
  101.     }
  102.  
  103.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement