UPDATE, Update Restler implementation ver 3.0

- First Funcional Commit to implement Restler 3.0
- PM supports now urls requests like:
	GET /api/<workspace_name>/class_name/method?param1=value1
   example:
        GET /api/workflow/class_name/mothod?param1=value1

   There is an equivalent expresion for url above

	GET /<workspace_name>-api/class_name/mothod?param1=value1
   example:
	GET /workflow-api/class_name/mothod?param1=value1

 * and all route expressions that Restler supports
This commit is contained in:
Erik Amaru Ortiz
2013-08-22 11:10:04 -04:00
parent da8e762256
commit d63fb635d6
4 changed files with 122 additions and 18 deletions

View File

@@ -6,6 +6,6 @@
"license": "GNU Affero General Public License version 3", "license": "GNU Affero General Public License version 3",
"require": { "require": {
"luracast/restler" : "3.0.0" "luracast/restler" : "dev-master"
} }
} }

View File

@@ -1059,17 +1059,57 @@ class Bootstrap
} }
return $res; return $res;
} }
//use Luracast\Restler\Format\XmlFormat as XmlFormat;
/** /**
* This method allow dispatch rest services using 'Restler' thirdparty library * This method dispatch rest/api services
* it implement "Restler" library
* *
* @param $url string Contains the url request
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com> * @author Erik Amaru Ortiz <aortiz.erik@gmail.com>
*/ */
public function dispatchRestService($uri, $config, $apiClassesPath = '') public function dispatchApiService($uri, $config, $apiClassesPath = '')
{ {
require_once 'restler/restler.php'; $rest = new Luracast\Restler\Restler();
$dataUri = explode('/', $uri);
array_shift($dataUri);
$reqClass = ucfirst(array_shift($dataUri));
$servicesDir = PATH_CORE . 'services' . PATH_SEP;
$apiDir = $servicesDir . 'api' . PATH_SEP;
$namespace = 'Services_Api_';
$classFile = $apiDir . $namespace . $reqClass . '.php';
if (! file_exists($classFile)) {
//throw new Luracast\Restler\RestException(404, "Invalid Service Request");
$rest->handleError(404);
die;
}
require_once $classFile;
$_SERVER['REQUEST_URI'] = $uri;
$rest->setSupportedFormats('JsonFormat', 'XmlFormat');
$rest->addAPIClass($namespace . $reqClass);
$rest->handle();
die;
// OLD CODE FROM HERE
$rest = new Restler();
$rest->setSupportedFormats('JsonFormat', 'XmlFormat'); $rest->setSupportedFormats('JsonFormat', 'XmlFormat');
// getting all services class // getting all services class
$restClasses = array(); $restClasses = array();
@@ -2275,18 +2315,36 @@ class Bootstrap
} }
/** /**
* This function parse a particular api/rest request
* *
* @param unknown_type $requestUri * Example url:
* GET /api/workflowdemo/user/rol?id=0000000000000000001
* POST /api/workflowdemo/cases
*
* - These urls are equivalents
* GET /api/<workspaceName>/cases/start
* GET /<workspaceName>-api/cases/start
*
* @param $url
* @return array return url parsed data
*/ */
public function parseRestUri($requestUri) public function parseRestUri($url)
{ {
array_shift($url);
$sysTemp = array_shift($url);
if ($sysTemp == 'api') {
$sysTemp = array_shift($url);
} elseif (strpos($sysTemp, '-') !== false) {
list($sysTemp,) = explode('-', $sysTemp);
}
$args = array(); $args = array();
//$args['SYS_TEMP'] = $requestUri[1]; define('SYS_TEMP', $sysTemp);
define('SYS_TEMP', $requestUri[2]);
$restUri = ''; $restUri = '';
for ($i = 3; $i < count($requestUri); $i++) { foreach ($url as $urlPart) {
$restUri .= '/' . $requestUri[$i]; $restUri .= '/' . $urlPart;
} }
$args['SYS_LANG'] = 'en'; // TODO, this can be set from http header $args['SYS_LANG'] = 'en'; // TODO, this can be set from http header
@@ -3016,5 +3074,27 @@ class Bootstrap
{ {
return strtoupper(PHP_OS) == "LINUX"; return strtoupper(PHP_OS) == "LINUX";
} }
public static function initVendors()
{
if (! is_dir(PATH_TRUNK . 'vendor')) {
if (file_exists(PATH_TRUNK . 'composer.phar')) {
throw new Exception(
"ERROR: Verdors are missing!\n" .
"Please execute the following command to install vendors for the environment:\n" .
"$>php composer.phar install"
);
} else {
throw new Exception(
"ERROR: Verdors are missing!\n" .
"Please execute the following commands to prepare/install vendors for the environment:\n" .
"$>curl -sS https://getcomposer.org/installer | php\n" .
"$>php composer.phar install"
);
}
}
require_once PATH_TRUNK . 'vendor' . PATH_SEP . "autoload.php";
}
} }

