Permalink
Browse files

Retorno sempre em Json; Suporte a RPC

  • Loading branch information...
1 parent 3d79ab6 commit ae42b728eb614e20b7b9cc0fc7c17511d27f2674 @eminetto eminetto committed Mar 12, 2012
Showing with 76 additions and 45 deletions.
  1. +47 −45 index.php
  2. +15 −0 procedure/Login.php
  3. +14 −0 procedure/Procedure.php
View
@@ -7,6 +7,7 @@
use Silex\Provider\ValidatorServiceProvider;
use model\User;
+use procedure as procedure;
require_once 'vendor/silex.phar';
@@ -19,8 +20,9 @@
'Doctrine\\DBAL\\Migrations' => __DIR__.'/vendor/doctrine/dbal/lib',
'Doctrine\\DBAL' => __DIR__.'/vendor/doctrine/dbal/lib',
'Doctrine' => __DIR__.'/vendor/doctrine/orm/lib',
- 'DMS' => __DIR__.'/vendor',
- 'model' => __DIR__
+ 'DMS' => __DIR__.'/vendor',
+ 'model' => __DIR__,
+ 'procedure' => __DIR__
));
$loader->register();
@@ -30,7 +32,6 @@
require_once __DIR__.'/library/doctrine.php';
require_once __DIR__.'/library/filter.php';
-
//app configuration
$app->register(new DoctrineServiceProvider(), array(
'db.options' => array(
@@ -48,35 +49,34 @@
'validator.class_path' => __DIR__.'/vendor',
));
-$app->get('/{entity}.{format}', function ($entity, $format) use ($app) {
+//Rest functions
+$app->get('/{entity}', function ($entity) use ($app)
+{
$query = "SELECT * FROM $entity";
$data = $app['db']->fetchAll($query);
- //@todo ver se está certo usar o codigo 404 quando não encontrado
if(count($data) == 0) {
$app->abort(404, 'Data not found');
}
return new Response(json_encode($data));
-})
-->assert('format', 'xml|json');
+});
-$app->get('/{entity}/{id}.{format}', function ($entity, $id, $format) use ($app, $em) {
+$app->get('/{entity}/{id}', function ($entity, $id) use ($em)
+{
$data = $em->find('model\\'.ucfirst($entity), $id);
- //@todo ver se está certo usar o codigo 404 quando não encontrado
if($data === null) {
$app->abort(404, 'Data not found');
}
return new Response($data->toJson());
-})
-->assert('format', 'xml|json');
-
+});
-$app->post('/{entity}', function ($entity, Request $request) use ($app, $em, $filter) {
+$app->post('/{entity}', function ($entity, Request $request) use ($app, $em, $filter)
+{
// Get POST data or 400 HTTP response
if (!$data = $request->get($entity)) {
- return new Response('Missing parameters.', 400);
+ return new Response('Missing parameters.', 400, array('Content-Type' => 'text/json'));
}
// Persist data to the database
@@ -86,7 +86,7 @@
//valid entity
if (count($app['validator']->validate($entity)) > 0) {
- $app->abort(400, 'Invalid parameters.');
+ return new Response('Invalid parameters.', 400, array('Content-Type' => 'text/json'));
}
//Filter entity
@@ -99,20 +99,20 @@
return new Response($entity->toJson());
});
-$app->put('/{entity}/{id}', function ($entity, $id, Request $request) use ($app, $em, $filter) {
-
+$app->put('/{entity}/{id}', function ($entity, $id, Request $request) use ($app, $em, $filter)
+{
if (!$data = $request->get($entity)) {
- return new Response('Missing parameters.', 400);
+ return new Response('Missing parameters.', 400, array('Content-Type' => 'text/json'));
}
if (!$entity = $em->find('model\\'.ucfirst($entity), $id)) {
- return new Response('Not found.', 404);
+ return new Response('Not found.', 404, array('Content-Type' => 'text/json'));
}
$entity->set($data);
if (count($app['validator']->validate($entity)) > 0) {
- $app->abort(400, 'Invalid parameters.');
+ return new Response('Invalid parameters.', 400, array('Content-Type' => 'text/json'));
}
//Filter entity
@@ -124,30 +124,42 @@
return new Response($entity->toJson(), 200);
});
-
-$app->delete('/{entity}/{id}', function ($entity, $id) use ($app, $em) {
+$app->delete('/{entity}/{id}', function ($entity, $id) use ($app, $em)
+{
if (!$entity = $em->find('model\\'.ucfirst($entity), $id)) {
- return new Response('Data not found.', 404);
+ return new Response('Data not found.', 404, array('Content-Type' => 'text/json'));
}
$em->remove($entity);
$em->flush();
return new Response('Data deleted.', 200);
});
+//rpc
+$app->post('/rpc/{procedure}', function ($procedure, Request $request) use ($app)
+{
+ $data = json_decode($request->getContent());
+
+ if (!isset($data->parameters)) {
+ return new Response('Missing parameters.', 400, array('Content-Type' => 'text/json'));
+ }
+
+ $procedure = "procedure\\". ucfirst($procedure);
+ if (!class_exists($procedure)) {
+ return new Response('Invalid procedure.', 400, array('Content-Type' => 'text/json'));
+ }
+ $class = new $procedure;
+ $result = $class->execute($data->parameters);
-$app->error(function (\Exception $e, $code) {
- echo $e->getMessage(), "\n";
- switch ($code) {
- case 400:
- $message = 'Bad request.';
+ switch ($result['status']) {
+ case 'success':
+ return new Response(json_encode($result['data']), 200);
break;
- case 404:
- $message = 'Page not found.';
+ case 'error':
+ return new Response('Error executing procedure', 400, array('Content-Type' => 'text/json'));
break;
- default:
- $message = 'Internal Server Error.';
- return new Response($message, $code);
-} });
+ }
+
+});
$app->before(function (Request $request) use ($app) {
if( ! $request->headers->has('authorization')){
@@ -161,17 +173,7 @@
});
$app->after(function (Request $request, Response $response) {
- // Get URI parameter to determine requested output format
- $format = $request->attributes->get('format');
- switch ($format) {
- case 'xml':
- $response->headers->set('Content-Type', 'text/xml');
- break;
- case 'json':
- $response->headers->set('Content-Type', 'text/json');
- break;
- break;
- }
+ $response->headers->set('Content-Type', 'text/json');
});
$app->run();
View
@@ -0,0 +1,15 @@
+<?php
+
+namespace procedure;
+
+class Login extends Procedure
+{
+ public function execute($parameters = array()) {
+ $result = array(
+ 'status' => 'success', //or error
+ 'data' => 'Valid user'
+ );
+
+ return $result;
+ }
+}
View
@@ -0,0 +1,14 @@
+<?php
+
+namespace procedure;
+
+abstract class Procedure
+{
+ /**
+ * Execute the procedure
+ *
+ * @param array $param
+ * @return array
+ */
+ abstract public function execute($parameters = array());
+}

0 comments on commit ae42b72

Please sign in to comment.