Skip to content

Commit

Permalink
added switch dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ytake committed Mar 14, 2018
1 parent ff05464 commit 55cd55a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Facebook\HackRouter\BaseRouter;
use Nazg\Http\HttpMethod;
use Nazg\Heredity\Heredity;
use Nazg\Heredity\{MiddlewareStack, PsrContainerResolver};
use Nazg\Response\Emitter;
use Nazg\RequestHandler\FallbackHandler;
Expand All @@ -39,6 +40,8 @@ class Application {

protected ?BootstrapRegister $bootstrapRegister;

protected $flag = false;

public function __construct(protected DependencyInterface $dependency) {}

public function run(ServerRequestInterface $serverRequest): void {
Expand Down Expand Up @@ -143,6 +146,10 @@ private function registerMiddlewares(mixed $config): void {
}
}

public function setValidateAttribute(bool $flag) {
$this->flag = $flag;
}

protected function middlewareProcessor(
ImmVector<\Nazg\Types\TMiddlewareClass> $middleware,
ContainerInterface $container,
Expand All @@ -151,15 +158,16 @@ protected function middlewareProcessor(
$this->im
->concat($this->middleware())
->concat($middleware)->toArray();
$dispatcher = new Dispatcher(
new MiddlewareStack(
$appMiddleware,
new PsrContainerResolver($container),
),
$this->requestHandler ?: new FallbackHandler(),
$stack = new MiddlewareStack(
$appMiddleware,
new PsrContainerResolver($container),
);
$dispatcher->setContainer($container);
return $dispatcher;
if ($this->flag) {
$dispatcher = new Dispatcher($stack, $this->requestHandler ?: new FallbackHandler());
$dispatcher->setContainer($container);
return $dispatcher;
}
return new Heredity($stack, $this->requestHandler ?: new FallbackHandler());
}

protected function send(ResponseInterface $response): void {
Expand Down
1 change: 1 addition & 0 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testShouldBeValidationFaild():void {
]);
$app = new Application(new \Nazg\Foundation\Dependency\Dependency());
$app->setApplicationConfig($aggregator->getMergedConfig());
$app->setValidateAttribute(true);
$app->run(
ServerRequestFactory::fromGlobals([
'REQUEST_URI' => '/validate/12',
Expand Down

0 comments on commit 55cd55a

Please sign in to comment.