Skip to content

Commit

Permalink
server-php: Now supports extensions just as Node server
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Nov 28, 2016
1 parent 1b2eb2e commit c4fe142
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
26 changes: 24 additions & 2 deletions src/server/php/Core/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,18 @@ final protected static function _loadAPI() {
require($path . $file);

$className = 'OSjs\\Modules\\API\\' . pathinfo($file, PATHINFO_FILENAME);
foreach ( get_class_methods($className) as $methodName ) {
self::$API[$methodName] = $className;
self::registerAPIMethods($className);
}
}

foreach ( self::getPackages() as $p => $pkg ) {
if ( $pkg['type'] == 'extension' ) {
$path = DIR_ROOT . '/src/packages/' . $p . '/api.php';
if ( file_exists($path) ) {
$className = require($path);
if ( is_string($className) && strlen($className) > 3 ) {
self::registerAPIMethods($className);
}
}
}
}
Expand Down Expand Up @@ -141,6 +151,18 @@ final public static function GetVFSModules() {
final public static function shutdown() {
}

/**
* Takes all public methods from a class and registeres them in
* the API
*/
final public static function registerAPIMethods($className) {
foreach ( get_class_methods($className) as $methodName ) {
if ( substr($methodName, 0, 1) != '_' ) {
self::$API[$methodName] = $className;
}
}
}

/**
* Startup handler
*/
Expand Down
17 changes: 13 additions & 4 deletions src/server/php/Modules/API/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,23 @@ public static function application(Request $request) {
$aa = empty($request->data['args']) ? Array() : $request->data['args'];

$apath = DIR_PACKAGES . '/' . $path . '/api.php';

if ( !file_exists($apath) ) {
throw new Exception("No such application or API file not available ({$an})!");
} else {
require $apath;
if ( !class_exists($an) || !method_exists($an, 'call') ) {
$error = 'Application API missing!';
$className = require $apath;
if ( is_string($className) && strlen($className) > 3 ) {
if ( method_exists($className, $am) ) {
return $className::$am($request, $aa);
} else {
throw new Exception('Application API missing!');
}
} else {
return $an::call($am, $aa);
if ( !class_exists($an) || !method_exists($an, 'call') ) {
throw new Exception('Application API missing!');
} else {
return $an::call($am, $aa);
}
}
}

Expand Down

0 comments on commit c4fe142

Please sign in to comment.