Skip to content

Commit

Permalink
Revert "Move PSR17 factories to the registry"
Browse files Browse the repository at this point in the history
This reverts commit 56afe05.
  • Loading branch information
fisharebest committed May 4, 2021
1 parent 56afe05 commit 00c45d2
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 176 deletions.
18 changes: 14 additions & 4 deletions app/Helpers/functions.php
Expand Up @@ -20,13 +20,14 @@
use Aura\Router\RouterContainer;
use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Webtrees\Html;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Session as WebtreesSession;
use Fisharebest\Webtrees\View as WebtreesView;
use Fisharebest\Webtrees\Webtrees;
use Illuminate\Container\Container;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;

/**
* Get the IoC container, or fetch something from it.
Expand Down Expand Up @@ -95,7 +96,10 @@ function csrf_token(): string
*/
function redirect(string $url, $code = StatusCodeInterface::STATUS_FOUND): ResponseInterface
{
return Registry::responseFactory()
/** @var ResponseFactoryInterface $response_factory */
$response_factory = app(ResponseFactoryInterface::class);

return $response_factory
->createResponse($code)
->withHeader('Location', $url);
}
Expand Down Expand Up @@ -130,9 +134,15 @@ function response($content = '', $code = StatusCodeInterface::STATUS_OK, $header
}
}

$stream = Registry::streamFactory()->createStream($content);
/** @var ResponseFactoryInterface $response_factory */
$response_factory = app(ResponseFactoryInterface::class);

/** @var StreamFactoryInterface $stream_factory */
$stream_factory = app(StreamFactoryInterface::class);

$stream = $stream_factory->createStream($content);

$response = Registry::responseFactory()
$response = $response_factory
->createResponse($code)
->withBody($stream);

Expand Down
78 changes: 78 additions & 0 deletions app/Http/Middleware/RegisterFactories.php
@@ -0,0 +1,78 @@
<?php

/**
* webtrees: online genealogy
* Copyright (C) 2021 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Fisharebest\Webtrees\Http\Middleware;

use Fisharebest\Webtrees\Factories\CacheFactory;
use Fisharebest\Webtrees\Factories\FamilyFactory;
use Fisharebest\Webtrees\Factories\FilesystemFactory;
use Fisharebest\Webtrees\Factories\ElementFactory;
use Fisharebest\Webtrees\Factories\GedcomRecordFactory;
use Fisharebest\Webtrees\Factories\HeaderFactory;
use Fisharebest\Webtrees\Factories\ImageFactory;
use Fisharebest\Webtrees\Factories\IndividualFactory;
use Fisharebest\Webtrees\Factories\LocationFactory;
use Fisharebest\Webtrees\Factories\MediaFactory;
use Fisharebest\Webtrees\Factories\NoteFactory;
use Fisharebest\Webtrees\Factories\RepositoryFactory;
use Fisharebest\Webtrees\Factories\SlugFactory;
use Fisharebest\Webtrees\Factories\SourceFactory;
use Fisharebest\Webtrees\Factories\SubmissionFactory;
use Fisharebest\Webtrees\Factories\SubmitterFactory;
use Fisharebest\Webtrees\Factories\XrefFactory;
use Fisharebest\Webtrees\Registry;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
* Middleware to register various factory objects.
*/
class RegisterFactories implements MiddlewareInterface
{
/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
Registry::cache(new CacheFactory());
Registry::familyFactory(new FamilyFactory());
Registry::filesystem(new FilesystemFactory());
Registry::elementFactory(new ElementFactory());
Registry::gedcomRecordFactory(new GedcomRecordFactory());
Registry::headerFactory(new HeaderFactory());
Registry::imageFactory(new ImageFactory());
Registry::individualFactory(new IndividualFactory());
Registry::locationFactory(new LocationFactory());
Registry::mediaFactory(new MediaFactory());
Registry::noteFactory(new NoteFactory());
Registry::repositoryFactory(new RepositoryFactory());
Registry::slugFactory(new SlugFactory());
Registry::sourceFactory(new SourceFactory());
Registry::submissionFactory(new SubmissionFactory());
Registry::submitterFactory(new SubmitterFactory());
Registry::xrefFactory(new XrefFactory());

return $handler->handle($request);
}
}
27 changes: 20 additions & 7 deletions app/Http/RequestHandlers/ExportGedcomClient.php
Expand Up @@ -30,12 +30,15 @@
use League\Flysystem\FilesystemException;
use League\Flysystem\ZipArchive\FilesystemZipArchiveProvider;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\RequestHandlerInterface;
use RuntimeException;

