Restful Feature, improvements on rest-gen cli command and Disptacher

On Dispatcher:
- Now it is handling Cross Domain AJAX request, it is accepting requests with method OPTIONS
- Now the behaviour of rest server is modified by confuguration
- The dispatcher can be load configuraion from a processmaker core dir. for all workspaces
and for a determinated workspace

On Cli Command:
- Now it can generate api crud for processmaker root dir and a determinated workspace
- More improvements to handle correctly build request for a plugin or a workspace or core of pmos
This commit is contained in:
Erik Amaru Ortiz
2012-08-30 16:26:19 -04:00
parent 6e98245f3f
commit de62be5506
6 changed files with 539 additions and 357 deletions

View File

@@ -5169,7 +5169,7 @@ function getDirectorySize($path,$maxmtime=0)
*
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com>
*/
public function dispatchRestService($uri)
public function dispatchRestService($uri, $config = array())
{
require_once 'restler/restler.php';
@@ -5177,9 +5177,9 @@ function getDirectorySize($path,$maxmtime=0)
$rest->setSupportedFormats('JsonFormat', 'XmlFormat');
// getting all services class
$srvClasses = glob(PATH_SERVICES_REST . '*.php');
$srvClasses = glob(PATH_SERVICES_REST . '*.php');
$crudClasses = glob(PATH_SERVICES_REST . 'crud/*.php');
$srvClasses = array_merge($srvClasses, $crudClasses);
$srvClasses = array_merge($srvClasses, $crudClasses);
// hook to get rest api classes from plugins
if ( class_exists( 'PMPluginRegistry' ) ) {
@@ -5219,22 +5219,44 @@ function getDirectorySize($path,$maxmtime=0)
// resolving the class for current request
$uriPart = explode('/', $uri);
$requestedClass = '';
if (isset($uriPart[1])) {
$requestedClass = ucfirst($uriPart[1]);
}
if (class_exists('Services_Rest_' . $requestedClass)) {
$namespace = 'Services_Rest_';
} elseif (class_exists('Plugin_Services_Rest_' . $requestedClass)) {
$namespace = 'Plugin_Services_Rest_';
} else {
$namespace = '';
$namespace = '';
}
// end resolv.
// Send additional headers (if exists) configured on rest-config.ini
if (array_key_exists('HEADERS', $config)) {
foreach ($config['HEADERS'] as $name => $value) {
header("$name: $value");
}
}
// to handle a request with "OPTIONS" method
if (! empty($namespace) && $_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
$reflClass = new ReflectionClass($namespace . $requestedClass);
// if the rest class has not a "options" method
if (! $reflClass->hasMethod('options')) {
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEADERS');
header('Access-Control-Allow-Headers: authorization, content-type');
header("Access-Control-Allow-Credentials", "false");
header('Access-Control-Max-Age: 60');
exit();
}
}
// override global REQUEST_URI to pass to Restler library
$_SERVER['REQUEST_URI'] = '/' . strtolower($namespace) . ltrim($uri, '/');
// handle the rest request
$rest->handle();
}
}

View File

