Skip to content

Commit

Permalink
wip impl valid aspect
Browse files Browse the repository at this point in the history
  • Loading branch information
ytake committed Jan 12, 2018
1 parent 0fe2337 commit 3de24c2
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 18 deletions.
18 changes: 10 additions & 8 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Nazg\Foundation;

use Facebook\HackRouter\BaseRouter;
use Ytake\Heredity\Heredity;
use Ytake\Heredity\MiddlewareStack;
use Ytake\Heredity\PsrContainerResolver;
use Nazg\Http\HttpMethod;
use Nazg\RequestHandler\FallbackHandler;
use Nazg\Foundation\Middleware\Dispatcher;
use Nazg\Foundation\Dependency\DependencyInterface;
use Nazg\Routing\HttpMethod;
use Interop\Http\Server\RequestHandlerInterface;
use Interop\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -49,9 +49,8 @@ public function run(
}
$heredity = $this->middlewareProcessor($middleware, $container);
$this->send(
$heredity->process(
$this->marshalAttributes($serverRequest, $attributes),
$this->requestHandler ?: new FallbackHandler()
$heredity->handle(
$this->marshalAttributes($serverRequest, $attributes)
)
);
}
Expand Down Expand Up @@ -109,15 +108,18 @@ private function registerMiddlewares(mixed $config): void {
protected function middlewareProcessor(
TMiddlewareClass $middleware,
ContainerInterface $container
): MiddlewareInterface {
): RequestHandlerInterface {
$appMiddleware = $this->im->concat($this->middleware())
|>$$->concat(Set{$middleware})->toArray();
return new Heredity(
$dispatcher = new Dispatcher(
new MiddlewareStack(
$appMiddleware,
new PsrContainerResolver($container)
),
);
$this->requestHandler ?: new FallbackHandler()
);
$dispatcher->setContainer($container);
return $dispatcher;
}

protected function send(ResponseInterface $response): void {
Expand Down
53 changes: 53 additions & 0 deletions src/Foundation/Middleware/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?hh // strict

namespace Nazg\Foundation\Middleware;

use ReflectionMethod;
use Ytake\Heredity\Heredity;
use Nazg\Foundation\Validation\Attribute;
use Nazg\Foundation\Validation\Validation;
use Nazg\Foundation\Validation\ValidatorFactory;
use Interop\Http\Server\MiddlewareInterface;
use Interop\Http\Server\RequestHandlerInterface;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

enum InterceptorMethod: string {
Process = 'process';
}

class Dispatcher extends Heredity {

protected int $validatorIndex = 0;
protected ?ContainerInterface $container;

<<__Override>>
protected function processor(
MiddlewareInterface $middleware,
ServerRequestInterface $request
): ResponseInterface {
$this->findAttributse($middleware, $request);
return $middleware->process($request, $this);
}

protected function findAttributse(
MiddlewareInterface $middleware,
ServerRequestInterface $request
): void {
$rm = new ReflectionMethod($middleware, InterceptorMethod::Process);
$attribute = $rm->getAttribute(Attribute::Named);
if (is_array($attribute)) {
if(array_key_exists($this->validatorIndex, $attribute)) {
$v = new ValidatorFactory(
$this->container?->get((string)$attribute[$this->validatorIndex])
);
var_dump($v->validate());
}
}
}

public function setContainer(ContainerInterface $container): void {
$this->container = $container;
}
}
21 changes: 21 additions & 0 deletions src/Foundation/Validation/Validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?hh // strict

namespace Nazg\Foundation\Validation;

use Psr\Http\Message\ServerRequestInterface;

enum Attribute: string as string {
Named = 'RequestValidation';
}

<<__ConsistentConstruct>>
abstract class Validation {

public function __construct(
protected ServerRequestInterface $request
) {}

public function validate(): bool {
return true;
}
}
17 changes: 17 additions & 0 deletions src/Foundation/Validation/ValidatorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?hh // strict

namespace Nazg\Foundation\Validation;

class ValidatorFactory {

public function __construct(
protected mixed $validatorName
) {}

public function validate(): bool {
if($this->validatorName instanceof Validation) {
return $this->validatorName->validate();
}
return true;
}
}
18 changes: 18 additions & 0 deletions tests/Action/ValidateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?hh

use Nazg\Foundation\Validation\Attribute;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Interop\Http\Server\MiddlewareInterface;
use Interop\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\JsonResponse;

final class ValidateAction implements MiddlewareInterface {
<<RequestValidation(\ValidateActionValid::class)>>
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler,
): ResponseInterface {
return new JsonResponse([]);
}
}
21 changes: 20 additions & 1 deletion tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testShouldReturnApplicationInstance(): void {
$this->assertInstanceOf(Application::class, $app);
}

public function testG(): void {
public function testShouldReturnQueryParam(): void {
$aggregator = new ConfigAggreagator([
new PhpFileProvider(__DIR__ . '/config/*.{hh,php}'),
new ArrayProvider(['config_cache_enabled' => false])
Expand All @@ -41,6 +41,25 @@ public function testG(): void {
])
);
}

public function testShouldBeValidationFaild():void {
$aggregator = new ConfigAggreagator([
new PhpFileProvider(__DIR__ . '/config/*.{hh,php}'),
new ArrayProvider(['config_cache_enabled' => false])
]);
$app = new Application(new \Nazg\Foundation\Dependency\Dependency());
$app->setApplicationConfig($aggregator->getMergedConfig());
$app->run(
ServerRequestFactory::fromGlobals([
'REQUEST_URI' => '/validate/12',
'REQUEST_METHOD' => 'GET'
],
[
'parameter1' => 'testing',
'parameter2' => 'hhvm',
])
);
}
}

class OverrideApplication extends Application {
Expand Down
10 changes: 5 additions & 5 deletions tests/Middleware/LogExceptionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
use Ytake\HHContainer\FactoryContainer;
use Psr\Log\LoggerInterface;
use Zend\Diactoros\ServerRequestFactory;
use Ytake\Heredity\Heredity;
use Ytake\Heredity\MiddlewareStack;
use Ytake\Heredity\PsrContainerResolver;
use Nazg\Foundation\Middleware\Dispatcher;

class LogExceptionMiddlewareTest extends TEstCase {
/**
* @expectedException \Exception
*/
public function testShouldThrowException(): void {
$container = $this->getDependencyContainer();
$heredity = new Heredity(
$heredity = new Dispatcher(
new MiddlewareStack(
[LogExceptionMiddleware::class, FakeThrowExceptionMiddleware::class],
new PsrContainerResolver($container),
),
);
$response = $heredity->process(
ServerRequestFactory::fromGlobals(),
new StubRequestHandler(),
);
$response = $heredity->handle(
ServerRequestFactory::fromGlobals()
);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/Middleware/SimpleCorsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
use Ytake\HHContainer\ServiceModule;
use Ytake\HHContainer\FactoryContainer;
use Zend\Diactoros\ServerRequestFactory;
use Ytake\Heredity\Heredity;
use Ytake\Heredity\MiddlewareStack;
use Ytake\Heredity\PsrContainerResolver;
use Nazg\Foundation\Middleware\Dispatcher;

class SimpleCorsMiddlewareTest extends TestCase {

public function testShouldThrowException(): void {
$container = $this->getDependencyContainer();
$heredity = new Heredity(
$heredity = new Dispatcher(
new MiddlewareStack(
[SimpleCorsMiddleware::class],
new PsrContainerResolver($container),
),
new StubRequestHandler(),
);
$response = $heredity->process(
$response = $heredity->handle(
ServerRequestFactory::fromGlobals(),
new StubRequestHandler(),
);
$headers = $response->getHeaders();
$this->assertArrayHasKey(AccessControl::AllowHeaders, $headers);
Expand Down
8 changes: 8 additions & 0 deletions tests/TestingServiceModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ public function provide(FactoryContainer $container): void {
\ParameterAction::class,
$container ==> new \ParameterAction()
);
$container->set(
\ValidateAction::class,
$container ==> new \ValidateAction()
);
$container->set(
\ValidateActionValid::class,
$container ==> new \ValidateActionValid()
);
}
}
7 changes: 7 additions & 0 deletions tests/Validation/ValidateActionValid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?hh // strict

use Nazg\Foundation\Validation\Validation;

final class ValidateActionValid extends Validation {

}
1 change: 1 addition & 0 deletions tests/config/routes.testing.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
HttpMethod::GET => ImmMap {
'/' => IndexAction::class,
'/testing/{id}' => ParameterAction::class,
'/validate/{id}' => ValidateAction::class,
},
},
];

0 comments on commit 3de24c2

Please sign in to comment.