Skip to content

Commit

Permalink
Merge pull request #7 from kawanamiyuu/demo
Browse files Browse the repository at this point in the history
add Demo
  • Loading branch information
kawanamiyuu authored Jul 13, 2020
2 parents 83d473a + ecbd339 commit 4a773ed
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/build/
/demo/cache/
/tests/cache/
/vendor/
/composer.lock
/.phpunit.result.cache
39 changes: 31 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@

[![badge](https://github.com/kawanamiyuu/K9u.Framework/workflows/CI/badge.svg)](https://github.com/kawanamiyuu/K9u.Framework/actions?query=workflow%3ACI)

## Overview

```php
use Acme\BarMiddleware;
use Acme\BuzRequestHandler;
use Acme\FooMiddleware;
use K9u\Framework\ApplicationInterface;
use K9u\Framework\CachedInjectorFactory;
use K9u\Framework\Demo\FakeMiddleware;
use K9u\Framework\Demo\FakeRequestHandler;
use K9u\Framework\FrameworkModule;
use Laminas\Diactoros\ServerRequestFactory;
use Ray\Di\AbstractModule;

class AppModule extends AbstractModule
{
protected function configure()
{
$middlewares = [
FakeMiddleware::class,
FakeRequestHandler::class
];

$module = new FrameworkModule([
FooMiddleware::class,
BarMiddleware::class,
BuzRequestHandler::class,
]);
$this->install(new FrameworkModule($middlewares));
}
}

$module = new AppModule();
$injector = (new CachedInjectorFactory('/path/to/cache'))($module);

$app = $injector->getInstance(ApplicationInterface::class);
Expand All @@ -26,3 +36,16 @@ $request = ServerRequestFactory::fromGlobals();

$app($request); // handle request and emit response
```

## Run demo application

See [demo](demo/).

```bash
git clone https://github.com/kawanamiyuu/K9u.Framework.git
cd K9u.Framework
composer install
composer serve:demo

# access to http://localhost:8080
```
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,26 @@
},
"autoload-dev": {
"psr-4": {
"K9u\\Framework\\": "tests/"
"K9u\\Framework\\": "tests/",
"K9u\\Framework\\Demo\\": "demo/src"
}
},
"scripts": {
"check":["@lint", "@test"],
"test": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text",
"lint": [
"phpcs --standard=PSR12 src",
"phpmd src text cleancode,codesize,controversial,design,naming,unusedcode",
"phpstan analyze --no-progress --level=max src"
"phpcs --standard=PSR12 src demo/src",
"phpmd src,demo/src text cleancode,codesize,controversial,design,naming,unusedcode",
"phpstan analyze --no-progress --level=max src demo/src"
],
"fmt": "phpcbf --standard=PSR12 src"
"fmt": "phpcbf --standard=PSR12 src demo/src",
"serve:demo": [
"rm -rf demo/cache",
"php -S localhost:8080 -t demo/public"
]
},
"config": {
"process-timeout": 0,
"sort-packages": true
}
}
22 changes: 22 additions & 0 deletions demo/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use K9u\Framework\ApplicationInterface;
use K9u\Framework\CachedInjectorFactory;
use K9u\Framework\Demo\AppModule;
use Laminas\Diactoros\ServerRequestFactory;

const DEMO_ROOT_DIR = __DIR__ . '/..';

require DEMO_ROOT_DIR . '/../vendor/autoload.php';

$module = new AppModule();
$injector = (new CachedInjectorFactory(DEMO_ROOT_DIR . '/cache'))($module);

$app = $injector->getInstance(ApplicationInterface::class);
/* @var ApplicationInterface $app */

$request = ServerRequestFactory::fromGlobals();

$app($request); // handle request and emit response
21 changes: 21 additions & 0 deletions demo/src/AppModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace K9u\Framework\Demo;

use K9u\Framework\FrameworkModule;
use Ray\Di\AbstractModule;

class AppModule extends AbstractModule
{
protected function configure()
{
$middlewares = [
FakeMiddleware::class,
FakeRequestHandler::class
];

$this->install(new FrameworkModule($middlewares));
}
}
2 changes: 1 addition & 1 deletion tests/FakeMiddleware.php → demo/src/FakeMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace K9u\Framework;
namespace K9u\Framework\Demo;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down
12 changes: 9 additions & 3 deletions tests/FakeRequestHandler.php → demo/src/FakeRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@

declare(strict_types=1);

namespace K9u\Framework;
namespace K9u\Framework\Demo;

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;

class FakeRequestHandler implements RequestHandlerInterface
{
private ResponseFactoryInterface $responseFactory;

public function __construct(ResponseFactoryInterface $responseFactory)
private StreamFactoryInterface $streamFactory;

public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
{
$this->responseFactory = $responseFactory;
$this->streamFactory = $streamFactory;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
unset($request); // unused

return $this->responseFactory->createResponse(200);
return $this->responseFactory->createResponse(200)
->withHeader('Content-Type', 'application/json; charset=utf-8')
->withBody($this->streamFactory->createStream('{"greeting": "Hello world !"}'));
}
}
5 changes: 5 additions & 0 deletions src/FrameworkModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace K9u\Framework;

use Laminas\Diactoros\ResponseFactory;
use Laminas\Diactoros\StreamFactory;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Ray\Di\AbstractModule;
use Ray\Di\Scope;
Expand Down Expand Up @@ -45,6 +47,9 @@ protected function configure(): void
$this->bind(ResponseFactoryInterface::class)
->to(ResponseFactory::class)->in(Scope::SINGLETON);

$this->bind(StreamFactoryInterface::class)
->to(StreamFactory::class)->in(Scope::SINGLETON);

$this->bind(ExceptionHandlerInterface::class)
->to(ExceptionHandler::class)->in(Scope::SINGLETON);

Expand Down
6 changes: 4 additions & 2 deletions tests/FrameworkModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@

class FrameworkModuleTest extends TestCase
{
private const CACHE_DIR = __DIR__ . '/cache';

public function setUp(): void
{
array_map('unlink', glob(__DIR__ . '/../build/tests/*'));
array_map('unlink', glob(self::CACHE_DIR . '/*'));
}

public function testCompile(): void
{
$module = new FrameworkModule();
$compiler = new DiCompiler($module, __DIR__ . '/../build/tests');
$compiler = new DiCompiler($module, self::CACHE_DIR);
$instance = $compiler->getInstance(ApplicationInterface::class);

$this->assertInstanceOf(ApplicationInterface::class, $instance);
Expand Down

0 comments on commit 4a773ed

Please sign in to comment.