@@ -9,63 +9,91 @@
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com>
*/
include dirname(__FILE__) . '/../../../gulliver/core/Bootstrap.php';
include dirname(__FILE__) . '/../../../workflow/engine/PmBootstrap.php';
$basePath = realpath(dirname(__FILE__) . '/../../../');
include $basePath . '/gulliver/core/Bootstrap.php';
include $basePath . '/workflow/engine/PmBootstrap.php';
$config = array(
'path_trunk' => realpath(dirname(__FILE__) . '/../../../')
);
$bootstrap = new PmBootstrap($config);
$bootstrap = new PmBootstrap(array('path_trunk' => $basePath));
$bootstrap->registerClasses();
$bootstrap->configure();
if (! isset($argv[1])) {
$help = '$>' . $argv[0] . " [option] [plugin-name]\n";
$help .= "Avalaibles options:\n";
$help .= " build-api : Build Crud Rest API from ProcessMaker or a plugin.\n";
$help .= " gen-ini : Generates the rest config ini file.\n";
$help .= "* [plugin-name] : (Optional) to specify create crud api for a determiated plugin.\n\n";
$help = <<<EOT
Usage: {$argv[0]} [build-crud] [gen-ini] [-p <plugin name>] [-w <workspace name>]
Options:
build-crud : Task, build Rest Crud API.
gen-ini : Task, generates the rest config ini file.
-p : Especify a plugin to set as enviroment to perform the tasks.
-w : Especify a workspace to set as enviroment to perform the tasks.
EOT;
echo $help;
exit(0);
}
$restTool = new Service_Rest_RestTool();
$restTool->setBasePath(PATH_CORE);
try {
switch ($argv[1]) {
case 'build-api':
if (isset($argv[2])) {
// attemp create rest api from a plugin
if (! is_dir(PATH_PLUGINS . $argv[2])) {
throw new Exception(sprintf("Plugin '%s' doesn't exist.", $argv[2]));
}
$restTool->setBasePath(PATH_PLUGINS . $argv[2] . PATH_SEP);
}
$restTool->buildApi();
break;
case 'build-crud':
case 'gen-ini':
if (file_exists(PATH_CONFIG . '/rest-config.ini')) {
echo "The file 'rest-config.ini' already exits, overwrite (Y/n)? ";
$resp = trim(fgets(STDIN));
if (isset($argv[2])) {
if (! isset($argv[3])) {
throw new Exception("Missing option, need especify a valid argument after option '{$argv[2]}'");
}
if (strtolower($resp) != 'y') {
echo "Skipped\n";
exit(0);
switch ($argv[2]) {
case '-p':
// attempt create rest api from a plugin
if (! is_dir(PATH_PLUGINS . $argv[3])) {
throw new Exception(sprintf("Plugin '%s' doesn't exist.", $argv[3]));
}
$restTool->setBasePath($optPath = PATH_PLUGINS . $argv[3] . PATH_SEP);
break;
case '-w':
// attempt create rest api from a plugin
if (! is_dir(PATH_DATA . 'sites' . PATH_SEP . $argv[3])) {
throw new Exception(sprintf("Workspace '%s' doesn't exist.", $argv[3]));
}
$path = PATH_DATA . 'sites' . PATH_SEP . $argv[3] . PATH_SEP;
$restTool->setBasePath($path);
$restTool->setConfigFile($path . 'rest-config.ini');
$restTool->setDbXmlSchemaFile(PATH_CONFIG . 'schema.xml');
break;
default:
throw new Exception(sprintf("Invalid option '%s'", $argv[2]));
}
}
echo "Generating config ini file ... ";
$restTool->init();
$genFile = $restTool->buildConfigIni();
if ($argv[1] == 'build-crud') {
$restTool->buildCrudApi();
} else {
if (file_exists(PATH_CONFIG . '/rest-config.ini')) {
echo "The file 'rest-config.ini' already exits, overwrite (Y/n)? ";
$resp = trim(fgets(STDIN));
echo "DONE!\n";
echo "File generated: $genFile\n\n";
if (strtolower($resp) != 'y') {
echo "Skipped\n";
exit(0);
}
}
echo "Generating config ini file ... ";
$genFile = $restTool->buildConfigIni();
echo "DONE!\n";
echo "File generated: $genFile\n\n";
}
break;
default:
@@ -73,5 +101,5 @@ try {
break;
}
} catch (Exception $e) {
echo $e->getMessage() . "\n";
Service_Rest_RestTool::out($e->getMessage(), 'error');
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -2,7 +2,23 @@
class Services_Rest_Case
{
protected function get($id = '', $start=null, $limit=null, $type=null, $filter=null, $search=null, $process=null, $user=null, $status=null, $typeResource=null, $dateFrom=null, $dateTo=null)
public function get()
{
echo 'hello world';
}
public function options22()
{
echo 'opts';
}
public function post()
{
header('Content-Type: application/json');
echo '{"response": "hello post"}';
}
protected function get11($id = '', $start=null, $limit=null, $type=null, $filter=null, $search=null, $process=null, $user=null, $status=null, $typeResource=null, $dateFrom=null, $dateTo=null)
{
if (empty($id)) {
// getting all records.

View File

@@ -423,6 +423,28 @@
define('SERVER_NAME', $_SERVER ['SERVER_NAME']);
define('SERVER_PORT', $_SERVER ['SERVER_PORT']);
// verify configuration for rest service
if ($isRestRequest) {
// disable until confirm that rest is enabled & configured on rest-config.ini file
$isRestRequest = false;
$confFile = '';
// try load and getting rest configuration
if (file_exists(PATH_DATA_SITE . 'rest-config.ini')) {
$confFile = PATH_DATA_SITE . 'rest-config.ini';
} elseif (file_exists(PATH_CONFIG . 'rest-config.ini')) {
$confFile = PATH_CONFIG . 'rest-config.ini';
}
if (! empty($confFile) && $restConfig = @parse_ini_file($confFile, true)) {
if (array_key_exists('enable_service', $restConfig)) {
if ($restConfig['enable_service'] == 'true' || $restConfig['enable_service'] == '1') {
$isRestRequest = true; // rest service enabled
}
}
}
}
// load Plugins base class
G::LoadClass('plugin');
@@ -575,10 +597,6 @@
$isControllerCall = true;
}
}
// var_dump(SYS_SYS);
// var_dump(SYS_TARGET);
// var_dump($isRestRequest);
// die;
if (!$isControllerCall && ! file_exists($phpFile) && ! $isRestRequest) {
$_SESSION['phpFileNotFound'] = $_SERVER['REQUEST_URI'];
@@ -707,7 +725,7 @@
$controller->setHttpRequestData($_REQUEST);
$controller->call($controllerAction);
} elseif ($isRestRequest) {
G::dispatchRestService(SYS_TARGET);
G::dispatchRestService(SYS_TARGET, $restConfig);
} else {
require_once $phpFile;
}