Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Merge 6afadd6 into 063fd90
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgarlag committed Dec 4, 2020
2 parents 063fd90 + 6afadd6 commit f3ff370
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
language: php

env:
global:
- XDEBUG_MODE=coverage

jobs:
fast_finish: true
include:
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
## [Unreleased]
### Added
- \#80 Support for multiple features in route definition @ajgarlag

### Changed
- \#82 Restore support for Symfony 4.4 LTS @ajgarlag

## [4.0.2]
### Changed
- Enabled support for twig v3

## [4.0.1]
### Fix
- Fix subscriber for invokable controllers @vitsadecky

## [4.0.0]
### Changed
- \#67 Add compatibility for Symfony 5 @migo315
Expand All @@ -10,6 +21,14 @@
- \#64 Add Travis tests for Symony 5 @migo315
- \#59 Update PHP requirements @migo315

## [3.6.0]
### Added
- Support for multiple features in route definition @ajgarlag

## [3.5.1]
### Fix
- Fix subscriber for invokable controllers @vitsadecky

## [3.5.0]
### Changed
- \#69 Support for twig 3 @migo315
Expand Down
83 changes: 82 additions & 1 deletion docs/route.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,88 @@ or via xml
<controller>AppBundle:Blog:list</controller>
<default key="_feature">feature_123</default>
</route>


<!-- ... -->
</routes>
```

To check for multiple features, pass them as an array to the route definition.

```php
// src/AppBundle/Controller/BlogController.php
// src/Controller/BlogController.php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class BlogController extends Controller
{
/**
* @Route("/blog/{page}", defaults={"_feature": {"feature_123", "feature_456"}})
*/
public function listAction($page)
{
// ...
}

/**
* @Route("/blog/{slug}")
*/
public function showAction($slug)
{
// ...
}
}
```
or via yml

```yml
# app/config/routing.yml
blog_list:
path: /blog/{page}
defaults: { _controller: AppBundle:Blog:list, _feature: ['feature_456', 'feature_789'] }

# Symfony 3.4 / 4.0
blog_list:
path: /blog/{page}
controller: AppBundle:Blog:list
defaults: { _feature: ['feature_456', 'feature_789'] }

blog_show:
```

or via xml

```xml
<!-- app/config/routing.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="blog_list" path="/blog/{page}">
<default key="_controller">AppBundle:Blog:list</default>
<default key="_feature">
<list>
<string>feature_123</string>
<string>feature_456</string>
</list>
</default>
</route>

<!-- Symfony 3.4 / 4.0 -->
<route id="blog_list" path="/blog/{page}">
<controller>AppBundle:Blog:list</controller>
<default key="_feature">
<list>
<string>feature_123</string>
<string>feature_456</string>
</list>
</default>
</route>

<!-- ... -->
</routes>
```
8 changes: 5 additions & 3 deletions src/Listener/RoutingMetadataSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ public function onKernelController(ControllerEvent $event)
return;
}

$featureName = $event->getRequest()->attributes->get(static::FEATURE_KEY);
if (!$this->manager->isActive($featureName)) {
throw new NotFoundHttpException('Feature for this class is not active.');
$featureNames = (array) $event->getRequest()->attributes->get(static::FEATURE_KEY);
foreach ($featureNames as $featureName) {
if (!$this->manager->isActive($featureName)) {
throw new NotFoundHttpException('Feature for this class is not active.');
}
}
}

Expand Down
61 changes: 61 additions & 0 deletions tests/Listener/RoutingMetadataSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -123,4 +124,64 @@ private function createControllerEvent($request): ControllerEvent
HttpKernelInterface::MASTER_REQUEST
);
}

/**
* Test features are not active
*
* @return void
*/
public function testRouteWithMultipleFeaturesIsNotActive()
{
$this->expectException(NotFoundHttpException::class);

$request = new Request([], [], ['_feature' => ['feature_abc', 'feature_def']]);

$event = new ControllerEvent(
$this->createMock(HttpKernelInterface::class),
function () {
return new Response();
},
$request,
HttpKernelInterface::MASTER_REQUEST
);

$manager = $this->createMock(FeatureManagerInterface::class);
$manager
->expects(static::exactly(2))
->method('isActive')
->withConsecutive(['feature_abc'], ['feature_def'])
->willReturnOnConsecutiveCalls(true, false);

$subscriber = new RoutingMetadataSubscriber($manager);
$subscriber->onKernelController($event);
}

/**
* Test features are active
*
* @return void
*/
public function testRouteWithMultipleFeaturesIsActive()
{
$request = new Request([], [], ['_feature' => ['feature_abc', 'feature_def']]);

$event = new ControllerEvent(
$this->createMock(HttpKernelInterface::class),
function () {
return new Response();
},
$request,
HttpKernelInterface::MASTER_REQUEST
);

$manager = $this->createMock(FeatureManagerInterface::class);
$manager
->expects(static::exactly(2))
->method('isActive')
->withConsecutive(['feature_abc'], ['feature_def'])
->willReturnOnConsecutiveCalls(true, true);

$subscriber = new RoutingMetadataSubscriber($manager);
$subscriber->onKernelController($event);
}
}

0 comments on commit f3ff370

Please sign in to comment.