Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from kordos/silex-2
Silex 2
- Loading branch information
Showing
5 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
language: php | ||
|
||
sudo: false | ||
|
||
php: | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
- hhvm | ||
- 7.1 | ||
|
||
matrix: | ||
include: | ||
- php: 5.5 | ||
env: dependencies=lowest | ||
|
||
sudo: false | ||
|
||
before_script: | ||
- composer self-update | ||
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then composer require satooshi/php-coveralls:dev-master -n ; fi | ||
- if [[ "$TRAVIS_PHP_VERSION" != '5.6' ]]; then composer install -n ; fi | ||
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi; | ||
|
||
script: | ||
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then phpunit --coverage-clover clover.xml ; fi | ||
- if [[ "$TRAVIS_PHP_VERSION" != '5.6' ]]; then phpunit ; fi | ||
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi | ||
- if [[ "$TRAVIS_PHP_VERSION" != '5.6' ]]; then ./vendor/bin/phpunit ; fi | ||
|
||
after_script: | ||
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then php vendor/bin/coveralls -v ; fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace DI\Bridge\Silex\Controller; | ||
|
||
use Invoker\ParameterResolver\ParameterResolver; | ||
use Invoker\Reflection\CallableReflection; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
class ArgumentResolver implements ArgumentResolverInterface | ||
{ | ||
/** | ||
* @var ParameterResolver | ||
*/ | ||
private $parameterResolver; | ||
|
||
public function __construct(ParameterResolver $parameterResolver) | ||
{ | ||
$this->parameterResolver = $parameterResolver; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getArguments(Request $request, $controller) | ||
{ | ||
$controllerReflection = CallableReflection::create($controller); | ||
$controllerParameters = $controllerReflection->getParameters(); | ||
$resolvedArguments = []; | ||
|
||
foreach ($controllerParameters as $index => $parameter) { | ||
if ('request' === $parameter->getName() || ($parameter->getClass() && $parameter->getClass()->isInstance($request))) { | ||
$resolvedArguments[$index] = $request; | ||
|
||
break; | ||
} | ||
} | ||
|
||
$arguments = $this->parameterResolver->getParameters( | ||
$controllerReflection, | ||
$request->attributes->all(), | ||
$resolvedArguments | ||
); | ||
|
||
ksort($arguments); | ||
|
||
// Check if all parameters are resolved | ||
$diff = array_diff_key($controllerParameters, $arguments); | ||
if (0 < count($diff)) { | ||
/** @var \ReflectionParameter $parameter */ | ||
$parameter = reset($diff); | ||
throw new \RuntimeException(sprintf( | ||
'Controller "%s" requires that you provide a value for the "$%s" argument.', | ||
$controllerReflection->getName(), | ||
$parameter->getName() | ||
)); | ||
} | ||
|
||
return $arguments; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace DI\Bridge\Silex\Test\Provider; | ||
|
||
use DI\Bridge\Silex\Application; | ||
use DI\Bridge\Silex\CallbackInvoker; | ||
use DI\Bridge\Silex\CallbackResolver; | ||
use DI\Bridge\Silex\Controller\ArgumentResolver; | ||
use DI\Bridge\Silex\Controller\ControllerResolver; | ||
use DI\Bridge\Silex\Provider\HttpKernelServiceProvider; | ||
use Interop\Container\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
|
||
class HttpKernelServiceProviderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var CallbackInvoker | ||
*/ | ||
private $callbackInvoker; | ||
|
||
/** | ||
* @var Application | ||
*/ | ||
private $application; | ||
|
||
/** | ||
* @var HttpKernelServiceProvider | ||
*/ | ||
private $httpKernelProvider; | ||
|
||
public function setUp() | ||
{ | ||
$this->container = $this->prophesize(ContainerInterface::class); | ||
$this->callbackInvoker = $this->prophesize(CallbackInvoker::class); | ||
$this->httpKernelProvider = new HttpKernelServiceProvider($this->container->reveal(), $this->callbackInvoker->reveal()); | ||
$this->application = new Application(); | ||
} | ||
|
||
public function test_register_application_with_default_services() | ||
{ | ||
$this->httpKernelProvider->register($this->application); | ||
|
||
$this->assertArrayHasKey('resolver', $this->application); | ||
$this->assertInstanceOf(ControllerResolver::class, $this->application['resolver']); | ||
$this->assertArrayHasKey('callback_resolver', $this->application); | ||
$this->assertInstanceOf(CallbackResolver::class, $this->application['callback_resolver']); | ||
} | ||
|
||
public function test_register_application_have_bridge_argument_resolver_service() | ||
{ | ||
$this->httpKernelProvider->register($this->application); | ||
|
||
if (Kernel::VERSION_ID >= 30100) { | ||
$this->assertArrayHasKey('argument_resolver', $this->application); | ||
$this->assertInstanceOf(ArgumentResolverInterface::class, $this->application['argument_resolver']); | ||
$this->assertInstanceOf(ArgumentResolver::class, $this->application['argument_resolver']); | ||
} | ||
} | ||
} |