Skip to content

Commit b34b6b9

Browse files
[FrameworkBundle] allow container/routing configurators to vary by env
1 parent a379d9d commit b34b6b9

25 files changed

+240
-22
lines changed

Annotation/Route.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Route
3434
private $schemes = [];
3535
private $condition;
3636
private $priority;
37+
private $env;
3738

3839
/**
3940
* @param array|string $data data array managed by the Doctrine Annotations library or the path
@@ -59,7 +60,8 @@ public function __construct(
5960
string $locale = null,
6061
string $format = null,
6162
bool $utf8 = null,
62-
bool $stateless = null
63+
bool $stateless = null,
64+
string $env = null
6365
) {
6466
if (\is_string($data)) {
6567
$data = ['path' => $data];
@@ -84,6 +86,7 @@ public function __construct(
8486
$data['format'] = $data['format'] ?? $format;
8587
$data['utf8'] = $data['utf8'] ?? $utf8;
8688
$data['stateless'] = $data['stateless'] ?? $stateless;
89+
$data['env'] = $data['env'] ?? $env;
8790

8891
$data = array_filter($data, static function ($value): bool {
8992
return null !== $value;
@@ -241,4 +244,14 @@ public function getPriority(): ?int
241244
{
242245
return $this->priority;
243246
}
247+
248+
public function setEnv(?string $env): void
249+
{
250+
$this->env = $env;
251+
}
252+
253+
public function getEnv(): ?string
254+
{
255+
return $this->env;
256+
}
244257
}

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
CHANGELOG
22
=========
33

4-
5.3.0
5-
-----
4+
5.3
5+
---
66

7-
* already encoded slashes are not decoded nor double-encoded anymore when generating URLs
7+
* Already encoded slashes are not decoded nor double-encoded anymore when generating URLs
8+
* Add support for per-env configuration in loaders
89

910
5.2.0
1011
-----

Loader/AnnotationClassLoader.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
abstract class AnnotationClassLoader implements LoaderInterface
7474
{
7575
protected $reader;
76+
protected $env;
7677

7778
/**
7879
* @var string
@@ -84,9 +85,10 @@ abstract class AnnotationClassLoader implements LoaderInterface
8485
*/
8586
protected $defaultRouteIndex = 0;
8687

87-
public function __construct(Reader $reader = null)
88+
public function __construct(Reader $reader = null, string $env = null)
8889
{
8990
$this->reader = $reader;
91+
$this->env = $env;
9092
}
9193

