Skip to content

Commit

Permalink
feat(upgrade): use multiple routes
Browse files Browse the repository at this point in the history
  • Loading branch information
raf262 committed Aug 4, 2022
1 parent 194de71 commit 15bd958
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 81 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,30 @@ m6_web_log_bridge:
- all_error
filters:
get_article_error:
route: get_article
routes: ['get_article']
method: ['GET']
status: [422, 500]
level: 'error'
options:
response_body: true # from add Response body content (with DefaultFormatter)
post_article_all:
route: post_article
routes: ['post_article']
method: ~ # from all methods
status: ~ # from all status
get_article_not_found:
route: get_article
routes: ['get_article']
method: ['GET']
status: [404]
level: 'warning'
edit_category:
route: get_category
routes: ['get_category']
method: ['POST', 'PUT']
status: [400-422, ^510, !530-550]
level: 'error'
options:
post_parameters: true # From add post parameters in response content (with DefaultFormatter)
all_error: # All route, all method in error
route: ~
routes: ~
method: ~
status: [31*, 4*, 5*]
level: 'critical'
Expand Down
10 changes: 5 additions & 5 deletions src/M6Web/Bundle/LogBridgeBundle/Config/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class Filter
{
private ?string $route = null;
private ?array $routes = [];

/** @var string[]|null */
private ?array $method = null;
Expand All @@ -31,16 +31,16 @@ public function getName(): string
return $this->name;
}

public function setRoute(?string $route): self
public function setRoutes(?array $routes): self
{
$this->route = $route;
$this->routes = $routes;

return $this;
}

public function getRoute(): ?string
public function getRoutes(): ?array
{
return $this->route;
return $this->routes;
}

/**
Expand Down
48 changes: 40 additions & 8 deletions src/M6Web/Bundle/LogBridgeBundle/Config/FilterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace M6Web\Bundle\LogBridgeBundle\Config;

use Psr\Log\LogLevel;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;

/**
Expand Down Expand Up @@ -47,9 +48,14 @@ protected function isRoute(string $name): bool
return $this->router?->getRouteCollection()->get($name) !== null;
}

protected function getAllRoutes(): array
{
return $this->router?->getRouteCollection()->all();
}

/**
* @param array{
* route?: string,
* routes?: string[],
* method?: string[],
* status?: int[],
* level?: string,
Expand All @@ -59,11 +65,11 @@ protected function isRoute(string $name): bool
public function parse(string $name, array $config): Filter
{
if (
!array_key_exists('route', $config) ||
!array_key_exists('routes', $config) ||
!array_key_exists('method', $config) ||
!array_key_exists('status', $config)
) {
throw new ParseException(sprintf('Undefined "route", "method" or "status" parameter from filter "%s"', $name));
throw new ParseException(sprintf('Undefined "routes", "method" or "status" parameter from filter "%s"', $name));
}

if (!array_key_exists('level', $config)) {
Expand All @@ -74,19 +80,45 @@ public function parse(string $name, array $config): Filter
->setMethod($config['method'])
->setStatus($config['status']);

$this->parseRoute($filter, $config['route']);
$this->parseRoutes($filter, $config['routes'] ?? []);
$this->parseLevel($filter, $config['level']);

return $filter->setOptions($config['options'] ?? []);
}

protected function parseRoute(Filter $filter, ?string $route): void
protected function parseRoutes(Filter $filter, ?array $routes): void
{
if ($route !== null && !$this->isRoute($route)) {
throw new ParseException(sprintf('Undefined route "%s" from router service', $route));
if (empty($routes)) {
$filter->setRoutes(['all']);
return;
}

// Find and keep excluded routes
$excludedRoutes = array_filter($routes, function (string $route) {
return str_contains($route, '!');
});

// Create an array with routes not excluded
$routes = array_diff_key($routes, $excludedRoutes);

// Check that the route's name exist
foreach ($routes as $route) {
if (!$this->isRoute($route)) {
throw new ParseException(sprintf('Undefined route "%s" from router service', $route));
}
}

// If empty routes, return all routes except the excluded ones
if (empty($routes)) {
$existingRoutes = $this->getAllRoutes();
$excludedRoutes = array_map(function (string $route) {
return ltrim($route, '!');
}, $excludedRoutes);

$routes = array_diff($existingRoutes, $excludedRoutes);
}

$filter->setRoute($route);
$filter->setRoutes($routes);
}

protected function parseLevel(Filter $filter, ?string $level): void
Expand Down
4 changes: 2 additions & 2 deletions src/M6Web/Bundle/LogBridgeBundle/Config/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(private RouterInterface $router)

/**
* @param array<string, array{
* route?: string,
* routes?: string[],
* method?: string[],
* status?: int[],
* level?: string,
Expand All @@ -42,7 +42,7 @@ protected function createFilterCollection(array $filters): FilterCollection
*
* @param array{
* filters?: array<string, array{
* route?: string,
* routes?: string[],
* method?: string[],
* status?: int[],
* level?: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('route')->defaultNull()->end()
->arrayNode('routes')
->prototype('scalar')->end()
->end()
->arrayNode('method')
->prototype('scalar')->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface BuilderInterface
{
/**
* @param array<string, array{
* route?: string,
* routes?: string[],
* method?: string[],
* status?: int[],
* level?: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,20 @@ private function compileFilter(Filter $filter): array
{
$compiledKeys = [];
$compiled = [];
/** @var string $prefix */
$prefix = is_null($filter->getRoute()) ? 'all' : $filter->getRoute();
/** @var array $routesPrefix */
$routesPrefix = $filter->getRoutes();

