Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use composer autoload in favor of manual spl_autoload_register #50

Merged
merged 1 commit into from
Jul 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"phpunit/phpunit": ">=4.0.0"
},
"autoload": {
"files": ["src/Hprose.php"]
"files": [
"src/init.php"
],
"psr-4": {
"Hprose\\": "src/Hprose"
}
}
}
102 changes: 9 additions & 93 deletions src/Hprose.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,105 +14,21 @@
* *
* hprose for php 5.3+ *
* *
* LastModified: Jul 30, 2016 *
* LastModified: Jul 10, 2017 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/

require_once 'Throwable.php';
require_once 'TypeError.php';
require_once 'Hprose/Future/functions.php';
require_once 'Hprose/Promise/functions.php';
require_once 'Hprose/functions.php';
require_once 'functions.php';
require_once __DIR__ . '/init.php';

spl_autoload_register(function($className) {
if ((strlen($className) > 6) && (strtolower(substr($className, 0, 6)) === "hprose")) {
if ($className{6} === '\\') {
include __DIR__ . DIRECTORY_SEPARATOR . str_replace("\\", DIRECTORY_SEPARATOR, $className) . ".php";
// Autoload for non-composer applications
spl_autoload_register(function ($className) {
if ((strlen($className) > 7) && (strtolower(substr($className, 0, 7)) === "hprose\\")) {
$file = __DIR__ . DIRECTORY_SEPARATOR . str_replace("\\", DIRECTORY_SEPARATOR, $className) . ".php";
if (is_file($file)) {
include $file;
return true;
}
else {
// Deprecated
// Compatible with older versions only
// You'd better not use these classes.
switch (strtolower($className)) {
case 'hproseasync':
class_alias('Hprose\\Async', 'HproseAsync');
break;
case 'hprosecompleter':
class_alias('Hprose\\Completer', 'HproseCompleter');
break;
case 'hprosefuture':
class_alias('Hprose\\Future', 'HproseFuture');
break;
case 'hprosetags':
class_alias('Hprose\\Tags', 'HproseTags');
break;
case 'hprosebytesio':
class_alias('Hprose\\BytesIO', 'HproseBytesIO');
break;
case 'hproseclassmanager':
class_alias('Hprose\\ClassManager', 'HproseClassManager');
break;
case 'hproserawreader':
class_alias('Hprose\\RawReader', 'HproseRawReader');
break;
case 'hprosereader':
class_alias('Hprose\\Reader', 'HproseReader');
break;
case 'hprosewriter':
class_alias('Hprose\\Writer', 'HproseWriter');
break;
case 'hproseformatter':
class_alias('Hprose\\Formatter', 'HproseFormatter');
break;
case 'hproseresultmode':
class_alias('Hprose\\ResultMode', 'HproseResultMode');
break;
case 'hprosefilter':
class_alias('Hprose\\Filter', 'HproseFilter');
break;
case 'hproseclient':
class_alias('Hprose\\Client', 'HproseClient');
break;
case 'hproseservice':
class_alias('Hprose\\Service', 'HproseService');
break;
case 'hprosehttpclient':
class_alias('Hprose\\Http\\Client', 'HproseHttpClient');
break;
case 'hprosehttpservice':
class_alias('Hprose\\Http\\Service', 'HproseHttpService');
break;
case 'hprosehttpserver':
class_alias('Hprose\\Http\\Server', 'HproseHttpServer');
break;
case 'hprosesocketclient':
class_alias('Hprose\\Socket\\Client', 'HproseSocketClient');
break;
case 'hprosesocketservice':
class_alias('Hprose\\Socket\\Service', 'HproseSocketService');
break;
case 'hprosesocketserver':
class_alias('Hprose\\Socket\\Server', 'HproseSocketServer');
break;
case 'hprosejsonrpcclientfilter':
class_alias('Hprose\\Filter\\JSONRPC\\ClientFilter', 'HproseJSONRPCClientFilter');
break;
case 'hprosejsonrpcservicefilter':
class_alias('Hprose\\Filter\\JSONRPC\\ServiceFilter', 'HproseJSONRPCServiceFilter');
break;
case 'hprosexmlrpcclientfilter':
class_alias('Hprose\\Filter\\XMLRPC\\ClientFilter', 'HproseXMLRPCClientFilter');
break;
case 'hprosexmlrpcservicefilter':
class_alias('Hprose\\Filter\\XMLRPC\\ServiceFilter', 'HproseXMLRPCServiceFilter');
break;
default:
return false;
}
}
return true;
}
return false;
});
47 changes: 47 additions & 0 deletions src/init.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

require_once __DIR__ . '/Throwable.php';
require_once __DIR__ . '/TypeError.php';
require_once __DIR__ . '/Hprose/Future/functions.php';
require_once __DIR__ . '/Hprose/Promise/functions.php';
require_once __DIR__ . '/Hprose/functions.php';
require_once __DIR__ . '/functions.php';

// Deprecated
// Compatible with older versions only
// You'd better not use these classes.
spl_autoload_register(function ($className) {
$oldVersionAliases = array(
// 'hproseasync' => array('Hprose\\Async', 'HproseAsync'), // Hprose\Async not found
'hprosecompleter' => array('Hprose\\Completer', 'HproseCompleter'),
'hprosefuture' => array('Hprose\\Future', 'HproseFuture'),
'hprosetags' => array('Hprose\\Tags', 'HproseTags'),
'hprosebytesio' => array('Hprose\\BytesIO', 'HproseBytesIO'),
'hproseclassmanager' => array('Hprose\\ClassManager', 'HproseClassManager'),
'hproserawreader' => array('Hprose\\RawReader', 'HproseRawReader'),
'hprosereader' => array('Hprose\\Reader', 'HproseReader'),
'hprosewriter' => array('Hprose\\Writer', 'HproseWriter'),
'hproseformatter' => array('Hprose\\Formatter', 'HproseFormatter'),
'hproseresultmode' => array('Hprose\\ResultMode', 'HproseResultMode'),
'hprosefilter' => array('Hprose\\Filter', 'HproseFilter'),
'hproseclient' => array('Hprose\\Client', 'HproseClient'),
'hproseservice' => array('Hprose\\Service', 'HproseService'),
'hprosehttpclient' => array('Hprose\\Http\\Client', 'HproseHttpClient'),
'hprosehttpservice' => array('Hprose\\Http\\Service', 'HproseHttpService'),
'hprosehttpserver' => array('Hprose\\Http\\Server', 'HproseHttpServer'),
'hprosesocketclient' => array('Hprose\\Socket\\Client', 'HproseSocketClient'),
'hprosesocketservice' => array('Hprose\\Socket\\Service', 'HproseSocketService'),
'hprosesocketserver' => array('Hprose\\Socket\\Server', 'HproseSocketServer'),
'hprosejsonrpcclientfilter' => array('Hprose\\Filter\\JSONRPC\\ClientFilter', 'HproseJSONRPCClientFilter'),
'hprosejsonrpcservicefilter' => array('Hprose\\Filter\\JSONRPC\\ServiceFilter', 'HproseJSONRPCServiceFilter'),
'hprosexmlrpcclientfilter' => array('Hprose\\Filter\\XMLRPC\\ClientFilter', 'HproseXMLRPCClientFilter'),
'hprosexmlrpcservicefilter' => array('Hprose\\Filter\\XMLRPC\\ServiceFilter', 'HproseXMLRPCServiceFilter'),
);

if (isset($oldVersionAliases[$key = strtolower($className)])) {
list($original, $alias) = $oldVersionAliases[$key];
class_alias($original, $alias);
return true;
}
return false;
});