-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate codebase from Woda namespace (#3)
- Loading branch information
1 parent
3e2e94f
commit a448d7d
Showing
22 changed files
with
782 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
"source": { | ||
"directories": [ | ||
"src" | ||
], | ||
"excludes": [ | ||
"src/ConfigProvider.php" | ||
] | ||
}, | ||
"logs": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\AssetManager\Asset; | ||
|
||
interface AssetFactoryInterface | ||
{ | ||
public function create(string $path): AssetInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\AssetManager\Asset; | ||
|
||
interface AssetInterface | ||
{ | ||
public function getPath(): string; | ||
|
||
public function getMimeType(): string; | ||
|
||
public function getContentLength(): string; | ||
|
||
public function getContent(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\AssetManager\Asset; | ||
|
||
use Narrowspark\MimeType\MimeTypeExtensionGuesser; | ||
use SplFileInfo; | ||
|
||
final class FileAsset implements AssetInterface | ||
{ | ||
private ?string $content; | ||
private string $fullPath; | ||
|
||
public function __construct(string $fullPath) | ||
{ | ||
$this->fullPath = $fullPath; | ||
$this->content = null; | ||
} | ||
|
||
public function getPath(): string | ||
{ | ||
return $this->fullPath; | ||
} | ||
|
||
public function getMimeType(): string | ||
{ | ||
return MimeTypeExtensionGuesser::guess($this->getExtension()) ?? 'application/octet-stream'; | ||
} | ||
|
||
public function getContent(): string | ||
{ | ||
if ($this->content === null) { | ||
$this->content = \Safe\file_get_contents($this->getPath()); | ||
} | ||
return $this->content; | ||
} | ||
|
||
public function getContentLength(): string | ||
{ | ||
return (string)strlen($this->getContent()); | ||
} | ||
|
||
private function getExtension(): string | ||
{ | ||
return (new SplFileInfo($this->getPath()))->getExtension(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\AssetManager\Asset; | ||
|
||
final class FileAssetFactory implements AssetFactoryInterface | ||
{ | ||
public function create(string $path): AssetInterface | ||
{ | ||
return new FileAsset($path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\AssetManager; | ||
|
||
use Eventjet\AssetManager\Asset\AssetFactoryInterface; | ||
use Eventjet\AssetManager\Asset\FileAssetFactory; | ||
use Eventjet\AssetManager\Resolver\PathMappingResolver; | ||
use Eventjet\AssetManager\Resolver\PathMappingResolverFactory; | ||
use Eventjet\AssetManager\Resolver\ResolverInterface; | ||
|
||
final class ConfigProvider | ||
{ | ||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function __invoke(): array | ||
{ | ||
return [ | ||
'dependencies' => [ | ||
'aliases' => [ | ||
AssetFactoryInterface::class => FileAssetFactory::class, | ||
ResolverInterface::class => PathMappingResolver::class, | ||
], | ||
'factories' => [ | ||
PathMappingResolver::class => PathMappingResolverFactory::class, | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\AssetManager\Middleware; | ||
|
||
use Eventjet\AssetManager\Service\AssetManager; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
final class ResolveAssetMiddleware implements MiddlewareInterface | ||
{ | ||
private AssetManager $assetManager; | ||
|
||
public function __construct(AssetManager $assetManager) | ||
{ | ||
$this->assetManager = $assetManager; | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
if (!$this->assetManager->resolvesToAsset($request)) { | ||
return $handler->handle($request); | ||
} | ||
return $this->assetManager->buildAssetResponse($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\AssetManager\Resolver; | ||
|
||
use Eventjet\AssetManager\Asset\AssetFactoryInterface; | ||
use Eventjet\AssetManager\Asset\AssetInterface; | ||
|
||
final class PathMappingResolver implements ResolverInterface | ||
{ | ||
/** @var string[] */ | ||
private array $pathMapping; | ||
private AssetFactoryInterface $factory; | ||
|
||
/** | ||
* @param string[] $pathMapping | ||
*/ | ||
public function __construct(array $pathMapping, AssetFactoryInterface $factory) | ||
{ | ||
$this->pathMapping = $pathMapping; | ||
$this->factory = $factory; | ||
} | ||
|
||
public function resolve(string $path): ?AssetInterface | ||
{ | ||
if ($path === '/') { | ||
return null; | ||
} | ||
foreach ($this->pathMapping as $current) { | ||
$fullPath = $current . $path; | ||
if (!file_exists($fullPath)) { | ||
continue; | ||
} | ||
return $this->factory->create($fullPath); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\AssetManager\Resolver; | ||
|
||
use Eventjet\AssetManager\Asset\AssetFactoryInterface; | ||
use Psr\Container\ContainerInterface; | ||
use RuntimeException; | ||
|
||
use function count; | ||
|
||
final class PathMappingResolverFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): PathMappingResolver | ||
{ | ||
return new PathMappingResolver( | ||
$this->paths($container), | ||
$container->get(AssetFactoryInterface::class) | ||
); | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
private function paths(ContainerInterface $container): array | ||
{ | ||
/** @var array<string, mixed> $config */ | ||
$config = $container->get('config'); | ||
$paths = $config['eventjet']['asset_manager']['paths'] ?? []; | ||
if (count($paths) < 1) { | ||
throw new RuntimeException( | ||
'Path mapping is missing. Please configure your path mapping at "eventjet.asset_manager.paths"' | ||
); | ||
} | ||
return $paths; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\AssetManager\Resolver; | ||
|
||
use Eventjet\AssetManager\Asset\AssetInterface; | ||
|
||
interface ResolverInterface | ||
{ | ||
public function resolve(string $path): ?AssetInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\AssetManager\Service; | ||
|
||
use Eventjet\AssetManager\Resolver\ResolverInterface; | ||
use Laminas\Diactoros\Response; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use RuntimeException; | ||
|
||
final class AssetManager | ||
{ | ||
private ResolverInterface $resolver; | ||
private StreamFactoryInterface $streamFactory; | ||
|
||
public function __construct(ResolverInterface $resolver, StreamFactoryInterface $streamFactory) | ||
{ | ||
$this->resolver = $resolver; | ||
$this->streamFactory = $streamFactory; | ||
} | ||
|
||
public function resolvesToAsset(RequestInterface $request): bool | ||
{ | ||
return $this->resolver->resolve($request->getUri()->getPath()) !== null; | ||
} | ||
|
||
public function buildAssetResponse(RequestInterface $request): ResponseInterface | ||
{ | ||
$asset = $this->resolver->resolve($request->getUri()->getPath()); | ||
if ($asset === null) { | ||
throw new RuntimeException( | ||
'Asset could not be resolved. Use "resolvesToAsset" before "buildAssetResponse".' | ||
); | ||
} | ||
return (new Response()) | ||
->withStatus(200) | ||
->withAddedHeader('Content-Transfer-Encoding', 'binary') | ||
->withAddedHeader('Content-Type', $asset->getMimeType()) | ||
->withAddedHeader('Content-Length', $asset->getContentLength()) | ||
->withBody($this->streamFactory->createStreamFromFile($asset->getPath())); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
tests/functional/Middleware/ResolveAssetMiddlewareTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Eventjet\Test\Functional\AssetManager\Middleware; | ||
|
||
use Eventjet\AssetManager\Asset\FileAssetFactory; | ||
use Eventjet\AssetManager\Middleware\ResolveAssetMiddleware; | ||
use Eventjet\AssetManager\Resolver\PathMappingResolver; | ||
use Eventjet\AssetManager\Service\AssetManager; | ||
use Eventjet\Test\Unit\AssetManager\ObjectFactory; | ||
use Laminas\Diactoros\StreamFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ResolveAssetMiddlewareTest extends TestCase | ||
{ | ||
private ResolveAssetMiddleware $middleware; | ||
|
||
public function testHandlerIsCalledWhenAssetIsNotFound(): void | ||
{ | ||
$called = false; | ||
$handler = ObjectFactory::requestHandlerSpy($called); | ||
|
||
$this->middleware->process( | ||
ObjectFactory::serverRequest('GET', '/' . ObjectFactory::randomFileName() . '.jpg'), | ||
$handler | ||
); | ||
|
||
self::assertTrue($called); | ||
} | ||
|
||
public function testHandlerIsNotCalledWhenAssetIsFound(): void | ||
{ | ||
$fileName = ObjectFactory::randomFileName() . '.jpg'; | ||
$called = false; | ||
$handler = ObjectFactory::requestHandlerSpy($called); | ||
ObjectFactory::tmpFile('', $fileName); | ||
|
||
$this->middleware->process(ObjectFactory::serverRequest('GET', '/' . $fileName), $handler); | ||
|
||
self::assertFalse($called); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$paths = [ObjectFactory::pathToTmpFiles()]; | ||
$manager = new AssetManager(new PathMappingResolver($paths, new FileAssetFactory()), new StreamFactory()); | ||
$this->middleware = new ResolveAssetMiddleware($manager); | ||
} | ||
} |
Oops, something went wrong.