Add any operators online and JSONP support

closes #1, closes #2
This commit is contained in:
everyx 2017-03-18 21:56:45 +08:00
parent e532b91378
commit 8e5d1f6e47
4 changed files with 74 additions and 20 deletions

View File

@ -28,6 +28,7 @@ namespace Everyx\Mibew\Plugin\OperatorStatus\Controller;
use Mibew\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Operator Status actions
@ -40,27 +41,55 @@ class OperatorStatusController extends AbstractController
* @param Request $request
* @return Response Rendered page content
*/
public function indexAction(Request $request)
public function isOperatorOnlineAction(Request $request)
{
$is_online = "true";
$is_online = false;
$opcode = $request->attributes->get('opcode');
$online_operators = get_online_operators();
if ( count($online_operators) == 0 ) {
$is_online = "false";
} else if ( !empty($opcode) ) {
$is_online = "false";
foreach ($online_operators as $item) {
if ($item['code'] == $opcode) {
$is_online = "true";
if ($opcode == $item['code']) {
$is_online = true;
break;
}
}
$callback = $request->query->get('callback');
return $this->prepareResponse($is_online, $callback);
}
$response = new Response($is_online);
/**
* Returns true or false of whether an operator is online or not.
*
* @param Request $request
* @return Response Rendered page content
*/
public function hasOnlineOperatorsAction(Request $request)
{
$is_online = has_online_operators();
$callback = $request->query->get('callback');
return $this->prepareResponse($is_online, $callback);
}
/**
* Returns prepared response: JSONP or plain text.
*
* @param Boolean $is_online
* @param String $callback
* @return Response Rendered page content
*/
private function prepareResponse($is_online, $callback) {
$response = NULL;
if ( empty($callback) ) {
$response = new Response( $is_online ? 'true' : 'false' );
$response->headers->set('Access-Control-Allow-Origin', '*');
} else {
$response = new JsonResponse($is_online);
$response->setCallback($callback);
}
return $response;
}
}

View File

@ -46,6 +46,6 @@ class Plugin extends \Mibew\Plugin\AbstractPlugin implements \Mibew\Plugin\Plugi
*/
public static function getVersion()
{
return '0.2.0';
return '0.3.0';
}
}

View File

@ -3,8 +3,28 @@ Plugin for Mibew, get statement based on the availability of operators.
# Useage
Request `<MIBEW-BASE-URL>/opstatus/<OPERATOR-CODE>`, your will get `true` when operator
is online or `false` when operator is offline.
1. Get any operators online status:
* request URL:`<MIBEW-BASE-URL>/opstatus`.
* return `true` when any operators is online and `false` when not.
2. Get an operator online status by operator code:
* Request URL: `<MIBEW-BASE-URL>/opstatus/<OPERATOR-CODE>`.
* return `true` when operator is online or `false` when not.
3. Use callback parameter:
Just inset `<script>` tag and set `src` to URL above and add `callback` parameter
* `<MIBEW-BASE-URL>/opstatus?callback=<CALLBACK_FUNCTION>`
* `<MIBEW-BASE-URL>/opstatus/<OPERATOR-CODE>?callback=<CALLBACK_FUNCTION>`
will return bellow and run `CALLBACK_FUNCTION` automatically.
```javascript
/**/CALLBACK_FUNCTION(status);
```
# Install

View File

@ -1,4 +1,9 @@
everyx_operator_status:
everyx_operator_status_has_online_operators:
path: /opstatus
defaults:
_controller: Everyx\Mibew\Plugin\OperatorStatus\Controller\OperatorStatusController::hasOnlineOperatorsAction
everyx_operator_status_is_operator_online:
path: /opstatus/{opcode}
defaults:
_controller: Everyx\Mibew\Plugin\OperatorStatus\Controller\OperatorStatusController::indexAction
_controller: Everyx\Mibew\Plugin\OperatorStatus\Controller\OperatorStatusController::isOperatorOnlineAction