8000 [Validator] Changed context manager to context factory · symfony/symfony@df41974 · GitHub
[go: up one dir, main page]

Skip to content

Commit df41974

Browse files
committed
[Validator] Changed context manager to context factory
The current context is not stored anymore. Instead, it is passed around the traverser and the visitors. For this reason, validation can occur in multiple contexts at the same time.
1 parent 26eafa4 commit df41974

27 files changed

+667
-904
lines changed

src/Symfony/Component/Validator/Context/ExecutionContext.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
2626

2727
/**
28-
* The context used and created by {@link ExecutionContextManager}.
28+
* The context used and created by {@link ExecutionContextFactory}.
2929
*
3030
* @since 2.5
3131
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -34,13 +34,33 @@
3434
*/
3535
class ExecutionContext implements ExecutionContextInterface, LegacyExecutionContextInterface
3636
{
37+
/**
38+
* @var ValidatorInterface
39+
*/
40+
private $validator;
41+
3742
/**
3843
* The root value of the validated object graph.
3944
*
4045
* @var mixed
4146
*/
4247
private $root;
4348

49+
/**
50+
* @var GroupManagerInterface
51+
*/
52+
private $groupManager;
53+
54+
/**
55+
* @var TranslatorInterface
56+
*/
57+
private $translator;
58+
59+
/**
60+
* @var string
61+
*/
62+
private $translationDomain;
63+
4464
/**
4565
* The violations generated in the current context.
4666
*
@@ -62,32 +82,12 @@ class ExecutionContext implements ExecutionContextInterface, LegacyExecutionCont
6282
*/
6383
private $nodeStack;
6484

65-
/**
66-
* @var ValidatorInterface
67-
*/
68-
private $validator;
69-
70-
/**
71-
* @var GroupManagerInterface
72-
*/
73-
private $groupManager;
74-
75-
/**
76-
* @var TranslatorInterface
77-
*/
78-
private $translator;
79-
80-
/**
81-
* @var string
82-
*/
83-
private $translationDomain;
84-
8585
/**
8686
* Creates a new execution context.
8787
*
88+
* @param ValidatorInterface $validator The validator
8889
* @param mixed $root The root value of the
8990
* validated object graph
90-
* @param ValidatorInterface $validator The validator
9191
* @param GroupManagerInterface $groupManager The manager for accessing
9292
* the currently validated
9393
* group
@@ -96,13 +96,13 @@ class ExecutionContext implements ExecutionContextInterface, LegacyExecutionCont
9696
* use for translating
9797
* violation messages
9898
*
99-
* @internal Called by {@link ExecutionContextManager}. Should not be used
99+
* @internal Called by {@link ExecutionContextFactory}. Should not be used
100100
* in user code.
101101
*/
102-
public function __construct($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null)
102+
public function __construct(ValidatorInterface $validator, $root, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null)
103103
{
104-
$this->root = $root;
105104
$this->validator = $validator;
105+
$this->root = $root;
106106
$this->groupManager = $groupManager;
107107
$this->translator = $translator;
108108
$this->translationDomain = $translationDomain;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Context;
13+
14+
use Symfony\Component\Translation\TranslatorInterface;
15+
use Symfony\Component\Validator\Group\GroupManagerInterface;
16+
use Symfony\Component\Validator\Validator\ValidatorInterface;
17+
18+
/**
19+
* Creates new {@link ExecutionContext} instances.
20+
*
21+
* @since 2.5
22+
* @author Bernhard Schussek <bschussek@gmail.com>
23+
*/
24+
class ExecutionContextFactory implements ExecutionContextFactoryInterface
25+
{
26+
/**
27+
* @var GroupManagerInterface
28+
*/
29+
private $groupManager;
30+
31+
/**
32+
* @var TranslatorInterface
33+
*/
34+
private $translator;
35+
36+
/**
37+
* @var string|null
38+
*/
39+
private $translationDomain;
40+
41+
/**
42+
* Creates a new context factory.
43+
*
44+
* @param GroupManagerInterface $groupManager The manager for accessing
45+
* the currently validated
46+
* group
47+
* @param TranslatorInterface $translator The translator
48+
* @param string|null $translationDomain The translation domain to
49+
* use for translating
50+
* violation messages
51+
*/
52+
public function __construct(GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null)
53+
{
54+
$this->groupManager = $groupManager;
55+
$this->translator = $translator;
56+
$this->translationDomain = $translationDomain;
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function createContext(ValidatorInterface $validator, $root)
63+
{
64+
return new ExecutionContext(
65+
$validator,
66+
$root,
67+
$this->groupManager,
68+
$this->translator,
69+
$this->translationDomain
70+
);
71+
}
72+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Context;
13+
14+
use Symfony\Component\Validator\Validator\ValidatorInterface;
15+
16+
/**
17+
* Creates instances of {@link ExecutionContextInterface}.
18+
*
19+
* You can use a custom factory if you want to customize the execution context
20+
* that is passed through the validation run.
21+
*
22+
* @since 2.5
23+
* @author Bernhard Schussek <bschussek@gmail.com>
24+
*/
25+
interface ExecutionContextFactoryInterface
26+
{
27+
/**
28+
* Creates a new execution context.
29+
*
30+
* @param ValidatorInterface $validator The validator
31+
* @param mixed $root The root value of the validated
32+
* object graph
33+
*
34+
* @return ExecutionContextInterface The new execution context
35+
*/
36+
public function createContext(ValidatorInterface $validator, $root);
37+
}

0 commit comments

Comments
 (0)
0