File size: 6,106 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace Maveriks\Extension;

use Luracast\Restler\Defaults;
use Luracast\Restler\Format\JsonFormat;
use Luracast\Restler\Format\UrlEncodedFormat;
use ProcessMaker\Plugins\PluginRegistry;
use ProcessMaker\Services\Api;
use Luracast\Restler\RestException;

/**
 * Class Restler
 * Extension Restler class to implement in ProcessMaker
 *
 * @package Maveriks\Extension
 */
class Restler extends \Luracast\Restler\Restler
{
    public $flagMultipart = false;
    public $responseMultipart = array();
    public $inputExecute = '';

    protected $workspace;

    public function __construct($productionMode = false, $refreshCache = false)
    {
        parent::__construct($productionMode, $refreshCache);
    }

    /**
     * This method to set the value flag Multipart
     *
     * @param boolean $multipart. flag Multipart
     *
     * @access public
     * @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
     * @copyright Colosa - Bolivia
     *
     * @return void
     */
    public function setFlagMultipart($multipart = false)
    {
        $this->flagMultipart = ($multipart === true) ? true : false;
    }

    /**
     * This method to print or save the api response
     *
     * @access public
     * @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
     * @copyright Colosa - Bolivia
     *
     * @return void
     */
    protected function respond()
    {
        $this->dispatch('respond');
        //handle throttling
        if (Defaults::$throttle) {
            $elapsed = time() - $this->startTime;
            if (Defaults::$throttle / 1e3 > $elapsed) {
                usleep(1e6 * (Defaults::$throttle / 1e3 - $elapsed));
            }
        }
        if ($this->flagMultipart === true) {
            $responseTemp['status'] = $this->responseCode;
            $responseTemp['response'] = json_decode($this->responseData);
            $this->responseMultipart = $responseTemp;
        } else {
            echo $this->responseData;
        }
        $this->dispatch('complete');
    }

    /**
     * This method to set request data
     *
     * @param boolean $includeQueryParameters.
     *
     * @access public
     * @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
     * @copyright Colosa - Bolivia
     *
     * @return void
     */
    public function getRequestData($includeQueryParameters = true)
    {
        $get = UrlEncodedFormat::decoderTypeFix($_GET);
        if ($this->requestMethod == 'PUT'
            || $this->requestMethod == 'PATCH'
            || $this->requestMethod == 'POST'
        ) {
            if (!empty($this->requestData)) {
                return $includeQueryParameters
                    ? $this->requestData + $get
                    : $this->requestData;
            }

            if ($this->flagMultipart === false) {
                $r = file_get_contents('php://input');
                if (is_null($r)) {
                    return array(); //no body
                }
            } else {
                $r = $this->inputExecute;
            }
            if(!empty($r) || !$this->requestFormat instanceof JsonFormat){
                $r = $this->requestFormat->decode($r);
            }
            $r = is_array($r)
                ? array_merge($r, array(Defaults::$fullRequestDataName => $r))
                : array(Defaults::$fullRequestDataName => $r);
            return $includeQueryParameters
                ? $r + $get
                : $r;
        }
        return $includeQueryParameters ? $get : array(); //no body
    }

    /**
     * This method call the function message
     *
     * @param RestException $e. Exception the error
     *
     * @access public
     * @author Brayan Pereyra (Cochalo) <brayan@colosa.com>
     * @copyright Colosa - Bolivia
     *
     * @return void
     */
    public function setMessage(RestException $e)
    {
        $this->message($e);
    }

    public function setWorkspace($workspace)
    {
        $this->workspace = $workspace;
    }

    public function getWorkspace()
    {
        return $this->workspace;
    }

    protected function call()
    {
        $this->dispatch('call');
        $o = &$this->apiMethodInfo;
        $accessLevel = max(\Luracast\Restler\Defaults::$apiAccessLevel, $o->accessLevel);
        $object = \Luracast\Restler\Scope::get($o->className);
        switch ($accessLevel) {
            case 3 : //protected method
                $reflectionMethod = new \ReflectionMethod(
                    $object,
                    $o->methodName
                );
                $reflectionMethod->setAccessible(true);
                $result = $reflectionMethod->invokeArgs(
                    $object,
                    $o->parameters
                );
                break;
            default :
                $object = $this->reviewApiExtensions($object, $o->className);
                $result = call_user_func_array(array(
                    $object,
                    $o->methodName
                ), $o->parameters);
        }
        $this->responseData = $result;
    }

    /**
     * Review the API extensions, if the extension exists a new instance is 
     * returned.
     * 
     * @param object $object
     * @param string $className
     * @return \Maveriks\Extension\classExtName
     */
    public function reviewApiExtensions($object, $className)
    {
        $classReflection = new \ReflectionClass($object);
        $classShortName = $classReflection->getShortName();
        $registry = PluginRegistry::loadSingleton();
        $pluginsApiExtend = $registry->getExtendsRestService($classShortName);
        if ($pluginsApiExtend) {
            $classFilePath = $pluginsApiExtend->filePath;
            if (file_exists($classFilePath)) {
                require_once($classFilePath);
                $classExtName = $pluginsApiExtend->classExtend;
                $newObjectExt = new $classExtName();
                if (is_subclass_of($newObjectExt, $className)) {
                    $object = $newObjectExt;
                }
            }
        }
        return $object;
    }

}