main changes to enable register rest api endpoints from plugins

1. Enable rest api registering feature on main plugin file
  i.e, if I have a plugin named "erik", so we have a file named workflow/engine/plugins/erik.php
       and a folder workflow/engine/plugins/erik/
   - inside of setup method of plugin main class set: $this->enableRestService(true);

---- file: erik.php ----

class erikPlugin extends PMPlugin
{
  ...
  public function setup()
  {
    $this->registerMenu("processmaker", "menuerik.php");
    ...
    $this->enableRestService(true);  // <- this line enable Rest Service dispatching feature
  }
...

2. Create a folder: workflow/engine/plugins/erik/src/Services/Api/
   $ mkdir -p workflow/engine/plugins/erik/src/Services/Api/

3. Create a Restler class inside the folder created above
   i.e. creating a Hello class.

---- file: workflow/engine/plugins/erik/src/Services/Api/Hello.php -----

<?php
namespace Services\Api;
use Luracast\Restler\RestException;
use ProcessMaker\Services\Api;
use \ProcessMaker\Util;

/**
 * @protected
 */
class Hello extends Api
{
    /**
     * @url GET /world
     */
    public function world()
    {
        try {
            $hello = array(
                'message' => 'Hello world'
            );

            return $hello;
        } catch (\Exception $e) {
            throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage());
        }
    }
}

---------- end file ---------

4. Disable and enable the plugin named "erik" on ProcessMaker Admin Interface

5. Use the Rest Api defined inside the plugin
   you can access from a url something like that:
   format: http://<SERVER-ADDR>/api/1.0/<WORKSPACE>/plugin-<THE-PLUGIN-NAME>/hello/world

i.e.
   http://processmaker/api/1.0/workflow/plugin-erik/hello/world

Note.- to access this url that has the protected attribute into the class
       you need provide the access token into request header.
This commit is contained in:
Erik
2015-04-20 15:37:36 -05:00
committed by Julio Cesar Laura
parent 028baa647e
commit 5f4bec79b0
2 changed files with 54 additions and 36 deletions

View File

@@ -210,7 +210,7 @@ class WebApplication
*/
public function dispatchApiRequest($uri, $version = "1.0", $multipart = false, $inputExecute = '')
{
$this->initRest($uri, "1.0", $multipart);
$uri = $this->initRest($uri, "1.0", $multipart);
// to handle a request with "OPTIONS" method
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
@@ -328,50 +328,60 @@ class WebApplication
// Override $_SERVER['REQUEST_URI'] to Restler handles the modified url
if (! $isPluginRequest) { // if it is not a request for a plugin endpoint
// scan all api directory to find api classes
$classesList = Util\Common::rglob($apiDir . "/*");
// scan all api directory to find api classes
$classesList = Util\Common::rglob($apiDir . "/*");
foreach ($classesList as $classFile) {
if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
$relClassPath = str_replace('.php', '', str_replace($servicesDir, '', $classFile));
$namespace = '\\ProcessMaker\\Services\\' . str_replace(DS, '\\', $relClassPath);
$namespace = strpos($namespace, "//") === false? $namespace: str_replace("//", '', $namespace);
foreach ($classesList as $classFile) {
if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
$relClassPath = str_replace('.php', '', str_replace($servicesDir, '', $classFile));
$namespace = '\\ProcessMaker\\Services\\' . str_replace(DS, '\\', $relClassPath);
$namespace = strpos($namespace, "//") === false? $namespace: str_replace("//", '', $namespace);
//if (! class_exists($namespace)) {
require_once $classFile;
//}
//if (! class_exists($namespace)) {
require_once $classFile;
//}
$this->rest->addAPIClass($namespace);
$this->rest->addAPIClass($namespace);
}
}
// adding aliases for Restler
if (array_key_exists('alias', $config)) {
foreach ($config['alias'] as $alias => $aliasData) {
if (is_array($aliasData)) {
foreach ($aliasData as $label => $namespace) {
$namespace = '\\' . ltrim($namespace, '\\');
$this->rest->addAPIClass($namespace, $alias);
}
}
}
// adding aliases for Restler
if (array_key_exists('alias', $config)) {
foreach ($config['alias'] as $alias => $aliasData) {
if (is_array($aliasData)) {
foreach ($aliasData as $label => $namespace) {
$namespace = '\\' . ltrim($namespace, '\\');
$this->rest->addAPIClass($namespace, $alias);
}
//if (! $isPluginRequest) { // if it is not a request for a plugin endpoint
// hook to get rest api classes from plugins
if (class_exists('PMPluginRegistry')) {
$pluginRegistry = \PMPluginRegistry::loadSingleton(PATH_DATA_SITE . 'plugin.singleton');
$plugins = $pluginRegistry->getRegisteredRestServices();
if (! empty($plugins)) {
$pluginSourceDir = PATH_PLUGINS . $pluginName . DIRECTORY_SEPARATOR . 'src';
$loader = \Maveriks\Util\ClassLoader::getInstance();
$loader->add($pluginSourceDir);
if (is_array($plugins) && array_key_exists($pluginName, $plugins)) {
foreach ($plugins[$pluginName] as $class) {
if (class_exists($class['namespace'])) {
$this->rest->addAPIClass($class['namespace']);
}
}
}
}
} 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']);
// }
// }
// }
}
Services\OAuth2\Server::setWorkspace(SYS_SYS);
$this->rest->addAPIClass('\ProcessMaker\\Services\\OAuth2\\Server', 'oauth2');
return $uri;
}
public function parseApiRequestUri()
@@ -580,5 +590,10 @@ class WebApplication
throw $e;
}
}
}
public static function purgeRestApiCache($workspace)
{
@unlink(PATH_DATA . 'compiled' . DS . 'routes.php');
@unlink(PATH_DATA . 'sites' . DS . $workspace . DS . 'api-config.php');
}
}

View File

@@ -277,12 +277,15 @@ class PMPluginRegistry
$pluginSrcDir = PATH_PLUGINS . $detail->sNamespace . PATH_SEP . 'src';
if (is_dir($pluginSrcDir)) {
Bootstrap::registerDir($detail->sNamespace.'/src', $pluginSrcDir);
//Bootstrap::registerDir($detail->sNamespace.'/src', $pluginSrcDir);
$loader = \Maveriks\Util\ClassLoader::getInstance();
$loader->add($pluginSrcDir);
}
if (array_key_exists($detail->sNamespace, $this->_restServiceEnabled)
&& $this->_restServiceEnabled[$detail->sNamespace] == true
) {
$oPlugin->registerRestService();
}
@@ -1400,7 +1403,6 @@ class PMPluginRegistry
foreach ($classesList as $classFile) {
if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
$ns = str_replace(
DIRECTORY_SEPARATOR,
'\\',
@@ -1413,6 +1415,8 @@ class PMPluginRegistry
"filepath" => $classFile,
"namespace" => $ns
);
\Maveriks\WebApplication::purgeRestApiCache(basename(PATH_DATA_SITE));
}
}
}
@@ -1601,4 +1605,3 @@ class PMPluginRegistry
}
}
}