📜 ⬆️ ⬇️

Universal Startup Class

After reading this habratopik. I decided to share my autoloader class (if someone comes in handy), who can recursively search for the necessary files, not only classes, but any other file transferred to him, well, if there is no file found where indicated, it searches the entire server starting from the root , if the file is found somewhere not there, generates a corresponding message, is able to work with the namespace and log its actions if necessary.

<?php namespace youNameSpace { /** *    * *     ,    .<br/> *     ,  <br/> *         ,<br/> *      (   ),   . * * @author :   * @version 1.0 * @copyright:   */ class Autoloader { /** *,    * * @var type const * */ const debug = 0; public function __construct() { ; } /** *      .<br/> *   ,    . * * @param string $file  ( ) * @param string $ext  ( ) * @param string $dir   (    ) * * @return string * @return false * */ public static function autoload($file, $ext = FALSE, $dir = FALSE) { $file = str_replace('\\', '/', $file); if($ext === FALSE) { $path = $_SERVER['DOCUMENT_ROOT'] . '/classes'; $filepath = $_SERVER['DOCUMENT_ROOT'] . '/classes/' . $file . '.php'; } else { $path = $_SERVER['DOCUMENT_ROOT'] . (($dir) ? '/' . $dir : ''); $filepath = $path . '/' . $file . '.' . $ext; } if (file_exists($filepath)) { if($ext === FALSE) { if(Autoloader::debug) Autoloader::StPutFile((' ' .$filepath)); require_once($filepath); } else { if(Autoloader::debug) Autoloader::StPutFile(('   ' .$filepath)); return $filepath; } } else { $flag = true; if(Autoloader::debug) Autoloader::StPutFile(('    <b>' . $file . '</b>  <b>' . $path . '</b>')); return Autoloader::recursive_autoload($file, $path, $ext, $flag); } } /** *      . * * @param string $file  ( ) * @param string $path    * @param string $ext   * @param string $flag         * * @return string * @return bool * */ public static function recursive_autoload($file, $path, $ext, &$flag) { if (FALSE !== ($handle = opendir($path)) && $flag) { while (FAlSE !== ($dir = readdir($handle)) && $flag) { if (strpos($dir, '.') === FALSE) { $path2 = $path .'/' . $dir; $filepath = $path2 . '/' . $file .(($ext === FALSE) ? '.php' : '.' . $ext); if(Autoloader::debug) Autoloader::StPutFile(('  <b>' .$file .'</b> in ' .$filepath)); if (file_exists($filepath)) { $flag = FALSE; if($ext === FALSE) { if(Autoloader::debug) Autoloader::StPutFile((' ' .$filepath )); require_once($filepath); break; } else { if(Autoloader::debug) Autoloader::StPutFile(('   ' .$filepath )); return $filepath; } } $res = Autoloader::recursive_autoload($file, $path2, $ext, $flag); } } closedir($handle); } return $res; } /** *   * * @param string $data    * * @return void * */ private static function StPutFile($data) { $dir = $_SERVER['DOCUMENT_ROOT'] .'/Log/Log.html'; $file = fopen($dir, 'a'); flock($file, LOCK_EX); fwrite($file, ('║' .$data .'=>' .date('dmY H:i:s') .'<br/>║<br/>' .PHP_EOL)); flock($file, LOCK_UN); fclose ($file); } } \spl_autoload_register('youNameSpace\Autoloader::autoload'); } ?> 

')

Source: https://habr.com/ru/post/136887/


All Articles