Skip to content

Commit

Permalink
Filter through building container to required strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Bennett committed Aug 24, 2015
1 parent feb8337 commit 803cad3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 45 deletions.
46 changes: 20 additions & 26 deletions src/Strategy/AbstractStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,14 @@

use League\Container\Container;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use League\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

abstract class AbstractStrategy implements ContainerAwareInterface
{
/**
* @var \League\Container\ContainerInterface
*/
protected $container;

/**
* Set a container
*
* @param \League\Container\ContainerInterface $container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;

return $this;
}

/**
* Get the container
*
* @return \League\Container\ContainerInterface
*/
public function getContainer()
{
return (is_null($this->container)) ? new Container : $this->container;
}
use ContainerAwareTrait;

/**
* Invoke a controller action
Expand Down Expand Up @@ -75,4 +52,21 @@ protected function determineResponse($response)

return $response;
}

/**
* Get Request either from the container or else create it from globals.
*
* @return \Symfony\Component\HttpFoundation\Request
*/
protected function getRequest()
{
if (
$this->getContainer()->isRegistered('Symfony\Component\HttpFoundation\Request') ||
$this->getContainer()->isInServiceProvider('Symfony\Component\HttpFoundation\Request')
) {
return $this->getContainer()->get('Symfony\Component\HttpFoundation\Request');
}

return Request::createFromGlobals();
}
}
17 changes: 0 additions & 17 deletions src/Strategy/RequestResponseStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace League\Route\Strategy;

use RuntimeException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class RequestResponseStrategy extends AbstractStrategy implements StrategyInterface
Expand All @@ -28,20 +27,4 @@ public function dispatch($controller, array $vars)
'return an instance of [Symfony\Component\HttpFoundation\Response]'
);
}

/**
* Get Request either from the container or else create it from globals
* @return \Symfony\Component\HttpFoundation\Request
*/
protected function getRequest()
{
if (
$this->getContainer()->isRegistered('Symfony\Component\HttpFoundation\Request') ||
$this->getContainer()->isInServiceProvider('Symfony\Component\HttpFoundation\Request')
) {
return $this->getContainer()->get('Symfony\Component\HttpFoundation\Request');
}

return Request::createFromGlobals();
}
}
2 changes: 1 addition & 1 deletion src/Strategy/RestfulStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function dispatch($controller, array $vars)
{
try {
$response = $this->invokeController($controller, [
$this->getContainer()->get('Symfony\Component\HttpFoundation\Request'),
$this->getRequest(),
$vars
]);

Expand Down
7 changes: 6 additions & 1 deletion tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ public function testRestfulStrategyReturnsJsonResponseWhenHttpExceptionIsThrown(
->will($this->throwException(new HttpException\ConflictException));

$container = $this->getMock('League\Container\Container');
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');

$container->expects($this->at(1))
$container->expects($this->at(0))->method('isRegistered')->will($this->returnValue(false));
$container->expects($this->at(1))->method('isInServiceProvider')->will($this->returnValue(true));
$container->expects($this->at(2))->method('get')->will($this->returnValue($request));

$container->expects($this->at(3))
->method('get')
->with($this->equalTo('SomeClass'))
->will($this->returnValue($controller));
Expand Down

0 comments on commit 803cad3

Please sign in to comment.