Adding Rest Api Service Support
Rest Service on plugins
-----------------------
1. enable service
add the following line in plugin __constructor main class
$this->enableRestService(true);
2. Create the sources directory structure by example:
if you plugin is named myPlugin
myPlugin
|--src
|--Services
|--Api
|--MyPlugin
|--Test.php
Where Test.php is a Restler class
This commit is contained in:
@@ -66,7 +66,7 @@ class Bootstrap
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerDir($name, $dir)
|
public static function registerDir($name, $dir)
|
||||||
{
|
{
|
||||||
BootStrap::$includePaths[$name] = $dir;
|
BootStrap::$includePaths[$name] = $dir;
|
||||||
}
|
}
|
||||||
@@ -1108,19 +1108,16 @@ class Bootstrap
|
|||||||
*/
|
*/
|
||||||
$pmOauthClientId = 'x-pm-local-client';
|
$pmOauthClientId = 'x-pm-local-client';
|
||||||
|
|
||||||
// load Api class
|
/*
|
||||||
G::loadClass('api');
|
* Load Api ini file for Rest Service
|
||||||
|
*/
|
||||||
// Load Api ini file for Rest Service
|
|
||||||
|
|
||||||
$apiIniConf = array();
|
$apiIniConf = array();
|
||||||
|
|
||||||
if (file_exists($apiIniFile)) {
|
if (file_exists($apiIniFile)) {
|
||||||
$apiIniConf = self::parseIniFile($apiIniFile);
|
$apiIniConf = self::parseIniFile($apiIniFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setting current workspace to Api class
|
// Setting current workspace to Api class
|
||||||
\ProcessMaker\Api::setWorkspace(SYS_SYS);
|
\ProcessMaker\Services\Api::setWorkspace(SYS_SYS);
|
||||||
// TODO remove this setting on the future, it is not needed, but if it is not present is throwing a warning
|
// TODO remove this setting on the future, it is not needed, but if it is not present is throwing a warning
|
||||||
Luracast\Restler\Format\HtmlFormat::$viewPath = $servicesDir . 'oauth2/views';
|
Luracast\Restler\Format\HtmlFormat::$viewPath = $servicesDir . 'oauth2/views';
|
||||||
|
|
||||||
@@ -1144,33 +1141,56 @@ class Bootstrap
|
|||||||
$rest->setOverridingFormats('HtmlFormat', 'JsonFormat', 'UploadFormat');
|
$rest->setOverridingFormats('HtmlFormat', 'JsonFormat', 'UploadFormat');
|
||||||
|
|
||||||
// Override $_SERVER['REQUEST_URI'] to Restler handles the current url correctly
|
// Override $_SERVER['REQUEST_URI'] to Restler handles the current url correctly
|
||||||
$_SERVER['REQUEST_URI'] = $uri;
|
|
||||||
|
|
||||||
// scan all api directory to find api classes
|
$isPluginRequest = strpos($uri, '/plugin-') !== false ? true : false;
|
||||||
$classesList = Bootstrap::rglob('*', 0, $apiDir);
|
|
||||||
|
|
||||||
foreach ($classesList as $classFile) {
|
if ($isPluginRequest) {
|
||||||
if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
|
$tmp = explode('/', $uri);
|
||||||
$namespace = '\\Services\\' . str_replace(
|
array_shift($tmp);
|
||||||
DIRECTORY_SEPARATOR,
|
$tmp = array_shift($tmp);
|
||||||
'\\',
|
$tmp = explode('-', $tmp);
|
||||||
str_replace('.php', '', str_replace($servicesDir, '', $classFile))
|
$pluginName = $tmp[1];
|
||||||
);
|
$uri = str_replace('/plugin-'.$pluginName, '', $uri);
|
||||||
//var_dump($namespace);
|
|
||||||
$rest->addAPIClass($namespace);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// adding aliases for Restler
|
$_SERVER['REQUEST_URI'] = $uri;
|
||||||
|
|
||||||
if (array_key_exists('alias', $apiIniConf)) {
|
if (! $isPluginRequest) { // if it is not a request for a plugin endpoint
|
||||||
//print_r($apiIniConf['alias']); die;
|
// scan all api directory to find api classes
|
||||||
foreach ($apiIniConf['alias'] as $alias => $aliasData) {
|
$classesList = Bootstrap::rglob('*', 0, $apiDir);
|
||||||
if (is_array($aliasData)) {
|
|
||||||
foreach ($aliasData as $label => $namespace) {
|
foreach ($classesList as $classFile) {
|
||||||
$namespace = '\\' . ltrim($namespace, '\\');
|
if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
|
||||||
//var_dump($namespace, $alias);
|
$namespace = '\\Services\\' . str_replace(
|
||||||
$rest->addAPIClass($namespace, $alias);
|
DIRECTORY_SEPARATOR,
|
||||||
|
'\\',
|
||||||
|
str_replace('.php', '', str_replace($servicesDir, '', $classFile))
|
||||||
|
);
|
||||||
|
//var_dump($namespace);
|
||||||
|
$rest->addAPIClass($namespace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// adding aliases for Restler
|
||||||
|
if (array_key_exists('alias', $apiIniConf)) {
|
||||||
|
foreach ($apiIniConf['alias'] as $alias => $aliasData) {
|
||||||
|
if (is_array($aliasData)) {
|
||||||
|
foreach ($aliasData as $label => $namespace) {
|
||||||
|
$namespace = '\\' . ltrim($namespace, '\\');
|
||||||
|
$rest->addAPIClass($namespace, $alias);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// hook to get rest api classes from plugins
|
||||||
|
if (class_exists('PMPluginRegistry')) {
|
||||||
|
$pluginRegistry = & PMPluginRegistry::getSingleton();
|
||||||
|
$plugins = $pluginRegistry->getRegisteredRestServices();
|
||||||
|
|
||||||
|
if (is_array($plugins) && array_key_exists($pluginName, $plugins)) {
|
||||||
|
foreach ($plugins[$pluginName] as $class) {
|
||||||
|
$rest->addAPIClass($class['namespace']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -341,10 +341,10 @@ class PMPlugin
|
|||||||
* @param array/string $pluginJsFile
|
* @param array/string $pluginJsFile
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function registerRestService($classname, $path = '')
|
function registerRestService()
|
||||||
{
|
{
|
||||||
$oPluginRegistry =& PMPluginRegistry::getSingleton();
|
$oPluginRegistry =& PMPluginRegistry::getSingleton();
|
||||||
$oPluginRegistry->registerRestService($this->sNamespace, $classname, $path);
|
$oPluginRegistry->registerRestService($this->sNamespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -360,6 +360,12 @@ class PMPlugin
|
|||||||
$oPluginRegistry =& PMPluginRegistry::getSingleton();
|
$oPluginRegistry =& PMPluginRegistry::getSingleton();
|
||||||
$oPluginRegistry->unregisterRestService($this->sNamespace, $classname, $path);
|
$oPluginRegistry->unregisterRestService($this->sNamespace, $classname, $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function enableRestService($enable)
|
||||||
|
{
|
||||||
|
$oPluginRegistry =& PMPluginRegistry::getSingleton();
|
||||||
|
$oPluginRegistry->enableRestService($this->sNamespace, $enable);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class menuDetail
|
class menuDetail
|
||||||
|
|||||||
@@ -108,6 +108,8 @@ class PMPluginRegistry
|
|||||||
*/
|
*/
|
||||||
private $_restServices = array ();
|
private $_restServices = array ();
|
||||||
|
|
||||||
|
private $_restServiceEnabled = array();
|
||||||
|
|
||||||
private static $instance = null;
|
private static $instance = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -240,6 +242,27 @@ class PMPluginRegistry
|
|||||||
if (method_exists( $oPlugin, 'enable' )) {
|
if (method_exists( $oPlugin, 'enable' )) {
|
||||||
$oPlugin->enable();
|
$oPlugin->enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 1. register <plugin-dir>/src directory for autoloading
|
||||||
|
* 2. verify if rest service is enabled
|
||||||
|
* 3. register rest service directory
|
||||||
|
*/
|
||||||
|
$pluginSrcDir = PATH_PLUGINS . $detail->sNamespace . PATH_SEP . 'src';
|
||||||
|
|
||||||
|
if (is_dir($pluginSrcDir)) {
|
||||||
|
Bootstrap::registerDir($detail->sNamespace.'/src', $pluginSrcDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists($detail->sNamespace, $this->_restServiceEnabled)
|
||||||
|
&& $this->_restServiceEnabled[$detail->sNamespace] == true
|
||||||
|
) {
|
||||||
|
$oPlugin->registerRestService();
|
||||||
|
ProcessMaker\Util\Logger::log("plugin ".$detail->sNamespace." -> rest enabled");
|
||||||
|
} else {
|
||||||
|
ProcessMaker\Util\Logger::log("plugin ".$detail->sNamespace." -> rest not enabled");
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1339,27 +1362,39 @@ class PMPluginRegistry
|
|||||||
* @param string $path (optional) the class file path, if it is not set the system will try resolve the
|
* @param string $path (optional) the class file path, if it is not set the system will try resolve the
|
||||||
* file path from its classname.
|
* file path from its classname.
|
||||||
*/
|
*/
|
||||||
public function registerRestService ($sNamespace, $classname, $path = '')
|
public function registerRestService($sNamespace)
|
||||||
{
|
{
|
||||||
$restService = new StdClass();
|
$baseSrcPluginPath = PATH_PLUGINS . $sNamespace . PATH_SEP . "src";
|
||||||
$restService->sNamespace = $sNamespace;
|
$apiPath = PATH_SEP . "Services" . PATH_SEP . "Api" . PATH_SEP . ucfirst($sNamespace);
|
||||||
$restService->classname = $classname;
|
$classesList = Bootstrap::rglob('*', 0, $baseSrcPluginPath . $apiPath);
|
||||||
|
|
||||||
if (empty( $path )) {
|
foreach ($classesList as $classFile) {
|
||||||
$path = PATH_PLUGINS . $restService->sNamespace . "/services/rest/$classname.php";
|
if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
|
||||||
|
|
||||||
if (! file_exists( $path )) {
|
$ns = str_replace(
|
||||||
$path = PATH_PLUGINS . $restService->sNamespace . "/services/rest/crud/$classname.php";
|
DIRECTORY_SEPARATOR,
|
||||||
|
'\\',
|
||||||
|
str_replace('.php', '', str_replace($baseSrcPluginPath, '', $classFile))
|
||||||
|
);
|
||||||
|
|
||||||
|
ProcessMaker\Util\Logger::log("Namespace found: " . $ns);
|
||||||
|
|
||||||
|
// Ensure that is registering only existent classes.
|
||||||
|
if (class_exists($ns)) {
|
||||||
|
$this->_restServices[strtolower($sNamespace)][] = array(
|
||||||
|
"filepath" => $classFile,
|
||||||
|
"namespace" => $ns
|
||||||
|
);
|
||||||
|
ProcessMaker\Util\Logger::log("class exists: YES");
|
||||||
|
} else {
|
||||||
|
ProcessMaker\Util\Logger::log("class exists: NO");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ProcessMaker\Util\Logger::log($this->_restServices);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! file_exists( $path )) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$restService->path = $path;
|
|
||||||
$this->_restServices[] = $restService;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1370,31 +1405,14 @@ class PMPluginRegistry
|
|||||||
*/
|
*/
|
||||||
public function unregisterRestService ($sNamespace)
|
public function unregisterRestService ($sNamespace)
|
||||||
{
|
{
|
||||||
foreach ($this->_restServices as $i => $service) {
|
unset($this->_restServices[$sNamespace]);
|
||||||
if ($sNamespace == $service->sNamespace) {
|
|
||||||
unset( $this->_restServices[$i] );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Re-index when all js were unregistered
|
|
||||||
$this->_restServices = array_values( $this->_restServices );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRegisteredRestServices ()
|
public function getRegisteredRestServices()
|
||||||
{
|
{
|
||||||
return $this->_restServices;
|
return $this->_restServices;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRegisteredRestClassFiles ()
|
|
||||||
{
|
|
||||||
$restClassFiles = array ();
|
|
||||||
|
|
||||||
foreach ($this->_restServices as $restService) {
|
|
||||||
$restClassFiles[] = $restService->path;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $restClassFiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return all dashboard pages
|
* return all dashboard pages
|
||||||
*
|
*
|
||||||
@@ -1435,6 +1453,16 @@ class PMPluginRegistry
|
|||||||
}
|
}
|
||||||
$language->updateLanguagePlugin($namePlugin, SYS_LANG);
|
$language->updateLanguagePlugin($namePlugin, SYS_LANG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to enable rest service for plugins
|
||||||
|
* @param string $sNamespace
|
||||||
|
* @param bool $enable
|
||||||
|
*/
|
||||||
|
function enableRestService($sNamespace, $enable)
|
||||||
|
{
|
||||||
|
$this->_restServiceEnabled[$sNamespace] = $enable;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProcessMaker;
|
namespace ProcessMaker\Services;
|
||||||
|
|
||||||
class Api
|
class Api
|
||||||
{
|
{
|
||||||
@@ -31,9 +31,6 @@ class Api
|
|||||||
|
|
||||||
public function getUserId()
|
public function getUserId()
|
||||||
{
|
{
|
||||||
//return self::$userId;
|
return \Services\Api\OAuth2\Server::getUserId();
|
||||||
|
|
||||||
return \Api\OAuth2\Server::getUserId();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
40
workflow/engine/src/ProcessMaker/Util/Logger.php
Normal file
40
workflow/engine/src/ProcessMaker/Util/Logger.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ProcessMaker\Util;
|
||||||
|
|
||||||
|
class Logger
|
||||||
|
{
|
||||||
|
private static $instance;
|
||||||
|
private $logFile;
|
||||||
|
private $fp;
|
||||||
|
|
||||||
|
protected function __construct()
|
||||||
|
{
|
||||||
|
$this->logFile = sys_get_temp_dir() . '/processmaker.log';
|
||||||
|
$this->fp = fopen($this->logFile, "a+");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getInstance()
|
||||||
|
{
|
||||||
|
if (is_null(self::$instance)) {
|
||||||
|
self::$instance = new Logger();
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setLog($data)
|
||||||
|
{
|
||||||
|
if (! is_string($data)) {
|
||||||
|
$data = print_r($data, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
fwrite($this->fp, "PM LOG: ".date('Y-m-d H:i:s') . " " . $data . PHP_EOL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function log($data)
|
||||||
|
{
|
||||||
|
$me = Logger::getInstance();
|
||||||
|
$me->setLog($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Services\Api\ProcessMaker;
|
namespace Services\Api\ProcessMaker;
|
||||||
|
|
||||||
use \ProcessMaker\Api;
|
use \ProcessMaker\Services\Api;
|
||||||
use \Luracast\Restler\RestException;
|
use \Luracast\Restler\RestException;
|
||||||
|
|
||||||
//TODO we need Refactor this class
|
//TODO we need Refactor this class
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Services\Api\ProcessMaker\Project;
|
namespace Services\Api\ProcessMaker\Project;
|
||||||
|
|
||||||
use \ProcessMaker\Api;
|
use \ProcessMaker\Services\Api;
|
||||||
use \Luracast\Restler\RestException;
|
use \Luracast\Restler\RestException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,42 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Services\Api\ProcessMaker\Project\Activity;
|
namespace Services\Api\ProcessMaker\Project\Activity;
|
||||||
|
|
||||||
use \ProcessMaker\Api;
|
use \ProcessMaker\Services\Api;
|
||||||
use \Luracast\Restler\RestException;
|
use \Luracast\Restler\RestException;
|
||||||
|
|
||||||
class StepStructure
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var string {@from body}{@min 32}{@max 32}
|
|
||||||
*/
|
|
||||||
public $step_uid;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string {@from body}{@choice DYNAFORM,INPUT_DOCUMENT,OUTPUT_DOCUMENT}
|
|
||||||
*/
|
|
||||||
public $step_type_obj;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string {@from body}{@min 32}{@max 32}
|
|
||||||
*/
|
|
||||||
public $step_uid_obj;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
public $step_condition;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int {@from body}{@min 1}
|
|
||||||
*/
|
|
||||||
public $step_position;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string {@from body}{@choice EDIT,VIEW}
|
|
||||||
*/
|
|
||||||
public $step_mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project\Activity\Step Api Controller
|
* Project\Activity\Step Api Controller
|
||||||
*
|
*
|
||||||
@@ -122,3 +89,35 @@ class Step extends Api
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class StepStructure
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string {@from body}{@min 32}{@max 32}
|
||||||
|
*/
|
||||||
|
public $step_uid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string {@from body}{@choice DYNAFORM,INPUT_DOCUMENT,OUTPUT_DOCUMENT}
|
||||||
|
*/
|
||||||
|
public $step_type_obj;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string {@from body}{@min 32}{@max 32}
|
||||||
|
*/
|
||||||
|
public $step_uid_obj;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $step_condition;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int {@from body}{@min 1}
|
||||||
|
*/
|
||||||
|
public $step_position;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string {@from body}{@choice EDIT,VIEW}
|
||||||
|
*/
|
||||||
|
public $step_mode;
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Services\Api\ProcessMaker;
|
namespace Services\Api\ProcessMaker;
|
||||||
|
|
||||||
use \ProcessMaker\Api;
|
use \ProcessMaker\Services\Api;
|
||||||
use \Luracast\Restler\RestException;
|
use \Luracast\Restler\RestException;
|
||||||
|
|
||||||
class Test extends Api
|
class Test extends Api
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Services\Api\ProcessMaker;
|
namespace Services\Api\ProcessMaker;
|
||||||
|
|
||||||
use \ProcessMaker\Api;
|
use \ProcessMaker\Services\Api;
|
||||||
use \Luracast\Restler\RestException;
|
use \Luracast\Restler\RestException;
|
||||||
|
|
||||||
class Test2 extends Api
|
class Test2 extends Api
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Services\Api\ProcessMaker;
|
namespace Services\Api\ProcessMaker;
|
||||||
|
|
||||||
use \ProcessMaker\Api;
|
use \ProcessMaker\Services\Api;
|
||||||
use \Luracast\Restler\RestException;
|
use \Luracast\Restler\RestException;
|
||||||
|
|
||||||
class Test3 extends Api
|
class Test3 extends Api
|
||||||
|
|||||||
Reference in New Issue
Block a user