use function addcslashes;
use function app;
use function assert;
use function fclose;
use function fopen;
Expand Down Expand Up @@ -148,11 +151,16 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}

// Use a stream, so that we do not have to load the entire file into memory.
$http_stream = Registry::streamFactory()->createStreamFromFile($temp_zip_file);
$filename = addcslashes($download_filename, '"') . '.zip';
$stream_factory = app(StreamFactoryInterface::class);
assert($stream_factory instanceof StreamFactoryInterface);

return Registry::responseFactory()
->createResponse()
$http_stream = $stream_factory->createStreamFromFile($temp_zip_file);
$filename = addcslashes($download_filename, '"') . '.zip';

/** @var ResponseFactoryInterface $response_factory */
$response_factory = app(ResponseFactoryInterface::class);

return $response_factory->createResponse()
->withBody($http_stream)
->withHeader('Content-Type', 'application/zip')
->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
Expand All @@ -169,10 +177,15 @@ public function handle(ServerRequestInterface $request): ResponseInterface

$charset = $convert ? 'ISO-8859-1' : 'UTF-8';

$http_stream = Registry::streamFactory()->createStreamFromResource($resource);
$stream_factory = app(StreamFactoryInterface::class);
assert($stream_factory instanceof StreamFactoryInterface);

$http_stream = $stream_factory->createStreamFromResource($resource);

/** @var ResponseFactoryInterface $response_factory */
$response_factory = app(ResponseFactoryInterface::class);

return Registry::responseFactory()
->createResponse()
return $response_factory->createResponse()
->withBody($http_stream)
->withHeader('Content-Type', 'text/x-gedcom; charset=' . $charset)
->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($download_filename, '"') . '"');
Expand Down
4 changes: 3 additions & 1 deletion app/Http/RequestHandlers/ImportGedcomAction.php
Expand Up @@ -30,8 +30,10 @@
use Nyholm\Psr7\UploadedFile;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function app;
use function assert;
use function basename;
use function redirect;
Expand Down Expand Up @@ -96,7 +98,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface

if ($basename) {
$resource = $data_filesystem->readStream($basename);
$stream = Registry::streamFactory()->createStreamFromResource($resource);
$stream = app(StreamFactoryInterface::class)->createStreamFromResource($resource);
$this->tree_service->importGedcomFile($tree, $stream, $basename);
} else {
FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger');
Expand Down
5 changes: 4 additions & 1 deletion app/Http/RequestHandlers/SynchronizeTrees.php
Expand Up @@ -26,6 +26,9 @@
use Fisharebest\Webtrees\Services\AdminService;
use Fisharebest\Webtrees\Services\TimeoutService;
use Fisharebest\Webtrees\Services\TreeService;
use League\Flysystem\FilesystemException;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToRetrieveMetadata;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
Expand Down Expand Up @@ -86,7 +89,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface

if ($tree->getPreference('filemtime') !== $filemtime) {
$resource = $data_filesystem->readStream($gedcom_file);
$stream = Registry::streamFactory()->createStreamFromResource($resource);
$stream = app(StreamFactoryInterface::class)->createStreamFromResource($resource);
$this->tree_service->importGedcomFile($tree, $stream, $gedcom_file);
$stream->close();
$tree->setPreference('filemtime', $filemtime);
Expand Down
10 changes: 7 additions & 3 deletions app/Module/ClippingsCartModule.php
Expand Up @@ -51,8 +51,10 @@
use League\Flysystem\FilesystemException;
use League\Flysystem\ZipArchive\FilesystemZipArchiveProvider;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use RuntimeException;

use function app;
Expand Down Expand Up @@ -388,10 +390,12 @@ public function postDownloadAction(ServerRequestInterface $request): ResponseInt
$zip_filesystem->writeStream('clippings.ged', $stream);

// Use a stream, so that we do not have to load the entire file into memory.
$stream = Registry::streamFactory()->createStreamFromFile($temp_zip_file);
$stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file);

return Registry::responseFactory()
->createResponse()
/** @var ResponseFactoryInterface $response_factory */
$response_factory = app(ResponseFactoryInterface::class);

return $response_factory->createResponse()
->withBody($stream)
->withHeader('Content-Type', 'application/zip')
->withHeader('Content-Disposition', 'attachment; filename="clippings.zip');
Expand Down

0 comments on commit 00c45d2

Please sign in to comment.