Extract HTTP meta service provider

This commit is contained in:
Thibaud Fabre
2016-01-26 15:31:09 +01:00
parent 62f0388d7b
commit fa19e779cb
4 changed files with 134 additions and 107 deletions

View File

@@ -11,8 +11,6 @@
namespace Alchemy\Phrasea; namespace Alchemy\Phrasea;
use Alchemy\Cors\Options\DefaultProvider;
use Alchemy\CorsProvider\CorsServiceProvider;
use Alchemy\Geonames\GeonamesServiceProvider; use Alchemy\Geonames\GeonamesServiceProvider;
use Alchemy\Phrasea\Application\Environment; use Alchemy\Phrasea\Application\Environment;
use Alchemy\Phrasea\Application\Helper\AclAware; use Alchemy\Phrasea\Application\Helper\AclAware;
@@ -20,7 +18,6 @@ use Alchemy\Phrasea\Application\Helper\ApplicationBoxAware;
use Alchemy\Phrasea\Application\Helper\AuthenticatorAware; use Alchemy\Phrasea\Application\Helper\AuthenticatorAware;
use Alchemy\Phrasea\Application\RouteLoader; use Alchemy\Phrasea\Application\RouteLoader;
use Alchemy\Phrasea\Authorization\AuthorizationServiceProvider; use Alchemy\Phrasea\Authorization\AuthorizationServiceProvider;
use Alchemy\Phrasea\ControllerProvider\ControllerProviderServiceProvider;
use Alchemy\Phrasea\Core\Event\Subscriber\BasketSubscriber; use Alchemy\Phrasea\Core\Event\Subscriber\BasketSubscriber;
use Alchemy\Phrasea\Core\Event\Subscriber\BridgeSubscriber; use Alchemy\Phrasea\Core\Event\Subscriber\BridgeSubscriber;
use Alchemy\Phrasea\Core\Event\Subscriber\ExportSubscriber; use Alchemy\Phrasea\Core\Event\Subscriber\ExportSubscriber;
@@ -31,7 +28,8 @@ use Alchemy\Phrasea\Core\Event\Subscriber\PhraseaInstallSubscriber;
use Alchemy\Phrasea\Core\Event\Subscriber\RegistrationSubscriber; use Alchemy\Phrasea\Core\Event\Subscriber\RegistrationSubscriber;
use Alchemy\Phrasea\Core\Event\Subscriber\ValidationSubscriber; use Alchemy\Phrasea\Core\Event\Subscriber\ValidationSubscriber;
use Alchemy\Phrasea\Core\MetaProvider\DatabaseMetaProvider; use Alchemy\Phrasea\Core\MetaProvider\DatabaseMetaProvider;
use Alchemy\Phrasea\Core\MetaProvider\MediaUtilitiesServiceProvider; use Alchemy\Phrasea\Core\MetaProvider\HttpStackMetaProvider;
use Alchemy\Phrasea\Core\MetaProvider\MediaUtilitiesMetaServiceProvider;
use Alchemy\Phrasea\Core\MetaProvider\TemplateEngineMetaProvider; use Alchemy\Phrasea\Core\MetaProvider\TemplateEngineMetaProvider;
use Alchemy\Phrasea\Core\MetaProvider\TranslationMetaProvider; use Alchemy\Phrasea\Core\MetaProvider\TranslationMetaProvider;
use Alchemy\Phrasea\Core\Middleware\ApiApplicationMiddlewareProvider; use Alchemy\Phrasea\Core\Middleware\ApiApplicationMiddlewareProvider;
@@ -48,7 +46,6 @@ use Alchemy\Phrasea\Core\Provider\CacheConnectionServiceProvider;
use Alchemy\Phrasea\Core\Provider\CacheServiceProvider; use Alchemy\Phrasea\Core\Provider\CacheServiceProvider;
use Alchemy\Phrasea\Core\Provider\ConfigurationServiceProvider; use Alchemy\Phrasea\Core\Provider\ConfigurationServiceProvider;
use Alchemy\Phrasea\Core\Provider\ConfigurationTesterServiceProvider; use Alchemy\Phrasea\Core\Provider\ConfigurationTesterServiceProvider;
use Alchemy\Phrasea\Core\Provider\ContentNegotiationServiceProvider;
use Alchemy\Phrasea\Core\Provider\ConvertersServiceProvider; use Alchemy\Phrasea\Core\Provider\ConvertersServiceProvider;
use Alchemy\Phrasea\Core\Provider\CSVServiceProvider; use Alchemy\Phrasea\Core\Provider\CSVServiceProvider;
use Alchemy\Phrasea\Core\Provider\FeedServiceProvider; use Alchemy\Phrasea\Core\Provider\FeedServiceProvider;
@@ -68,7 +65,6 @@ use Alchemy\Phrasea\Core\Provider\RegistrationServiceProvider;
use Alchemy\Phrasea\Core\Provider\RepositoriesServiceProvider; use Alchemy\Phrasea\Core\Provider\RepositoriesServiceProvider;
use Alchemy\Phrasea\Core\Provider\SearchEngineServiceProvider; use Alchemy\Phrasea\Core\Provider\SearchEngineServiceProvider;
use Alchemy\Phrasea\Core\Provider\SerializerServiceProvider; use Alchemy\Phrasea\Core\Provider\SerializerServiceProvider;
use Alchemy\Phrasea\Core\Provider\SessionHandlerServiceProvider;
use Alchemy\Phrasea\Core\Provider\StatusServiceProvider; use Alchemy\Phrasea\Core\Provider\StatusServiceProvider;
use Alchemy\Phrasea\Core\Provider\SubdefServiceProvider; use Alchemy\Phrasea\Core\Provider\SubdefServiceProvider;
use Alchemy\Phrasea\Core\Provider\TasksServiceProvider; use Alchemy\Phrasea\Core\Provider\TasksServiceProvider;
@@ -94,12 +90,9 @@ use Silex\Application as SilexApplication;
use Silex\Application\TranslationTrait; use Silex\Application\TranslationTrait;
use Silex\Application\UrlGeneratorTrait; use Silex\Application\UrlGeneratorTrait;
use Silex\Provider\FormServiceProvider; use Silex\Provider\FormServiceProvider;
use Silex\Provider\HttpFragmentServiceProvider;
use Silex\Provider\MonologServiceProvider; use Silex\Provider\MonologServiceProvider;
use Silex\Provider\ServiceControllerServiceProvider; use Silex\Provider\ServiceControllerServiceProvider;
use Silex\Provider\SessionServiceProvider;
use Silex\Provider\SwiftmailerServiceProvider; use Silex\Provider\SwiftmailerServiceProvider;
use Silex\Provider\UrlGeneratorServiceProvider;
use Silex\Provider\ValidatorServiceProvider; use Silex\Provider\ValidatorServiceProvider;
use Silex\Provider\WebProfilerServiceProvider; use Silex\Provider\WebProfilerServiceProvider;
use Sorien\Provider\PimpleDumpProvider; use Sorien\Provider\PimpleDumpProvider;
@@ -110,9 +103,6 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormTypeInterface; use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Routing\RequestContext;
use Unoconv\UnoconvServiceProvider; use Unoconv\UnoconvServiceProvider;
use XPDF\PdfToText; use XPDF\PdfToText;
use XPDF\XPDFServiceProvider; use XPDF\XPDFServiceProvider;
@@ -191,7 +181,6 @@ class Application extends SilexApplication
$this->register(new CSVServiceProvider()); $this->register(new CSVServiceProvider());
$this->register(new RegistrationServiceProvider()); $this->register(new RegistrationServiceProvider());
$this->setUpImagine();
$this->register(new JMSSerializerServiceProvider()); $this->register(new JMSSerializerServiceProvider());
$this->register(new FeedServiceProvider()); $this->register(new FeedServiceProvider());
$this->register(new FtpServiceProvider()); $this->register(new FtpServiceProvider());
@@ -215,12 +204,7 @@ class Application extends SilexApplication
$this->register(new BorderManagerServiceProvider()); $this->register(new BorderManagerServiceProvider());
} }
$this->register(new SessionHandlerServiceProvider());
$this->register(new SessionServiceProvider(), [
'session.test' => $this->getEnvironment() === static::ENV_TEST,
'session.storage.options' => ['cookie_lifetime' => 0]
]);
$this->setupSession();
$this->register(new SerializerServiceProvider()); $this->register(new SerializerServiceProvider());
$this->register(new ServiceControllerServiceProvider()); $this->register(new ServiceControllerServiceProvider());
$this->register(new SwiftmailerServiceProvider()); $this->register(new SwiftmailerServiceProvider());
@@ -228,11 +212,8 @@ class Application extends SilexApplication
$this->register(new TasksServiceProvider()); $this->register(new TasksServiceProvider());
$this->register(new TokensServiceProvider()); $this->register(new TokensServiceProvider());
$this->register(new HttpFragmentServiceProvider()); $this->register(new HttpStackMetaProvider());
$this->register(new UrlGeneratorServiceProvider()); $this->register(new MediaUtilitiesMetaServiceProvider());
$this->setupRequestContext();
$this->register(new MediaUtilitiesServiceProvider());
$this->register(new TemplateEngineMetaProvider()); $this->register(new TemplateEngineMetaProvider());
$this->register(new TranslationMetaProvider()); $this->register(new TranslationMetaProvider());
@@ -248,31 +229,7 @@ class Application extends SilexApplication
$this->register(new ManipulatorServiceProvider()); $this->register(new ManipulatorServiceProvider());
$this->register(new PluginServiceProvider()); $this->register(new PluginServiceProvider());
$this->register(new PhraseaEventServiceProvider()); $this->register(new PhraseaEventServiceProvider());
$this->register(new ContentNegotiationServiceProvider());
$this->register(new CorsServiceProvider(), [
'alchemy_cors.debug' => $this['debug'],
'alchemy_cors.cache_path' => function (Application $app) {
return rtrim($app['cache.path'], '/\\') . '/alchemy_cors.cache.php';
},
]);
$this['phraseanet.api_cors.options_provider'] = function (Application $app) {
$paths = [];
if (isset($app['phraseanet.configuration']['api_cors'])) {
$config = $app['phraseanet.configuration']['api_cors'];
if (isset($config['enabled']) && $config['enabled']) {
unset($config['enabled']);
$paths['/api/v\d+/'] = $config;
}
}
return new DefaultProvider($paths, []);
};
$this['alchemy_cors.options_providers'][] = 'phraseanet.api_cors.options_provider';
$this->register(new LocaleServiceProvider()); $this->register(new LocaleServiceProvider());
$this->setupEventDispatcher(); $this->setupEventDispatcher();
$this['phraseanet.exception_handler'] = $this->share(function ($app) { $this['phraseanet.exception_handler'] = $this->share(function ($app) {
@@ -285,8 +242,6 @@ class Application extends SilexApplication
return $handler; return $handler;
}); });
$this->register(new ControllerProviderServiceProvider());
$resolvers = $this['alchemy_embed.resource_resolvers']; $resolvers = $this['alchemy_embed.resource_resolvers'];
$resolvers['datafile'] = $resolvers->share(function () { $resolvers['datafile'] = $resolvers->share(function () {
return new DatafilesResolver($this->getApplicationBox()); return new DatafilesResolver($this->getApplicationBox());
@@ -672,43 +627,6 @@ class Application extends SilexApplication
})); }));
} }
private function setUpImagine()
{
$this['imagine.factory'] = $this->share(function (Application $app) {
if ($app['conf']->get(['registry', 'executables', 'imagine-driver']) != '') {
return $app['conf']->get(['registry', 'executables', 'imagine-driver']);
}
if (class_exists('\Gmagick')) {
return 'gmagick';
}
if (class_exists('\Imagick')) {
return 'imagick';
}
if (extension_loaded('gd')) {
return 'gd';
}
throw new \RuntimeException('No Imagine driver available');
});
}
private function setupSession()
{
$this['session.storage.test'] = $this->share(function (Application $app) {
return new MockArraySessionStorage();
});
$this['session.storage.handler'] = $this->share(function (Application $app) {
if (!$this['phraseanet.configuration-tester']->isInstalled()) {
return new NullSessionHandler();
}
return $this['session.storage.handler.factory']->create($app['conf']);
});
}
private function setupRecaptacha() private function setupRecaptacha()
{ {
$this['recaptcha.public-key'] = $this->share(function (Application $app) { $this['recaptcha.public-key'] = $this->share(function (Application $app) {
@@ -741,24 +659,6 @@ class Application extends SilexApplication
} }
} }
private function setupRequestContext()
{
$this['request_context'] = $this->share($this->extend('request_context', function (RequestContext $context, Application $app) {
if ($app['configuration.store']->isSetup()) {
$data = parse_url($app['conf']->get('servername'));
if (isset($data['scheme'])) {
$context->setScheme($data['scheme']);
}
if (isset($data['host'])) {
$context->setHost($data['host']);
}
}
return $context;
}));
}
private function setupSwiftMailer() private function setupSwiftMailer()
{ {
$this['swiftmailer.transport'] = $this->share(function (Application $app) { $this['swiftmailer.transport'] = $this->share(function (Application $app) {

View File

@@ -0,0 +1,107 @@
<?php
namespace Alchemy\Phrasea\Core\MetaProvider;
use Alchemy\Cors\Options\DefaultProvider;
use Alchemy\CorsProvider\CorsServiceProvider;
use Alchemy\Phrasea\Application as PhraseaApplication;
use Alchemy\Phrasea\ControllerProvider\ControllerProviderServiceProvider;
use Alchemy\Phrasea\Core\Provider\ContentNegotiationServiceProvider;
use Alchemy\Phrasea\Core\Provider\SessionHandlerServiceProvider;
use Silex\Application;
use Silex\Provider\HttpFragmentServiceProvider;
use Silex\Provider\SessionServiceProvider;
use Silex\Provider\UrlGeneratorServiceProvider;
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Routing\RequestContext;
class HttpStackMetaProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
if (! $app instanceof PhraseaApplication) {
throw new \LogicException('Expected an instance Alchemy\Phrasea\Application');
}
$app->register(new HttpFragmentServiceProvider());
$app->register(new UrlGeneratorServiceProvider());
$this->setupRequestContext($app);
$app->register(new SessionHandlerServiceProvider());
$app->register(new SessionServiceProvider(), [
'session.test' => $app->getEnvironment() === PhraseaApplication::ENV_TEST,
'session.storage.options' => ['cookie_lifetime' => 0]
]);
$app['session.storage.test'] = $app->share(function (Application $app) {
return new MockArraySessionStorage();
});
$app['session.storage.handler'] = $app->share(function (Application $app) {
if (!$app['phraseanet.configuration-tester']->isInstalled()) {
return new NullSessionHandler();
}
return $app['session.storage.handler.factory']->create($app['conf']);
});
$app->register(new ControllerProviderServiceProvider());
$this->registerCors($app);
}
public function setupRequestContext(Application $app)
{
$app['request_context'] = $app->share($app->extend('request_context', function (RequestContext $context, Application $app) {
if ($app['configuration.store']->isSetup()) {
$data = parse_url($app['conf']->get('servername'));
if (isset($data['scheme'])) {
$context->setScheme($data['scheme']);
}
if (isset($data['host'])) {
$context->setHost($data['host']);
}
}
return $context;
}));
}
public function registerCors(Application $app)
{
$app->register(new ContentNegotiationServiceProvider());
$app->register(new CorsServiceProvider(), [
'alchemy_cors.debug' => $app['debug'],
'alchemy_cors.cache_path' => function (Application $app) {
return rtrim($app['cache.path'], '/\\') . '/alchemy_cors.cache.php';
},
]);
$app['phraseanet.api_cors.options_provider'] = function (Application $app) {
$paths = [];
if (isset($app['phraseanet.configuration']['api_cors'])) {
$config = $app['phraseanet.configuration']['api_cors'];
if (isset($config['enabled']) && $config['enabled']) {
unset($config['enabled']);
$paths['/api/v\d+/'] = $config;
}
}
return new DefaultProvider($paths, []);
};
$app['alchemy_cors.options_providers'][] = 'phraseanet.api_cors.options_provider';
}
public function boot(Application $app)
{
// no-op
}
}

View File

@@ -12,7 +12,7 @@ use PHPExiftool\PHPExiftoolServiceProvider;
use Silex\Application; use Silex\Application;
use Silex\ServiceProviderInterface; use Silex\ServiceProviderInterface;
class MediaUtilitiesServiceProvider implements ServiceProviderInterface class MediaUtilitiesMetaServiceProvider implements ServiceProviderInterface
{ {
public function register(Application $app) public function register(Application $app)
@@ -24,6 +24,26 @@ class MediaUtilitiesServiceProvider implements ServiceProviderInterface
$app->register(new MediaVorusServiceProvider()); $app->register(new MediaVorusServiceProvider());
$app->register(new MP4BoxServiceProvider()); $app->register(new MP4BoxServiceProvider());
$app->register(new PHPExiftoolServiceProvider()); $app->register(new PHPExiftoolServiceProvider());
$app['imagine.factory'] = $app->share(function (Application $app) {
if ($app['conf']->get(['registry', 'executables', 'imagine-driver']) != '') {
return $app['conf']->get(['registry', 'executables', 'imagine-driver']);
}
if (class_exists('\Gmagick')) {
return 'gmagick';
}
if (class_exists('\Imagick')) {
return 'imagick';
}
if (extension_loaded('gd')) {
return 'gd';
}
throw new \RuntimeException('No Imagine driver available');
});
} }
public function boot(Application $app) public function boot(Application $app)

View File

@@ -41,7 +41,7 @@ class MediaAlchemystServiceProvider implements ServiceProviderInterface
return $configuration; return $configuration;
}); });
$this['media-alchemyst.logger'] = $app->share(function (Application $app) { $app['media-alchemyst.logger'] = $app->share(function (Application $app) {
return $app['monolog']; return $app['monolog'];
}); });
} }