View File

@@ -0,0 +1,21 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: erik
* Date: 8/21/13
* Time: 4:55 PM
* To change this template use File | Settings | File Templates.
*/
class Services_Api_Say
{
public function hello($to='world')
{
return array('success'=>true, "message"=>"Hello $to!");
}
public function hi($to, $name)
{
return "Hi $to -> $name";
}
}

View File

@@ -236,6 +236,8 @@ set_include_path( PATH_CORE . PATH_SEPARATOR .
get_include_path() get_include_path()
); );
Bootstrap::initVendors();
/** /**
* Global definitions, before it was the defines.php file * Global definitions, before it was the defines.php file
*/ */
@@ -351,7 +353,7 @@ $virtualURITable['/html2ps_pdf/(*)'] = PATH_THIRDPARTY . 'html2ps_pdf/';
//$virtualURITable['/images/'] = 'errorFile'; //$virtualURITable['/images/'] = 'errorFile';
//$virtualURITable['/skins/'] = 'errorFile'; //$virtualURITable['/skins/'] = 'errorFile';
//$virtualURITable['/files/'] = 'errorFile'; //$virtualURITable['/files/'] = 'errorFile';
$virtualURITable['/rest/(*)'] = 'rest-service'; $virtualURITable['/(*)api/(*)'] = 'api-service';
$virtualURITable["/update/(*)"] = ($skinPathUpdate != "")? $skinPathUpdate : PATH_GULLIVER_HOME . "methods" . PATH_SEP . "update" . PATH_SEP; $virtualURITable["/update/(*)"] = ($skinPathUpdate != "")? $skinPathUpdate : PATH_GULLIVER_HOME . "methods" . PATH_SEP . "update" . PATH_SEP;
//$virtualURITable['/(*)'] = PATH_HTML; //$virtualURITable['/(*)'] = PATH_HTML;
$virtualURITable['/css/(*)'] = PATH_HTML . 'css/'; //ugly $virtualURITable['/css/(*)'] = PATH_HTML . 'css/'; //ugly
@@ -438,7 +440,7 @@ if (Bootstrap::virtualURI( $_SERVER['REQUEST_URI'], $virtualURITable, $realPath
break; break;
default: default:
//Process files loaded with tag head in HTML //Process files loaded with tag head in HTML
if (substr( $realPath, 0, 12 ) == 'rest-service') { if (substr( $realPath, 0, 11 ) == 'api-service') {
$isRestRequest = true; $isRestRequest = true;
} else { } else {
$realPath = explode( '?', $realPath ); $realPath = explode( '?', $realPath );
@@ -635,7 +637,7 @@ Bootstrap::LoadClass( 'memcached' );
$memcache = & PMmemcached::getSingleton( SYS_SYS ); $memcache = & PMmemcached::getSingleton( SYS_SYS );
// verify configuration for rest service // verify configuration for rest service
if ($isRestRequest) { /*if ($isRestRequest) {
// disable until confirm that rest is enabled & configured on rest-config.ini file // disable until confirm that rest is enabled & configured on rest-config.ini file
$isRestRequest = false; $isRestRequest = false;
$confFile = ''; $confFile = '';
@@ -655,7 +657,7 @@ if ($isRestRequest) {
} }
} }
} }
} }*/
// load Plugins base class // load Plugins base class
Bootstrap::LoadClass( 'plugin' ); Bootstrap::LoadClass( 'plugin' );
@@ -1002,9 +1004,10 @@ if (! defined( 'EXECUTE_BY_CRON' )) {
$controller->call($controllerAction); $controller->call($controllerAction);
} elseif ($isRestRequest) { } elseif ($isRestRequest) {
$restConfig = array();
//NewRelic Snippet - By JHL //NewRelic Snippet - By JHL
transactionLog($restConfig.$restApiClassPath.SYS_TARGET); //transactionLog($restConfig.PATH_DATA_SITE.SYS_TARGET); // ====> ??? this concat is very rare
Bootstrap::dispatchRestService( SYS_TARGET, $restConfig, $restApiClassPath ); Bootstrap::dispatchApiService(SYS_TARGET, $restConfig, PATH_DATA_SITE);
} else { } else {
//NewRelic Snippet - By JHL //NewRelic Snippet - By JHL
transactionLog($phpFile); transactionLog($phpFile);