9294
/**
@@ -122,6 +124,10 @@ public function load($class, string $type = null)
122124
$collection = new RouteCollection();
123125
$collection->addResource(new FileResource($class->getFileName()));
124126

127+
if ($globals['env'] && $this->env !== $globals['env']) {
128+
return $collection;
129+
}
130+
125131
foreach ($class->getMethods() as $method) {
126132
$this->defaultRouteIndex = 0;
127133
foreach ($this->getAnnotations($method) as $annot) {
@@ -144,6 +150,10 @@ public function load($class, string $type = null)
144150
*/
145151
protected function addRoute(RouteCollection $collection, object $annot, array $globals, \ReflectionClass $class, \ReflectionMethod $method)
146152
{
153+
if ($annot->getEnv() && $annot->getEnv() !== $this->env) {
154+
return;
155+
}
156+
147157
$name = $annot->getName();
148158
if (null === $name) {
149159
$name = $this->getDefaultRouteName($class, $method);
@@ -317,6 +327,7 @@ protected function getGlobals(\ReflectionClass $class)
317327
}
318328

319329
$globals['priority'] = $annot->getPriority() ?? 0;
330+
$globals['env'] = $annot->getEnv();
320331

321332
foreach ($globals['requirements'] as $placeholder => $requirement) {
322333
if (\is_int($placeholder)) {
@@ -342,6 +353,7 @@ private function resetGlobals(): array
342353
'condition' => '',
343354
'name' => '',
344355
'priority' => 0,
356+
'env' => null,
345357
];
346358
}
347359

Loader/ClosureLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ClosureLoader extends Loader
3333
*/
3434
public function load($closure, string $type = null)
3535
{
36-
return $closure();
36+
return $closure($this->env);
3737
}
3838

3939
/**

Loader/Configurator/RoutingConfigurator.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ class RoutingConfigurator
2424
private $loader;
2525
private $path;
2626
private $file;
27+
private $env;
2728

28-
public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file)
29+
public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file, string $env = null)
2930
{
3031
$this->collection = $collection;
3132
$this->loader = $loader;
3233
$this->path = $path;
3334
$this->file = $file;
35+
$this->env = $env;
3436
}
3537

3638
/**
@@ -58,6 +60,21 @@ final public function collection(string $name = ''): CollectionConfigurator
5860
return new CollectionConfigurator($this->collection, $name);
5961
}
6062

63+
/**
64+
* @return static
65+
*/
66+
final public function when(string $env): self
67+
{
68+
if ($env === $this->env) {
69+
return clone $this;
70+
}
71+
72+
$clone = clone $this;
73+
$clone->collection = new RouteCollection();
74+
75+
return $clone;
76+
}
77+
6178
/**
6279
* @return static
6380
*/

Loader/ContainerLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ class ContainerLoader extends ObjectLoader
2222
{
2323
private $container;
2424

25-
public function __construct(ContainerInterface $container)
25+
public function __construct(ContainerInterface $container, string $env = null)
2626
{
2727
$this->container = $container;
28+
parent::__construct($env);
2829
}
2930

3031
/**

Loader/ObjectLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function load($resource, string $type = null)
5959
throw new \BadMethodCallException(sprintf('Method "%s" not found on "%s" when importing routing resource "%s".', $method, get_debug_type($loaderObject), $resource));
6060
}
6161

62-
$routeCollection = $loaderObject->$method($this);
62+
$routeCollection = $loaderObject->$method($this, $this->env);
6363

6464
if (!$routeCollection instanceof RouteCollection) {
6565
$type = get_debug_type($routeCollection);

Loader/PhpFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function callConfigurator(callable $result, string $path, string $file
7171
{
7272
$collection = new RouteCollection();
7373

74-
$result(new RoutingConfigurator($collection, $this, $path, $file));
74+
$result(new RoutingConfigurator($collection, $this, $path, $file, $this->env));
7575

7676
return $collection;
7777
}

Loader/XmlFileLoader.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ protected function parseNode(RouteCollection $collection, \DOMElement $node, str
8888
case 'import':
8989
$this->parseImport($collection, $node, $path, $file);
9090
break;
91+
case 'when':
92+
if (!$this->env || $node->getAttribute('env') !== $this->env) {
93+
break;
94+
}
95+
foreach ($node->childNodes as $node) {
96+
if ($node instanceof \DOMElement) {
97+
$this->parseNode($collection, $node, $path, $file);
98+
}
99+
}
100+
break;
91101
default:
92102
throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "route" or "import".', $node->localName, $path));
93103
}

Loader/YamlFileLoader.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ public function load($file, string $type = null)
8484
}
8585

8686
foreach ($parsedConfig as $name => $config) {
87+
if (0 === strpos($name, 'when@')) {
88+
if (!$this->env || 'when@'.$this->env !== $name) {
89+
continue;
90+
}
91+
92+
foreach ($config as $name => $config) {
93+
$this->validate($config, $name.'" when "@'.$this->env, $path);
94+
95+
if (isset($config['resource'])) {
96+
$this->parseImport($collection, $config, $path, $file);
97+
} else {
98+
$this->parseRoute($collection, $name, $config, $path);
99+
}
100+
}
101+
102+
continue;
103+
}
104+
87105
$this->validate($config, $name, $path);
88106

89107
if (isset($config['resource'])) {

0 commit comments

Comments
 (0)