Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
Adding controller arguments passthrough
Browse files Browse the repository at this point in the history
  • Loading branch information
Nik Barham committed May 9, 2017
1 parent 0162572 commit 3c7bcd0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/HandlerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

class HandlerContainer implements Delegate
{
/** @var string */
public $route;

/** @var Middleware|null[] */
public $middlewareStack = [null];

/** @var mixed */
public $handler;

/** @var mixed[] */
protected $controllerArguments = [];

/**
* Store a handler against a list of middleware
*
Expand Down Expand Up @@ -63,7 +63,7 @@ public function dispatchController(Request $request) : Response
// Trim class/namespace
$class = trim($class, ' \t\n\r\0\x0B\\');

$c = new $class;
$c = new $class(...$this->controllerArguments);
$return = $c->$function($request, ...$args);
}

Expand All @@ -77,4 +77,14 @@ public function dispatchController(Request $request) : Response
array('content-type' => 'text/html')
);
}

public function setControllerDependancies(...$args)
{
$this->controllerArguments = $args;
}

public function __sleep()
{
return ['handler', 'middlewareStack'];
}
}
9 changes: 9 additions & 0 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Router
protected $routeCollection;
protected $dispatcher;
protected $cache;

/** @var mixed[] */
protected $controllerArguments = [];

public function __construct(array $options = [], $cache = null)
{
Expand Down Expand Up @@ -111,6 +114,7 @@ public function run(Request $request)
// $response will be of type HandlerContainer
$dispatcher = unserialize($dispatch[1]);

$dispatcher->setControllerDependancies(...$this->controllerArguments);
$response = $dispatcher->process($request);
break;
}
Expand All @@ -136,4 +140,9 @@ public function default405route() : Response
['content-type' => 'text/html']
);
}

public function setControllerDependancies(...$args)
{
$this->controllerArguments = $args;
}
}

0 comments on commit 3c7bcd0

Please sign in to comment.