Files
Phraseanet/lib/Alchemy/Phrasea/Application/Api.php
Nicolas Le Goff 9f38823f27 Merge branch '3.8'
Conflicts:
	composer.lock
	lib/Alchemy/Phrasea/Application.php
	lib/Alchemy/Phrasea/Command/Setup/H264MappingGenerator.php
	lib/Alchemy/Phrasea/Controller/AbstractDelivery.php
	lib/Alchemy/Phrasea/Controller/Prod/DoDownload.php
	lib/Alchemy/Phrasea/Controller/Prod/Edit.php
	lib/Alchemy/Phrasea/Controller/Prod/Story.php
	lib/Alchemy/Phrasea/Controller/Prod/Upload.php
	lib/Alchemy/Phrasea/Controller/Report/Activity.php
	lib/Alchemy/Phrasea/Controller/Report/Root.php
	lib/Alchemy/Phrasea/Controller/Root/Account.php
	lib/Alchemy/Phrasea/Core/PhraseaEvents.php
	lib/Alchemy/Phrasea/Core/Version.php
	lib/classes/API/V1/adapter.php
	lib/classes/User/Adapter.php
	lib/classes/databox.php
	lib/classes/media/subdef.php
	lib/classes/module/report.php
	lib/classes/module/report/activity.php
	lib/classes/module/report/connexion.php
	lib/classes/module/report/download.php
	lib/classes/module/report/nav.php
	lib/classes/module/report/question.php
	lib/classes/module/report/sqlaction.php
	lib/classes/module/report/sqlconnexion.php
	lib/classes/module/report/sqldownload.php
	lib/classes/module/report/sqlfilter.php
	lib/classes/task/abstract.php
	locale/de_DE/LC_MESSAGES/phraseanet.mo
	locale/de_DE/LC_MESSAGES/phraseanet.po
	locale/en_GB/LC_MESSAGES/phraseanet.mo
	locale/en_GB/LC_MESSAGES/phraseanet.po
	locale/fr_FR/LC_MESSAGES/phraseanet.mo
	locale/fr_FR/LC_MESSAGES/phraseanet.po
	locale/nl_NL/LC_MESSAGES/phraseanet.mo
	locale/nl_NL/LC_MESSAGES/phraseanet.po
	locale/phraseanet.pot
	templates/web/prod/index.html.twig
	tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php
	tests/classes/api/v1/api_v1_adapterTest.php
	tests/classes/report/activityTest.php
	tests/classes/report/editTest.php
2014-12-09 13:59:29 +01:00

126 lines
5.4 KiB
PHP

<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Application as PhraseaApplication;
use Alchemy\Phrasea\Controller\Datafiles;
use Alchemy\Phrasea\Core\Event\Subscriber\ApiCorsSubscriber;
use Alchemy\Phrasea\Core\PhraseaEvents;
use Alchemy\Phrasea\Controller\Api\Oauth2;
use Alchemy\Phrasea\Controller\Api\Result;
use Alchemy\Phrasea\Controller\Api\V1;
use Alchemy\Phrasea\Core\Event\ApiResultEvent;
use Alchemy\Phrasea\Core\Event\Subscriber\ApiOauth2ErrorsSubscriber;
use Alchemy\Phrasea\Core\Event\Subscriber\ApiExceptionHandlerSubscriber;
use Monolog\Logger;
use Monolog\Processor\WebProcessor;
use Silex\Application as SilexApplication;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) {
$app = new PhraseaApplication($environment);
$app->loadPlugins();
$app['exception_handler'] = $app->share(function ($app) {
return new ApiExceptionHandlerSubscriber($app);
});
$app['monolog'] = $app->share($app->extend('monolog', function (Logger $monolog) {
$monolog->pushProcessor(new WebProcessor());
return $monolog;
}));
// handle API content negotiation
$app->before(function(Request $request) use ($app) {
// register custom API format
$request->setFormat(Result::FORMAT_JSON_EXTENDED, V1::$extendedContentTypes['json']);
$request->setFormat(Result::FORMAT_YAML_EXTENDED, V1::$extendedContentTypes['yaml']);
$request->setFormat(Result::FORMAT_JSONP_EXTENDED, V1::$extendedContentTypes['jsonp']);
$request->setFormat(Result::FORMAT_JSONP, array('text/javascript', 'application/javascript'));
// handle content negociation
$priorities = array('application/json', 'application/yaml', 'text/yaml', 'text/javascript', 'application/javascript');
foreach (V1::$extendedContentTypes['json'] as $priorities[]);
foreach (V1::$extendedContentTypes['yaml'] as $priorities[]);
$format = $app['format.negociator']->getBest($request->headers->get('accept') ,$priorities);
// throw unacceptable http error if API can not handle asked format
if (null === $format) {
$app->abort(406);
}
// set request format according to negotiated content or override format with JSONP if callback parameter is defined
if (trim($request->get('callback')) !== '') {
$request->setRequestFormat(Result::FORMAT_JSONP);
} else {
$request->setRequestFormat($request->getFormat($format->getValue()));
}
// tells whether asked format is extended or not
$request->attributes->set('_extended', in_array(
$request->getRequestFormat(Result::FORMAT_JSON),
array(
Result::FORMAT_JSON_EXTENDED,
Result::FORMAT_YAML_EXTENDED,
Result::FORMAT_JSONP_EXTENDED
)
));
}, PhraseaApplication::EARLY_EVENT);
$app->after(function(Request $request, Response $response) use ($app) {
if ($request->getRequestFormat(Result::FORMAT_JSON) === Result::FORMAT_JSONP && !$response->isOk() && !$response->isServerError()) {
$response->setStatusCode(200);
}
// set response content type
if (!$response->headers->get('Content-Type')) {
$response->headers->set('Content-Type', $request->getMimeType($request->getRequestFormat(Result::FORMAT_JSON)));
}
});
$app->get('/api/', function (Request $request, SilexApplication $app) {
return Result::create($request, [
'name' => $app['conf']->get(['registry', 'general', 'title']),
'type' => 'phraseanet',
'description' => $app['conf']->get(['registry', 'general', 'description']),
'documentation' => 'https://docs.phraseanet.com/Devel',
'versions' => [
'1' => [
'number' => V1::VERSION,
'uri' => '/api/v1/',
'authenticationProtocol' => 'OAuth2',
'authenticationVersion' => 'draft#v9',
'authenticationEndPoints' => [
'authorization_token' => '/api/oauthv2/authorize',
'access_token' => '/api/oauthv2/token'
]
]
]
])->createResponse();
});
$app->mount('/api/oauthv2', new Oauth2());
$app->mount('/datafiles/', new Datafiles());
$app->mount('/api/v1', new V1());
$app['dispatcher'] = $app->share($app->extend('dispatcher', function ($dispatcher, PhraseaApplication $app) {
$dispatcher->addSubscriber(new ApiOauth2ErrorsSubscriber($app['phraseanet.exception_handler'], $app['translator']));
return $dispatcher;
}));
$app->after(function (Request $request, Response $response) use ($app) {
$app['dispatcher']->dispatch(PhraseaEvents::API_RESULT, new ApiResultEvent($request, $response));
});
$app['dispatcher']->addSubscriber(new ApiCorsSubscriber($app));
return $app;
}, isset($environment) ? $environment : PhraseaApplication::ENV_PROD);