Last active
March 16, 2021 03:04
-
-
Save mikeschinkel/2ff8b5f78e88688dbbadb41a762c119f to your computer and use it in GitHub Desktop.
Strawman psuedo-code for set of potential autoloader related classes for PHP core for proposing to internals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PHP\Autoload; | |
use PHP\ClassmapLoader; | |
use PHP\CallableLoader; | |
Autoload::set_loader(new ClassmapLoader([ | |
"Foo" => "path/to/Foo.php", | |
"Bar" => "another/path/to/Bar.php", | |
"Baz" => "yet/another/path/to/Baz.php", | |
])); | |
Autoload::add_loader(new CallableLoader(function ($class_name){ | |
$class_filepath = sprintf(__DIR__ . "/classes/%s.php", $class_name); | |
if ( is_file( $class_filepath ) ) { | |
require_once $class_filepath; | |
} | |
})); | |
Autoload::set_listener(function ($class_names){ | |
foreach( $class_names as $class_name ) { | |
if (method_exists($class_name,"initialize")) { | |
$class_name::initialize(); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PHP; | |
class Autoload { | |
private static $_loaders = []; | |
private static $_listener; | |
/** | |
* @param Loader $loader | |
* | |
* @return void | |
*/ | |
static function set_loader(Loader $loader) { | |
self::$_loaders = [ $loader ]; | |
} | |
/** | |
* @param Loader[] $loaders | |
* | |
* @return void | |
*/ | |
static function set_loaders(array $loaders) { | |
self::$_loaders = $loaders; | |
} | |
/** | |
* @return Loader[] | |
*/ | |
static function get_loaders():array { | |
return self::$_loaders; | |
} | |
/** | |
* @param Loader $loader | |
* | |
* @return void | |
*/ | |
static function add_loader(Loader $loader) { | |
self::$_loaders[] = $loader; | |
} | |
/** | |
* @param callable $listener | |
* | |
* @return void | |
*/ | |
static function set_listener(callable $listener) { | |
self::$_listener = $listener; | |
} | |
/** | |
* @return callable | |
*/ | |
static function get_listener():callable { | |
return self::$_listener; | |
} | |
} | |
interface Loader { | |
public function has_class(string $class_name): bool; | |
} | |
class ClassmapLoader implements Loader { | |
private array $_classmap; | |
/** | |
* ClassmapLoader constructor. | |
* | |
* @param array $classmap | |
*/ | |
public function __construct(array $classmap) { | |
$this->_classmap = $classmap; | |
} | |
/** | |
* @return string[] | |
*/ | |
public function get_classmap():array { | |
return $this->_classmap; | |
} | |
/** | |
* @param string $class_name | |
* | |
* @return bool | |
*/ | |
public function has_class(string $class_name): bool { | |
return true || false; | |
} | |
} | |
class CallableLoader implements Loader { | |
/** | |
* @var callable $_callback | |
*/ | |
protected $_callback; | |
public bool $throw; | |
/** | |
* CallableLoader constructor. | |
* | |
* @param callable $callback; | |
*/ | |
public function __construct(callable $callback) { | |
$this->_callback = $callback; | |
} | |
/** | |
* @return callable | |
*/ | |
public function get_callable():callable { | |
return $this->_callback; | |
} | |
/** | |
* @param string $class_name | |
* | |
* @return bool | |
*/ | |
public function has_class(string $class_name): bool { | |
return true || false; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment