|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Islandora\Crayfish\Commons\Provider; |
| 4 | + |
| 5 | +use Islandora\Crayfish\Commons\CmdExecuteService; |
| 6 | +use Islandora\Crayfish\Commons\FedoraResourceConverter; |
| 7 | +use Pimple\Container; |
| 8 | +use Pimple\ServiceProviderInterface; |
| 9 | +use Silex\Provider\DoctrineServiceProvider; |
| 10 | +use Silex\Provider\MonologServiceProvider; |
| 11 | +use Silex\Provider\ServiceControllerServiceProvider; |
| 12 | +use Silex\Provider\SecurityServiceProvider; |
| 13 | +use Islandora\Chullo\FedoraApi; |
| 14 | +use Islandora\Crayfish\Commons\Syn\SettingsParser; |
| 15 | +use Islandora\Crayfish\Commons\Syn\JwtAuthenticator; |
| 16 | +use Islandora\Crayfish\Commons\Syn\JwtFactory; |
| 17 | + |
| 18 | +class IslandoraServiceProvider implements ServiceProviderInterface |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @inheritDoc |
| 22 | + */ |
| 23 | + public function register(Container $container) |
| 24 | + { |
| 25 | + // Register services we rely on |
| 26 | + $container->register(new MonologServiceProvider()); |
| 27 | + $container->register(new ServiceControllerServiceProvider()); |
| 28 | + $container->register(new SecurityServiceProvider()); |
| 29 | + $container->register(new DoctrineServiceProvider()); |
| 30 | + |
| 31 | + // Configure external services |
| 32 | + $container['monolog.logfile'] = function ($container) { |
| 33 | + return strtolower($container['crayfish.log.level']) == 'none' ? null : $container['crayfish.log.file']; |
| 34 | + }; |
| 35 | + $container['monolog.level'] = function ($container) { |
| 36 | + return $container['crayfish.log.level']; |
| 37 | + }; |
| 38 | + |
| 39 | + $container['security.firewalls'] = function ($container) { |
| 40 | + if ($container['crayfish.syn.enable']) { |
| 41 | + return [ |
| 42 | + 'default' => [ |
| 43 | + 'stateless' => true, |
| 44 | + 'anonymous' => false, |
| 45 | + 'guard' => [ |
| 46 | + 'authenticators' => [ |
| 47 | + 'crayfish.syn.jwt_authentication' |
| 48 | + ], |
| 49 | + ], |
| 50 | + ], |
| 51 | + ]; |
| 52 | + } else { |
| 53 | + return []; |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + $this->registerDbOptions($container); |
| 58 | + |
| 59 | + // Register our services |
| 60 | + $container['crayfish.cmd_execute_service'] = function ($container) { |
| 61 | + return new CmdExecuteService( |
| 62 | + $container['monolog']->withName('crayfish.cmd_execute_service') |
| 63 | + ); |
| 64 | + }; |
| 65 | + |
| 66 | + $container['crayfish.fedora_resource'] = function ($container) { |
| 67 | + return new FedoraResourceConverter( |
| 68 | + FedoraApi::create($container['crayfish.fedora_resource.base_url']) |
| 69 | + ); |
| 70 | + }; |
| 71 | + |
| 72 | + $container['crayfish.syn.settings_parser'] = function ($container) { |
| 73 | + if (file_exists($container['crayfish.syn.config'])) { |
| 74 | + $xml = file_get_contents($container['crayfish.syn.config']); |
| 75 | + } else { |
| 76 | + $xml = ''; |
| 77 | + $container['monolog'] |
| 78 | + ->error("Securty configuration not found. ${container['crayfish.syn.config']}"); |
| 79 | + } |
| 80 | + |
| 81 | + return new SettingsParser( |
| 82 | + $xml, |
| 83 | + $container['monolog']->withName('crayfish.syn.settings_parser') |
| 84 | + ); |
| 85 | + }; |
| 86 | + |
| 87 | + $container['crayfish.syn.jwt_authentication'] = function ($app) { |
| 88 | + return new JwtAuthenticator( |
| 89 | + $app['crayfish.syn.settings_parser'], |
| 90 | + new JwtFactory(), |
| 91 | + $app['monolog']->withName('crayfish.syn.jwt_authentication') |
| 92 | + ); |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + protected function registerDbOptions($container) |
| 97 | + { |
| 98 | + $container['db.options'] = function ($container) { |
| 99 | + $setoption = function (&$settings, $container, $name) { |
| 100 | + if (isset($container["crayfish.db.options.$name"])) { |
| 101 | + $settings[$name] = $container["crayfish.db.options.$name"]; |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + $settings = []; |
| 106 | + $setoption($settings, $container, 'host'); |
| 107 | + $setoption($settings, $container, 'port'); |
| 108 | + $setoption($settings, $container, 'dbname'); |
| 109 | + $setoption($settings, $container, 'user'); |
| 110 | + $setoption($settings, $container, 'password'); |
| 111 | + $setoption($settings, $container, 'charset'); |
| 112 | + $setoption($settings, $container, 'path'); |
| 113 | + $setoption($settings, $container, 'url'); |
| 114 | + |
| 115 | + return $settings; |
| 116 | + }; |
| 117 | + } |
| 118 | +} |
0 commit comments