Skip to content

Commit

Permalink
Merge pull request #139 from whikloj/make_txService_use_providers
Browse files Browse the repository at this point in the history
Use ServiceProvider and ControllerProvider for the Transaction Service
  • Loading branch information
DiegoPino committed Feb 9, 2016
2 parents 162e1b9 + 3962509 commit edad6b2
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 31 deletions.
13 changes: 11 additions & 2 deletions services/TransactionService/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
"description": "RESTful service providing transactions in Fedora 4",
"repositories": [
{
"type": "vcs",
"url": "/Users/dpino/Desktop/Development/ISLANDORAWORK/CLAW_MICRO/chullo"
"type": "path",
"url": "../ResourceServiceProvider"
},
{
"type": "path",
"url": "../../../chullo"
}
],
"require": {
"islandora/chullo": "dev-master",
"islandora/resource-service": "dev-sprint-002",
"silex/silex": "^1.3"
},
"autoload": {
Expand All @@ -18,6 +23,10 @@
{
"name": "Daniel Lamb",
"email": "daniel@discoverygarden.ca"
},
{
"name": "Jared Whiklo",
"email": "jwhiklo@gmail.com"
}
]
}
11 changes: 11 additions & 0 deletions services/TransactionService/config/settings.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Islandora Dev Settings to be used with $app['debug'] == TRUE
islandora:
fedoraProtocol: http
fedoraHost: "localhost:8080"
fedoraPath: /fcrepo/rest
tripleProtocol: http
tripleHost: "localhost:8080"
triplePath: /bigdata/sparql
resourceIdRegex: "(?:[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})?"
# This domain is used as namespace (hashed) when generating UUID V5 identifiers
defaultNamespaceDomainUuuidV5: "www.islandora.ca"
10 changes: 10 additions & 0 deletions services/TransactionService/config/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
islandora:
fedoraProtocol: http
fedoraHost: "localhost:8080"
fedoraPath: /fcrepo/rest
tripleProtocol: http
tripleHost: "localhost:8080"
triplePath: /bigdata/sparql
resourceIdRegex: "(?:[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})?"
# This domain is used as namespace (hashed) when generating UUID V5 identifiers, replace with your own
defaultNamespaceDomainUuuidV5: "www.islandora.ca"
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Islandora\TransactionService\Controller;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class TransactionController {

public function create(Application $app, Request $request) {
try {
$response = $app['api']->createTransaction();
} catch (\Exception $e) {
$app->abort(503, 'Chullo says "Fedora4 Repository Not available"');
}
return $response;
}

public function extend(Application $app, Request $request, $id) {
try {
$response = $app['api']->extendTransaction($id);
} catch (\Exception $e) {
$app->abort(503, 'Chullo says "Fedora4 Repository Not available"');
}
return $response;
}

public function commit(Application $app, Request $request, $id) {
try {
$response = $app['api']->commitTransaction($id);
} catch (\Exception $e) {
$app->abort(503, 'Chullo says "Fedora4 Repository Not available"');
}
return $response;
}

public function rollback(Application $app, Request $request, $id) {
try {
$response = $app['api']->rollbackTransaction($id);
} catch (\Exception $e) {
$app->abort(503, 'Chullo says "Fedora4 Repository Not available"');
}
return $response;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Islandora\TransactionService\Provider;

use Silex\Application;
use Silex\ServiceProviderInterface;
use Silex\ControllerProviderInterface;
use Islandora\Chullo\FedoraApi;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Yaml\Yaml;
use Islandora\ResourceService\Controller\ResourceController;
use Islandora\TransactionService\Controller\TransactionController;

class TransactionServiceProvider implements ServiceProviderInterface, ControllerProviderInterface {

/**
* Part of ServiceProviderInterface
*/
public function register(Application $app) {
//
// Define controller services
//
$app['islandora.transactioncontroller'] = $app->share(function() use ($app) {
return new \Islandora\TransactionService\Controller\TransactionController($app);
});

if (!isset($app['islandora.resourcecontroller'])) {
$app['islandora.resourcecontroller'] = $app->share(function() use ($app) {
return new \Islandora\ResourceService\Controller\ResourceController($app);
});
}

if (!isset($app['api'])) {
$app['api'] = $app->share(function() use ($app) {
return FedoraApi::create($app['config']['islandora']['fedoraProtocol'].'://'.$app['config']['islandora']['fedoraHost'].$app['config']['islandora']['fedoraPath']);
});
}

// Common simple YAML config parser.
if (!isset($app['config'])) {
$app['config'] = $app->share(function() use ($app){
if ($app['debug']) {
$configFile = __DIR__.'/../../config/settings.dev.yml';
}
else {
$configFile = __DIR__.'/../../config/settings.yml';
}
$settings = Yaml::parse(file_get_contents($configFile));
return $settings;
});
}

}

public function boot(Application $app) {}

public function connect(Application $app) {
$controllers = $app['controllers_factory'];

$controllers->get("/transaction/{id}", "islandora.resourcecontroller:get")
->value('id',"")
->value('child', "")
->before(function(Request $request) {
if (isset($request->attributes->parameters) && $request->attributes->parameters->has('id')) {
// To get this to work we need to GET /islandora/resource//tx:id
// So we move the $id to the $child parameter.
$id = $request->attributes->parameters->get('id');
$request->attributes->parameters->set('child', $id);
$request->attributes->parameters->set('id', '');
}
})
->bind('islandora.transactionGet');

$controllers->post("/transaction", "islandora.transactioncontroller:create")
->bind('islandora.transactionCreate');

$controllers->post("/transaction/{id}/extend", "islandora.transactioncontroller:extend")
->value('id',"")
->bind('islandora.transactionExtend');

$controllers->post("/transaction/{id}/commit", "islandora.transactioncontroller:commit")
->value('id',"")
->bind('islandora.transactionCommit');

$controllers->post("/transaction/{id}/rollback", "islandora.transactioncontroller:rollback")
->value('id',"")
->bind('islandora.transactionRollback');

return $controllers;
}

}
64 changes: 35 additions & 29 deletions services/TransactionService/src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Islandora\Chullo\FedoraApi;
use Silex\Application;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Psr\Http\Message\ResponseInterface;

date_default_timezone_set('UTC');
Expand All @@ -16,8 +17,18 @@

$app['debug'] = true;

$app['fedora'] = $app->share(function () {
return FedoraApi::create('http://127.0.0.1:8080/fcrepo/rest');
$app->register(new \Silex\Provider\ServiceControllerServiceProvider());

$islandoraTransactionService = new \Islandora\TransactionService\Provider\TransactionServiceProvider;

$app->register($islandoraTransactionService);
$app->mount("/islandora", $islandoraTransactionService);

/**
* Convert returned Guzzle responses to Symfony responses.
*/
$app->view(function (ResponseInterface $psr7) {
return new Response($psr7->getBody(), $psr7->getStatusCode(), $psr7->getHeaders());
});

/**
Expand All @@ -27,32 +38,27 @@
return new Response($psr7->getBody(), $psr7->getStatusCode(), $psr7->getHeaders());
});

$app->post(
"/islandora/transaction",
function (Application $app) {
return $app['fedora']->createTransaction();
}
);

$app->post(
"/islandora/transaction/{id}/extend",
function (Application $app, $id) {
return $app['fedora']->extendTransaction($id);
}
);

$app->post(
"/islandora/transaction/{id}/commit",
function (Application $app, $id) {
return $app['fedora']->commitTransaction($id);
}
);

$app->post(
"/islandora/transaction/{id}/rollback",
function (Application $app, $id) {
return $app['fedora']->rollbackTransaction($id);
}
);
$app->after(function (Request $request, Response $response, Application $app) {
// Todo a closing controller, not sure what now but i had an idea.
});
$app->error(function (\Symfony\Component\HttpKernel\Exception\HttpException $e, $code) use ($app){
if ($app['debug']) {
return;
}
return new Response(sprintf('Islandora Transaction Service exception: %s / HTTP %d response', $e->getMessage(), $code), $code);
});
$app->error(function (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e, $code) use ($app){
if ($app['debug']) {
return;
}
//Not sure what the best "verbose" message is
return new Response(sprintf('Islandora Transaction Service exception: %s / HTTP %d response', $e->getMessage(), $code), $code);
});
$app->error(function (\Exception $e, $code) use ($app){
if ($app['debug']) {
return;
}
return new Response(sprintf('Islandora Transaction Service uncatched exception: %s %d response', $e->getMessage(), $code), $code);
});

$app->run();

0 comments on commit edad6b2

Please sign in to comment.