Skip to content

Commit

Permalink
Endpoint-wide context (#5)
Browse files Browse the repository at this point in the history
* HTTP Routing readme
* Context inheritance configuration
* Explicitly require yaml as a DI loading format
  • Loading branch information
scaytrase authored Jun 29, 2016
1 parent f72a4ba commit 188ca1f
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 12 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ env:
- SYMFONY_VERSION='2.7.*' deps='no'
- SYMFONY_VERSION='2.8.*' deps='no'
- SYMFONY_VERSION='3.0.*' deps='no'
- SYMFONY_VERSION='~3.0@dev' deps='no'
- SYMFONY_VERSION='3.1.*' deps='no'
- SYMFONY_VERSION='~3.2@dev' deps='no'

matrix:
include:
Expand All @@ -32,4 +33,4 @@ install:

script:
- mkdir -p build
- phpunit --colors -c phpunit.xml --coverage-text
- vendor/bin/phpunit --colors -c phpunit.xml
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ endpoint for several methods
Endpoint is a HTTP route which process basic HTTP request, providing initial parsing
and processing request data

To enable HTTP endpoint you should enable custom endpoint router loader via the following router configuration:

```yaml
# app/config/routing.yml
rpc:
resource: .
type: endpoint

```

Resource value is not important, it is ignored when loading routes as the endpoints are
configured vua config

### Configuration

Basic endpoint configuration looks like
Expand All @@ -41,6 +54,7 @@ rpc:
defaults:
_controller: JsonRpcBundle:JsonRpc:jsonRpc
_format: json
context: Default
resources:
- "@MyBundle/Resources/config/service_rpc.yml"
```
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"symfony/console": "~2.7 || ~3.0",
"symfony/config": "~2.7 || ~3.0",
"symfony/http-kernel": "~2.7 || ~3.0",
"symfony/yaml": "~2.7 || ~3.0",
"symfony/dependency-injection": "~2.7 || ~3.0",
"doctrine/annotations": "~1.2",
"scaytrase/rpc-common": "~1.0"
Expand Down
14 changes: 14 additions & 0 deletions src/Bankiru/Api/Rpc/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ function ($v) {
$proto->append(
(new VariableNodeDefinition('defaults'))
);
$proto->append(
(new VariableNodeDefinition('context'))
->beforeNormalization()
->ifString()
->then(
function ($v) {
return [$v];
}
)
->end()
->defaultValue(['Default'])
->info('Endpoint-wide context')
->example(['Default'])
);

$endpoints->useAttributeAsKey('name');
$proto->addDefaultsIfNotSet();
Expand Down
1 change: 1 addition & 0 deletions src/Bankiru/Api/Rpc/DependencyInjection/RpcExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private function configureRouter(array $router, ContainerBuilder $container)
$container->getDefinition('rpc.router.resolver'),
$config['resources'],
$collection,
$config['context']
]
);

Expand Down
15 changes: 15 additions & 0 deletions src/Bankiru/Api/Rpc/Routing/Annotation/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Method
public $method;
public $context = [];
public $defaultContext = true;
public $inherit = true;

public function __construct(array $values)
{
Expand Down Expand Up @@ -111,5 +112,19 @@ public function setDefaultContext($defaultContext)
$this->defaultContext = $defaultContext;
}

/**
* @return boolean
*/
public function isInherit()
{
return $this->inherit;
}

/**
* @param boolean $inherit
*/
public function setInherit($inherit)
{
$this->inherit = (bool)$inherit;
}
}
3 changes: 2 additions & 1 deletion src/Bankiru/Api/Rpc/Routing/Loader/AnnotationClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ protected function addRoute(
$annot->getMethod(),
$class->getName() . '::' . $method->getName(),
array_merge($parents['context'], $annot->getContext()),
$parents['default_context'] && $annot->isDefaultContext()
$parents['default_context'] && $annot->isDefaultContext(),
$annot->isInherit()
)
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Bankiru/Api/Rpc/Routing/Loader/YamlLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class YamlLoader extends FileLoader
'controller',
'context',
'default_context',
'inherit',
];

/** @var Parser */
Expand Down Expand Up @@ -176,11 +177,13 @@ protected function parseRoute(MethodCollection $collection, $name, array $config
{
$context = array_key_exists('context', $config) ? (array)$config['context'] : [];
$method = array_key_exists('method', $config) ? $config['method'] : $name;
$inherit = array_key_exists('inherit', $config) ? $config['inherit'] : true;
$route = new Route(
$method,
$config['controller'],
$context,
array_key_exists('default_context', $config) ? $config['default_context'] : true
array_key_exists('default_context', $config) ? $config['default_context'] : true,
$inherit
);

$collection->add($name, $route);
Expand Down
19 changes: 15 additions & 4 deletions src/Bankiru/Api/Rpc/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Route
private $context;
/** @var bool */
private $defaultContext;
/** @var bool */
private $inheritContext;

/**
* Route constructor.
Expand All @@ -26,13 +28,21 @@ class Route
* @param string $controller
* @param array $context
* @param bool $defaultContext
* @param bool $inheritContext
*/
public function __construct($method, $controller, array $context, $defaultContext = true)
public function __construct(
$method,
$controller,
array $context,
$defaultContext = true,
$inheritContext = true
)
{
$this->method = $method;
$this->controller = $controller;
$this->context = $context;
$this->defaultContext = (bool)$defaultContext;
$this->inheritContext = (bool)$inheritContext;
}

/**
Expand Down Expand Up @@ -64,7 +74,7 @@ public function setMethod($method)
*/
public function getContext()
{
return array_unique($this->context);
return array_unique(array_merge($this->context, $this->includeDefaultContext() ? ['Default'] : []));
}

/**
Expand All @@ -80,7 +90,7 @@ public function setContext($context)
*/
public function includeDefaultContext()
{
return $this->defaultContext;
return $this->defaultContext || in_array('Default', $this->context, true);
}

/**
Expand All @@ -104,8 +114,9 @@ public function addContext($context)
$this->context[] = $context;
}

/** @return bool */
public function inheritContext()
{
return true;
return $this->inheritContext;
}
}
10 changes: 8 additions & 2 deletions src/Bankiru/Api/Rpc/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ class Router
* @param array $resources
* @param MethodCollection|null $collection
*
* @throws \RuntimeException
* @param array $context
*/
public function __construct(
LoaderResolverInterface $resolver,
array $resources = [],
MethodCollection $collection = null)
MethodCollection $collection = null,
array $context = []
)
{
$this->collection = $collection;

Expand All @@ -40,6 +42,10 @@ public function __construct(
}
$this->collection->addCollection($loader->load($resource));
}

foreach ($context as $item) {
$this->collection->addContext($item);
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Bankiru/Api/Rpc/Tests/Fixtures/Rpc/RpcImplController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Bankiru\Api\Rpc\Tests\Fixtures\Rpc;

use Bankiru\Api\Rpc\Impl\Response;
use Bankiru\Api\Rpc\Routing\Annotation\Method;
use ScayTrase\Api\Rpc\RpcRequestInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand All @@ -21,6 +22,8 @@ class RpcImplController extends Controller
* @param array $array
* @param RpcRequestInterface $request
*
* @Method("annotation", inherit=false, context={"annotation-non-inherit"}, defaultContext=false)
*
* @return JsonResponse
*/
public function testAction($noDefault, $default = 'test', array $array, RpcRequestInterface $request)
Expand Down
2 changes: 1 addition & 1 deletion src/Bankiru/Api/Rpc/Tests/Fixtures/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rpc:
resource: /
resource: .
type: endpoint
11 changes: 11 additions & 0 deletions src/Bankiru/Api/Rpc/Tests/Fixtures/config/rpc_routing.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
test_method:
method: test/method
controller: Bankiru\Api\Rpc\Tests\Fixtures\Rpc\RpcImplController::testAction
context: ['internal']

test_inheritance:
method: test/non-inherited
controller: Bankiru\Api\Rpc\Tests\Fixtures\Rpc\RpcImplController::testAction
context: ['own']
inherit: false

test/method2:
controller: Bankiru\Api\Rpc\Tests\Fixtures\Rpc\RpcImplController::testAction

annotation:
resource: Bankiru\Api\Rpc\Tests\Fixtures\Rpc\RpcImplController
type: annotation
context: annotation
8 changes: 8 additions & 0 deletions src/Bankiru/Api/Rpc/Tests/Fixtures/config/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ rpc:
endpoints:
test:
path: /test/
context: [Default, test]
defaults:
_controller: Bankiru\Api\Rpc\Tests\Fixtures\Rpc\TestController::rpcAction
_format: json
resources:
- '%kernel.root_dir%/config/rpc_routing.yml'

wScalarContext:
path: /test/
context: test
defaults:
_controller: Bankiru\Api\Rpc\Tests\Fixtures\Rpc\TestController::rpcAction
_format: json
resources: []
24 changes: 23 additions & 1 deletion src/Bankiru/Api/Rpc/Tests/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,29 @@ public function testRpcRouterCollection()
self::assertInstanceOf(MethodCollection::class, $collection);

self::assertTrue($collection->has('test_method'));
self::assertSame('test/method', $collection->get('test_method')->getMethod());
$route = $collection->get('test_method');
self::assertSame('test/method', $route->getMethod());

self::assertContains('Default', $route->getContext());
self::assertContains('test', $route->getContext());
self::assertTrue($route->includeDefaultContext());

$route = $collection->get('test_inheritance');
self::assertSame('test/non-inherited', $route->getMethod());

self::assertCount(2, $route->getContext());
self::assertContains('Default', $route->getContext());
self::assertNotContains('test', $route->getContext());
self::assertContains('own', $route->getContext());
self::assertTrue($route->includeDefaultContext());

$route = $collection->get('annotation');
self::assertSame('annotation', $route->getMethod());

self::assertNotContains('Default', $route->getContext());
self::assertFalse($route->includeDefaultContext());
self::assertContains('annotation-non-inherit', $route->getContext());
self::assertCount(1, $route->getContext());
}

protected static function getKernelClass()
Expand Down

0 comments on commit 188ca1f

Please sign in to comment.