Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Crayfish-Commons a Symfony bundle only. #49

Merged
merged 6 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Comment on lines -50 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In poking around and running tests, I found that the we broke the tests here, where it tests for the 400 error being emitted... easy enough fix just setting the response with something like:

diff --git a/ApixMiddleware.php b/ApixMiddleware.php
index b75df0a..5a29209 100644
--- a/ApixMiddleware.php
+++ b/ApixMiddleware.php
@@ -62,6 +62,10 @@ class ApixMiddleware implements EventSubscriberInterface
         if (!$request->headers->has("Apix-Ldp-Resource")) {
             $this->log->debug("No Apix-Ldp-Resource header present, no fedora_resource set");
             $request->attributes->set('fedora_resource', false);
+           $event->setResponse(new Response(
+                "Malformed request, no Apix-Ldp-Resource header present",
+                400
+            ));
             return;
         }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking to address in #57

}

// 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.
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