Skip to content

Commit

Permalink
Make Crayfish-Commons a Symfony bundle only. (#49)
Browse files Browse the repository at this point in the history
* Change to symfony/flex

* Coder

* Correct namespace for tests (#39)

Restrict symfony versions

Fix deprecation in test

* Finalize Crayfish-Commons in Symfony 4.4

* Don't fail if no Apix-Ldp-Resource header

* Remove check for master request
  • Loading branch information
whikloj authored May 5, 2022
1 parent 6088085 commit e7b8cbd
Show file tree
Hide file tree
Showing 29 changed files with 1,817 additions and 1,389 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
vendor
.phpunit*
clover.xml
47 changes: 36 additions & 11 deletions src/ApixMiddleware.php → ApixMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

use Islandora\Chullo\IFedoraApi;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Retrieves a Fedora resource using the Apix-Ldp-Resource header.
*
* @package Islandora\Crayfish\Commons
*/
class ApixMiddleware
class ApixMiddleware implements EventSubscriberInterface
{

/**
Expand All @@ -39,18 +42,27 @@ public function __construct(
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Psr\Http\Message\ResponseInterface
*
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
*/
public function before(Request $request)
public function before(RequestEvent $event)
{

$request = $event->getRequest();

// Short circuit if this is an OPTIONS or HEAD request.
if (in_array(
strtoupper($request->getMethod()),
['OPTIONS', 'HEAD']
)) {
return;
}

// Short circuit if there's no Apix-Ldp-Resource header.
if (!$request->headers->has("Apix-Ldp-Resource")) {
$this->log->debug("Malformed request, no Apix-Ldp-Resource header present");
return new Response(
"Malformed request, no Apix-Ldp-Resource header present",
400
);
$this->log->debug("No Apix-Ldp-Resource header present, no fedora_resource set");
$request->attributes->set('fedora_resource', false);
return;
}

// Get the resource.
Expand All @@ -64,10 +76,11 @@ public function before(Request $request)
'status' => $fedora_resource->getStatusCode(),
'headers' => $fedora_resource->getHeaders()
]);
return new Response(
$event->setResponse(new Response(
$fedora_resource->getReasonPhrase(),
$status
);
));
return;
}

// Set the Fedora resource on the request.
Expand All @@ -89,4 +102,16 @@ protected function getFedoraResource(Request $request)
$headers
);
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => [
['before', 0],
],
];
}
}
6 changes: 3 additions & 3 deletions src/CmdExecuteService.php → CmdExecuteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function execute($cmd, $data)
// On error, extract message from STDERR and throw an exception.
if ($exit_code != 0) {
$msg = stream_get_contents($pipes[2]);
$this->cleanup($pipes, $this->output, $process);
$this->cleanup($pipes, $process);
if ($this->log) {
$this->log->error('Process exited with non-zero code.', [
'exit_code' => $exit_code,
Expand All @@ -128,11 +128,11 @@ public function execute($cmd, $data)
ob_flush();
flush();
}
$this->cleanup($pipes, $this->output, $process);
$this->cleanup($pipes, $process);
};
}

protected function cleanup($pipes, $output, $process)
protected function cleanup($pipes, $process)
{
// Close STDERR
fclose($pipes[2]);
Expand Down
9 changes: 9 additions & 0 deletions CrayfishCommonsBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Islandora\Crayfish\Commons;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class CrayfishCommonsBundle extends Bundle
{
}
26 changes: 26 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Islandora\Crayfish\Commons\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{

/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('crayfish_commons');
$root = $treeBuilder->getRootNode();
$root->addDefaultsIfNotSet()
->children()
->scalarNode('fedora_base_uri')->cannotBeEmpty()->defaultValue('http://localhost:8080/fcrepo/rest')->end()
->scalarNode('syn_config')->defaultValue(__DIR__ . '/../Resources/default_syn.xml')->end()
->end();

return $treeBuilder;
}
}
64 changes: 64 additions & 0 deletions DependencyInjection/CrayfishCommonsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Islandora\Crayfish\Commons\DependencyInjection;

use Islandora\Chullo\IFedoraApi;
use Islandora\Crayfish\Commons\Client\GeminiClient;
use Islandora\Crayfish\Commons\CmdExecuteService;
use Islandora\Crayfish\Commons\EntityMapper\EntityMapper;
use Islandora\Crayfish\Commons\Syn\JwtAuthenticator;
use Islandora\Crayfish\Commons\Syn\JwtFactory;
use Islandora\Crayfish\Commons\Syn\JwtUserProvider;
use Islandora\Crayfish\Commons\Syn\SettingsParser;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class CrayfishCommonsExtension extends Extension
{

public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();

$config = $this->processConfiguration($configuration, $configs);

$loader = new YamlFileLoader(
$container,
new FileLocator(realpath(__DIR__ . '/../Resources/config'))
);
$loader->load('crayfish_commons.yaml');

if (!$container->has('Islandora\Crayfish\Commons\Syn\SettingsParser')) {
if (file_exists($config['syn_config'])) {
$xml = file_get_contents($config['syn_config']);
}
else {
throw new IOException("Security configuration not found. ${config['syn_config']}");
}

$container->register('Islandora\Crayfish\Commons\Syn\SettingsParser', SettingsParser::class)
->setArgument('$xml', $xml);
}

if (!$container->has('Islandora\Crayfish\Commons\Syn\JwtUserProvider')) {
$container->register('Islandora\Crayfish\Commons\Syn\JwtUserProvider', JwtUserProvider::class);
}
if (!$container->has('Islandora\Crayfish\Commons\Syn\JwtFactory')) {
$container->register('Islandora\Crayfish\Commons\Syn\JwtFactory', JwtFactory::class);
}
if (!$container->has('Islandora\Crayfish\Commons\Syn\JwtAuthenticator')) {
$container->register('Islandora\Crayfish\Commons\Syn\JwtAuthenticator', JwtAuthenticator::class)
->setAutowired(true);
}

if (!$container->has('Islandora\Chullo\IFedoraApi')) {
$container->register('Islandora\Chullo\IFedoraApi', IFedoraApi::class)
->setFactory('Islandora\Chullo\FedoraApi::create')
->setArgument('$fedora_rest_url', $config['fedora_base_uri']);
$container->setAlias('Islandora\Chullo\FedoraApi', 'Islandora\Chullo\IFedoraApi');
}
}
}
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions Resources/config/crayfish_commons.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

services:
_defaults:
autowire: true
autoconfigure: true
public: true

# These services rely on expected container available DI injections.
# Other services are loaded in the CrayfishCommonsExtension class.
Islandora\Crayfish\Commons\CmdExecuteService: ~

Islandora\Crayfish\Commons\EntityMapper\:
resource: '../../EntityMapper/*'

Islandora\Crayfish\Commons\ApixMiddleware:
tags:
- { name: kernel.event_subscriber, event: kernel.request }

# Aliases, if the class has not yet been instantiated it will be
# in CrayfishCommonsExtension class
crayfish.cmd_execute_service:
alias: Islandora\Crayfish\Commons\CmdExecuteService

# Map the concrete class to the interface.
Islandora\Crayfish\Commons\EntityMapper\EntityMapperInterface: '@Islandora\Crayfish\Commons\EntityMapper\EntityMapper'
4 changes: 4 additions & 0 deletions Resources/default_syn.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Default Config -->
<config version='1'>
</config>
Loading

0 comments on commit e7b8cbd

Please sign in to comment.