if (empty($filter->getMethod())) {
$prefix = sprintf('%s.all', $prefix);
$compiledKeys = $this->compileFilterStatus($prefix, $filter);
foreach ($routesPrefix as $routePrefix) {
$prefix = sprintf('%s.all', $routePrefix);
$compiledKeys = array_merge($compiledKeys, $this->compileFilterStatus($prefix, $filter));
}
} else {
foreach ($filter->getMethod() as $method) {
$methodPrefix = sprintf('%s.%s', $prefix, $method);
$compiledKeys = array_merge($compiledKeys, $this->compileFilterStatus($methodPrefix, $filter));
foreach ($routesPrefix as $routePrefix) {
$methodPrefix = sprintf('%s.%s', $routePrefix, $method);
$compiledKeys = array_merge($compiledKeys, $this->compileFilterStatus($methodPrefix, $filter));
}
}
}

Expand All @@ -282,7 +286,7 @@ private function compileFilter(Filter $filter): array
/**
* @return string[]
*/
private function compileFilterStatus(string $prefix, Filter $filter): array
private function compileFilterStatus(?string $prefix, Filter $filter): array
{
$compiled = [];
/** @var string[]|null $filterStatusList */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,44 @@ active_filters:

filters:
get_clip_all_status:
route: get_clip
routes: ['get_clip']
method: ['GET', 'PUT']
status: ~
options:
response_body: true
post_parameters: true

put_clip_form_all_200:
route: put_clips_form
routes: ['put_clips_form']
method: ['PUT']
status: [!^240, 2*]

delete_clip_500_all_hundred_without_580_to_590:
route: delete_clip
routes: ['delete_clip']
method: ['DELETE']
status: [5*, !58*]

edit_clip_5*_30*_without_550_549:
route: edit_clip
routes: ['edit_clip']
method: ['PATH']
status: [5*, !550, !549, 30*]

edit_clip_404_to_410:
route: edit_clip
routes: ['edit_clip']
method: ['POST']
status: [404-410]

delete_clip_450_more_without_452_to_458:
route: delete_clip
routes: ['delete_clip']
method: ['DELETE']
status: [^450, !452-458]

get_program_200_hundred_without_290_more:
route: get_program
routes: ['get_program']
method: ['GET']
status: [2*, !^290]
status: [2*, !^290]

get_program_200_hundred_without_route_no_filter:
routes: [ 'get_program', '!no_filter' ]
method: [ 'GET' ]
status: [ 2*, !^290 ]
1 change: 1 addition & 0 deletions src/M6Web/Bundle/LogBridgeBundle/Tests/Units/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ protected function getMockedRouterCollection(): object
{
$collection = new \mock\Symfony\Component\Routing\RouteCollection();
$collection->getMockController()->get = fn($name) => $name != 'invalid_route' ? new Route('/path') : null;
$collection->getMockController()->all = ['fake_url', 'fake_second_url', 'excluded_route'];

return $collection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ private function getFilters(): Config\FilterCollection

$filter = new Config\Filter('active_filters_one');
$filter
->setRoute('route_name')
->setRoutes(['route_name'])
->setMethod(['GET', 'POST'])
->setStatus(['all']);

$collection->add($filter);

$filter = new Config\Filter('active_filters_two');
$filter
->setRoute('route_name_two')
->setRoutes(['route_name_two'])
->setMethod(['PUT', 'POST'])
->setStatus([200]);

$collection->add($filter);

$filter = new Config\Filter('active_filters_three');
$filter
->setRoute('route_name_three')
->setRoutes(['route_name_three'])
->setMethod(['all'])
->setStatus([422, 404, 500]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public function testFilter(): void
->then
->string($filter->getName())
->isEqualTo('filter_name')
->object($filter->setRoute('filter_route'))
->object($filter->setRoutes(['filter_route']))
->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Filter::class)
->string($filter->getRoute())
->isEqualTo('filter_route')
->array($filter->getRoutes())
->isEqualTo(['filter_route'])
->object($filter->setMethod(null))
->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Config\Filter::class)
->variable($filter->getMethod())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@

namespace M6Web\Bundle\LogBridgeBundle\Tests\Units\Config;

use atoum;
use atoum;
use M6Web\Bundle\LogBridgeBundle\Config;

class FilterCollection extends atoum
{
private function createFilter(string $name, string $route, ?array $method, ?array $status): Config\Filter
private function createFilter(string $name, array $routes, ?array $method, ?array $status): Config\Filter
{
$filter = new Config\Filter($name);
$filter
->setRoute($route)
->setRoutes($routes)
->setMethod($method)
->setStatus($status);

return $filter;
return $filter;
}

public function testCollection(): void
{
$filters = [];
$filters[] = $this->createFilter('filter_un', 'get_clip', ['all'], ['all']);
$filters[] = $this->createFilter('filter_deux', 'put_clip', ['PUT'], ['all']);
$filters[] = $this->createFilter('filter_trois', 'put_clip', ['PUT'], [400, 404, 422, 500]);
$filters[] = $this->createFilter('filter_un', ['get_clip'], ['all'], ['all']);
$filters[] = $this->createFilter('filter_deux', ['put_clip'], ['PUT'], ['all']);
$filters[] = $this->createFilter('filter_trois', ['put_clip'], ['PUT'], [400, 404, 422, 500]);

$filterQuatre =$this->createFilter('filter_quatre', 'post_clip', ['POST'], [400, 404, 422, 500]);
$filterQuatre =$this->createFilter('filter_quatre', ['post_clip'], ['POST'], [400, 404, 422, 500]);

$this
->if($collection = new Config\FilterCollection($filters))
Expand Down
Loading

0 comments on commit 15bd958

Please sign in to comment.