Skip to content
This repository has been archived by the owner on Dec 31, 2021. It is now read-only.

Commit

Permalink
Got rid of the BasePath middleware
Browse files Browse the repository at this point in the history
The reasons are:

1. It has been a continuous source of troubles during Slim upgrades.
2. I may have been abusing it since I was passing the full URL there instead of just a path.
3. It broke again after slimphp/Slim#2844 was merged.
4. I don't really need it right now.
  • Loading branch information
morozov committed Oct 11, 2019
1 parent 9840f36 commit 2d9fbd2
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 133 deletions.
1 change: 0 additions & 1 deletion etc/config.php
Expand Up @@ -10,5 +10,4 @@
'title' => 'Кремов и Хрусталёв',
'logo' => 'http://www.radiorecord.ru/i/img/rr-logo-podcast.png',
],
'baseUri' => getenv('BASE_URI') ?: '',
];
8 changes: 1 addition & 7 deletions etc/container.php
Expand Up @@ -7,7 +7,6 @@
use KiH\Client;
use KiH\Generator;
use KiH\Generator\Rss;
use KiH\Middleware\BasePath;
use KiH\Providers\Vk\Client as VkClient;
use Slim\App;
use Slim\Psr7\Factory\ResponseFactory;
Expand Down Expand Up @@ -52,7 +51,7 @@
$container->get(Client::class)
);
},
App::class => static function (Container $container) use ($settings) : App {
App::class => static function (Container $container) : App {
$app = new App(
new ResponseFactory(),
$container
Expand All @@ -64,11 +63,6 @@
$app->get('/media/{id}.mp3', Media::class)
->setName('media');

$app->add(new BasePath(
$app->getRouteCollector(),
$settings['baseUri']
));

return $app;
},
]);
2 changes: 1 addition & 1 deletion src/Action/Feed.php
Expand Up @@ -40,7 +40,7 @@ public function __invoke(Request $request, Response $response) : Response
}

$response->getBody()->write(
$this->generator->generate($feed)->saveXML()
$this->generator->generate($feed, $request->getUri())->saveXML()
);

return $response;
Expand Down
3 changes: 2 additions & 1 deletion src/Generator.php
Expand Up @@ -4,8 +4,9 @@

use DOMDocument;
use KiH\Entity\Feed;
use Psr\Http\Message\UriInterface;

interface Generator
{
public function generate(Feed $feed) : DOMDocument;
public function generate(Feed $feed, UriInterface $requestUri) : DOMDocument;
}
11 changes: 6 additions & 5 deletions src/Generator/Rss.php
Expand Up @@ -7,6 +7,7 @@
use KiH\Entity\Feed;
use KiH\Entity\Item;
use KiH\Generator;
use Psr\Http\Message\UriInterface;
use Slim\Interfaces\RouteParserInterface;

final class Rss implements Generator
Expand All @@ -28,7 +29,7 @@ public function __construct(RouteParserInterface $routeParser, array $settings)
$this->settings = $settings;
}

public function generate(Feed $feed) : DOMDocument
public function generate(Feed $feed, UriInterface $requestUri) : DOMDocument
{
$document = new DOMDocument('1.0', 'UTF-8');
$rss = $document->createElement('rss');
Expand All @@ -52,7 +53,7 @@ public function generate(Feed $feed) : DOMDocument
$link = $document->createElement('link');
$link->appendChild(
$document->createTextNode(
$this->routeParser->urlFor('index')
$this->routeParser->fullUrlFor($requestUri, 'index')
)
);
$channel->appendChild($link);
Expand All @@ -63,14 +64,14 @@ public function generate(Feed $feed) : DOMDocument

foreach ($feed as $file) {
$channel->appendChild(
$this->generateItem($document, $file)
$this->generateItem($document, $file, $requestUri)
);
}

return $document;
}

private function generateItem(DOMDocument $document, Item $file) : DOMElement
private function generateItem(DOMDocument $document, Item $file, UriInterface $requestUri) : DOMElement
{
$item = $document->createElement('item');

Expand All @@ -93,7 +94,7 @@ private function generateItem(DOMDocument $document, Item $file) : DOMElement
);
$item->appendChild($guid);

$url = $this->routeParser->urlFor('media', [
$url = $this->routeParser->fullUrlFor($requestUri, 'media', [
'id' => $file->getId(),
]);

Expand Down
47 changes: 0 additions & 47 deletions src/Middleware/BasePath.php

This file was deleted.

3 changes: 3 additions & 0 deletions tests/Action/FeedTest.php
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\UriInterface;
use const PHP_EOL;

class FeedTest extends TestCase
Expand All @@ -24,6 +25,8 @@ class FeedTest extends TestCase
public function invoke() : void
{
$request = $this->createMock(Request::class);
$request->method('getUri')
->willReturn($this->createMock(UriInterface::class));

$entity = new Entity([
new Item(
Expand Down
7 changes: 4 additions & 3 deletions tests/Generator/RssTest.php
Expand Up @@ -8,6 +8,7 @@
use KiH\Generator\Rss;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface;
use Slim\Interfaces\RouteParserInterface;
use function array_merge;
use function http_build_query;
Expand All @@ -23,8 +24,8 @@ public function generate() : void
$router = $this->createMock(RouteParserInterface::class);
$router->expects(
$this->any()
)->method('urlFor')
->willReturnCallback(static function (string $name, array $params) {
)->method('fullUrlFor')
->willReturnCallback(static function (UriInterface $requestUri, string $name, array $params) {
return 'http://example.com/index.php?'
. http_build_query(array_merge(['page' => $name], $params));
});
Expand All @@ -48,7 +49,7 @@ public function generate() : void

$this->assertXmlStringEqualsXmlFile(
__DIR__ . '/fixtures/rss.xml',
$rss->generate($folder)
$rss->generate($folder, $this->createMock(UriInterface::class))
);
}
}
68 changes: 0 additions & 68 deletions tests/Middleware/BasePathTest.php

This file was deleted.

0 comments on commit 2d9fbd2

Please sign in to comment.