Added new global autoloader

This commit is contained in:
Shoghi Cervantes 2014-03-28 05:04:34 +01:00
parent b59926bece
commit fd8e5b27e5
5 changed files with 340 additions and 48 deletions

View File

@ -83,24 +83,19 @@ namespace PocketMine {
const MINECRAFT_VERSION = "v0.8.1 alpha";
const PHP_VERSION = "5.5";
\spl_autoload_register(function ($load){
$path = explode('\\', trim($load, '\\'));
if(($parent = array_shift($path)) === "PocketMine"){ //part of the PocketMine-MP code
$className = array_pop($path);
if(count($path) > 0){
$path = implode(DIRECTORY_SEPARATOR, array_map("strtolower", $path)) . DIRECTORY_SEPARATOR;
}else{
$path = "";
}
$fPath = \PocketMine\PATH . "src" . DIRECTORY_SEPARATOR . "PocketMine" . DIRECTORY_SEPARATOR . $path . $className . ".php";
if(file_exists($fPath)){
require_once($fPath);
}
}
});
define("PocketMine\\PATH", \getcwd() . DIRECTORY_SEPARATOR);
if(!class_exists("SplClassLoader", false)){
require_once(\PocketMine\PATH . "src/SPL/SplClassLoader.php");
}
$autoloader = new \SplClassLoader();
$autoloader->add("PocketMine", array(
\PocketMine\PATH . "src"
));
$autoloader->register(true);
//Startup code. Do not look at it, it can harm you. Most of them are hacks to fix date-related bugs, or basic functions used after this
set_time_limit(0); //Who set it to 30 seconds?!?!
@ -168,28 +163,6 @@ namespace PocketMine {
}
}
function hard_unset(&$var){
if(is_object($var)){
$unset = new \ReflectionClass($var);
foreach($unset->getProperties() as $prop){
$prop->setAccessible(true);
@hard_unset($prop->getValue($var));
$prop->setValue($var, null);
}
$var = null;
unset($var);
}elseif(is_array($var)){
foreach($var as $i => $v){
hard_unset($var[$i]);
}
$var = null;
unset($var);
}else{
$var = null;
unset($var);
}
}
/**
* Output text to the console, can contain Minecraft-formatted text.
*
@ -200,8 +173,8 @@ namespace PocketMine {
*/
function console($message, $EOL = true, $log = true, $level = 1){
if(!defined("PocketMine\\DEBUG") or \PocketMine\DEBUG >= $level){
$message = Utils\TextFormat::GRAY . $message . ($EOL === true ? PHP_EOL : "");
if($message{3} !== "["){
$message .= $EOL === true ? PHP_EOL : "";
if($message{0} !== "["){
$message = "[INFO] $message";
}
$time = (\PocketMine\ANSI === true ? Utils\TextFormat::AQUA . date("H:i:s") . Utils\TextFormat::RESET : date("H:i:s")) . " ";
@ -379,8 +352,9 @@ namespace PocketMine {
new Wizard\Installer();
}
if(!defined("PARENT_API_EXISTENT")){
$server = new Server(\PocketMine\PATH, \PocketMine\DATA, \PocketMine\PLUGIN_PATH);
$server = new Server($autoloader, \PocketMine\PATH, \PocketMine\DATA, \PocketMine\PLUGIN_PATH);
$server->start();
kill(getmypid());

View File

@ -117,6 +117,7 @@ class Server{
private $serverID;
private $autoloader;
private $filePath;
private $dataPath;
private $pluginPath;
@ -299,6 +300,13 @@ class Server{
return $this->getConfigString("motd", "");
}
/**
* @return \SplClassLoader
*/
public function getLoader(){
return $this->autoloader;
}
/**
* @return PluginManager
*/
@ -445,13 +453,15 @@ class Server{
}
/**
* @param \SplClassLoader $autoloader
* @param string $filePath
* @param string $dataPath
* @param string $pluginPath
*/
public function __construct($filePath, $dataPath, $pluginPath){
public function __construct(\SplClassLoader $autoloader, $filePath, $dataPath, $pluginPath){
self::$instance = $this;
$this->autoloader = $autoloader;
$this->filePath = $filePath;
$this->dataPath = $dataPath;
$this->pluginPath = $pluginPath;

View File

@ -59,17 +59,20 @@ class FolderPluginLoader implements PluginLoader{
return null;
}
foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($file . "/src/")) as $fName){
if(strtolower(substr($fName, -4)) === ".php"){
include_once($fName);
}
}
$className = $description->getMain();
if(class_exists($className, true)){ //call autoloader, TODO: replace this with a specific Plugin autoload
$this->server->getLoader()->add(substr($className, 0, strpos($className, "\\")), array(
$file . "/src"
));
if(class_exists($className, true)){
$plugin = new $className();
$this->initPlugin($plugin, $description, $dataFolder, $file);
return $plugin;
}else{
trigger_error("Couldn't load plugin ".$description->getName().": main class not found", E_USER_WARNING);
return null;
}
}
}

65
src/SPL/SplAutoloader.php Normal file
View File

@ -0,0 +1,65 @@
<?php
/**
* SplAutoloader defines the contract that any OO based autoloader must follow.
*
* @author Guilherme Blanco <guilhermeblanco@php.net>
*/
interface SplAutoloader
{
/**
* Defines autoloader to work silently if resource is not found.
*
* @const
*/
const MODE_SILENT = 0;
/**
* Defines autoloader to work normally (requiring an un-existent resource).
*
* @const
*/
const MODE_NORMAL = 1;
/**
* Defines autoloader to work in debug mode, loading file and validating requested resource.
*
* @const
*/
const MODE_DEBUG = 2;
/**
* Define the autoloader work mode.
*
* @param integer $mode Autoloader work mode.
*/
public function setMode($mode);
/**
* Add a new resource lookup path.
*
* @param string $resourceName Resource name, namespace or prefix.
* @param mixed $resourcePath Resource single path or multiple paths (array).
*/
public function add($resourceName, $resourcePath = null);
/**
* Load a resource through provided resource name.
*
* @param string $resourceName Resource name.
*/
public function load($resourceName);
/**
* Register this as an autoloader instance.
*
* @param boolean $prepend Whether to prepend the autoloader or not in autoloader's list.
*/
public function register($prepend = false);
/**
* Unregister this autoloader instance.
*
*/
public function unregister();
}

240
src/SPL/SplClassLoader.php Normal file
View File

@ -0,0 +1,240 @@
<?php
require_once("SplAutoLoader.php");
/**
* SplClassLoader implementation that implements the technical interoperability
* standards for PHP 5.3 namespaces and class names.
*
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
*
* Example usage:
*
* $classLoader = new \SplClassLoader();
*
* // Configure the SplClassLoader to act normally or silently
* $classLoader->setMode(\SplClassLoader::MODE_NORMAL);
*
* // Add a namespace of classes
* $classLoader->add('Doctrine', array(
* '/path/to/doctrine-common', '/path/to/doctrine-dbal', '/path/to/doctrine-orm'
* ));
*
* // Add a prefix
* $classLoader->add('Swift', '/path/to/swift');
*
* // Add a prefix through PEAR1 convention, requiring include_path lookup
* $classLoader->add('PEAR');
*
* // Allow to PHP use the include_path for file path lookup
* $classLoader->setIncludePathLookup(true);
*
* // Possibility to change the default php file extension
* $classLoader->setFileExtension('.php');
*
* // Register the autoloader, prepending it in the stack
* $classLoader->register(true);
*
* @author Guilherme Blanco <guilhermeblanco@php.net>
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman S. Borschel <roman@code-factory.org>
* @author Matthew Weier O'Phinney <matthew@zend.com>
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
* @author Fabien Potencier <fabien.potencier@symfony-project.org>
*/
class SplClassLoader implements SplAutoloader
{
/**
* @var string
*/
private $fileExtension = '.php';
/**
* @var boolean
*/
private $includePathLookup = false;
/**
* @var array
*/
private $resources = array();
/**
* @var integer
*/
private $mode = self::MODE_NORMAL;
/**
* {@inheritdoc}
*/
public function setMode($mode)
{
if ($mode & self::MODE_SILENT && $mode & self::MODE_NORMAL) {
throw new \InvalidArgumentException(
sprintf('Cannot have %s working normally and silently at the same time!', __CLASS__)
);
}
$this->mode = $mode;
}
/**
* Define the file extension of resource files in the path of this class loader.
*
* @param string $fileExtension
*/
public function setFileExtension($fileExtension)
{
$this->fileExtension = $fileExtension;
}
/**
* Retrieve the file extension of resource files in the path of this class loader.
*
* @return string
*/
public function getFileExtension()
{
return $this->fileExtension;
}
/**
* Turns on searching the include for class files. Allows easy loading installed PEAR packages.
*
* @param boolean $includePathLookup
*/
public function setIncludePathLookup($includePathLookup)
{
$this->includePathLookup = $includePathLookup;
}
/**
* Gets the base include path for all class files in the namespace of this class loader.
*
* @return boolean
*/
public function getIncludePathLookup()
{
return $this->includePathLookup;
}
/**
* {@inheritdoc}
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'load'), true, $prepend);
}
/**
* {@inheritdoc}
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'load'));
}
/**
* {@inheritdoc}
*/
public function add($resource, $resourcePath = null)
{
$this->resources[$resource] = (array) $resourcePath;
}
/**
* {@inheritdoc}
*/
public function load($resourceName)
{
$resourceAbsolutePath = $this->getResourceAbsolutePath($resourceName);
switch (true) {
case ($this->mode & self::MODE_SILENT):
if ($resourceAbsolutePath !== false) {
require $resourceAbsolutePath;
}
break;
case ($this->mode & self::MODE_NORMAL):
default:
require $resourceAbsolutePath;
break;
}
if ($this->mode & self::MODE_DEBUG && ! $this->isResourceDeclared($resourceName)) {
throw new \RuntimeException(
sprintf('Autoloader expected resource "%s" to be declared in file "%s".', $resourceName, $resourceAbsolutePath)
);
}
}
/**
* Transform resource name into its absolute resource path representation.
*
* @param string $resourceName
*
* @return string Resource absolute path.
*/
private function getResourceAbsolutePath($resourceName)
{
$resourceRelativePath = $this->getResourceRelativePath($resourceName);
foreach ($this->resources as $resource => $resourcesPath) {
if (strpos($resourceName, $resource) !== 0) {
continue;
}
foreach ($resourcesPath as $resourcePath) {
$resourceAbsolutePath = $resourcePath . DIRECTORY_SEPARATOR . $resourceRelativePath;
if (is_file($resourceAbsolutePath)) {
return $resourceAbsolutePath;
}
}
}
if ($this->includePathLookup && ($resourceAbsolutePath = stream_resolve_include_path($resourceRelativePath)) !== false) {
return $resourceAbsolutePath;
}
return false;
}
/**
* Transform resource name into its relative resource path representation.
*
* @param string $resourceName
*
* @return string Resource relative path.
*/
private function getResourceRelativePath($resourceName)
{
// We always work with FQCN in this context
$resourceName = ltrim($resourceName, '\\');
$resourcePath = '';
if (($lastNamespacePosition = strrpos($resourceName, '\\')) !== false) {
// Namespaced resource name
$resourceNamespace = substr($resourceName, 0, $lastNamespacePosition);
$resourceName = substr($resourceName, $lastNamespacePosition + 1);
$resourcePath = str_replace('\\', DIRECTORY_SEPARATOR, $resourceNamespace) . DIRECTORY_SEPARATOR;
}
return $resourcePath . str_replace('_', DIRECTORY_SEPARATOR, $resourceName) . $this->fileExtension;
}
/**
* Check if resource is declared in user space.
*
* @param string $resourceName
*
* @return boolean
*/
private function isResourceDeclared($resourceName)
{
return class_exists($resourceName, false)
|| interface_exists($resourceName, false)
|| (function_exists('trait_exists') && trait_exists($resourceName, false));
}
}