Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Context / ContextFactory.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Context;
12
13 use Behat\Testwork\Argument\Validator;
14 use Behat\Behat\Context\Argument\ArgumentResolver;
15 use Behat\Behat\Context\Initializer\ContextInitializer;
16 use Behat\Testwork\Argument\ArgumentOrganiser;
17 use ReflectionClass;
18
19 /**
20  * Instantiates contexts using registered argument resolvers and context initializers.
21  *
22  * @author Konstantin Kudryashov <ever.zet@gmail.com>
23  */
24 final class ContextFactory
25 {
26     /**
27      * @var ArgumentOrganiser
28      */
29     private $argumentOrganiser;
30     /**
31      * @var ArgumentResolver[]
32      */
33     private $argumentResolvers = array();
34     /**
35      * @var ContextInitializer[]
36      */
37     private $contextInitializers = array();
38     /**
39      * @var Validator
40      */
41     private $validator;
42
43     /**
44      * Initialises factory.
45      *
46      * @param ArgumentOrganiser $argumentOrganiser
47      */
48     public function __construct(ArgumentOrganiser $argumentOrganiser)
49     {
50         $this->argumentOrganiser = $argumentOrganiser;
51         $this->validator = new Validator();
52     }
53
54     /**
55      * Registers context argument resolver.
56      *
57      * @param ArgumentResolver $resolver
58      */
59     public function registerArgumentResolver(ArgumentResolver $resolver)
60     {
61         $this->argumentResolvers[] = $resolver;
62     }
63
64     /**
65      * Registers context initializer.
66      *
67      * @param ContextInitializer $initializer
68      */
69     public function registerContextInitializer(ContextInitializer $initializer)
70     {
71         $this->contextInitializers[] = $initializer;
72     }
73
74     /**
75      * Creates and initializes context class.
76      *
77      * @param string             $class
78      * @param array              $arguments
79      * @param ArgumentResolver[] $singleUseResolvers
80      *
81      * @return Context
82      */
83     public function createContext($class, array $arguments = array(), array $singleUseResolvers = array())
84     {
85         $reflection = new ReflectionClass($class);
86         $resolvers = array_merge($singleUseResolvers, $this->argumentResolvers);
87         $resolvedArguments = $this->resolveArguments($reflection, $arguments, $resolvers);
88         $context = $this->createInstance($reflection, $resolvedArguments);
89         $this->initializeInstance($context);
90
91         return $context;
92     }
93
94     /**
95      * Resolves arguments for a specific class using registered argument resolvers.
96      *
97      * @param ReflectionClass    $reflection
98      * @param array              $arguments
99      * @param ArgumentResolver[] $resolvers
100      *
101      * @return mixed[]
102      */
103     private function resolveArguments(ReflectionClass $reflection, array $arguments, array $resolvers)
104     {
105         $newArguments = $arguments;
106
107         foreach ($resolvers as $resolver) {
108             $newArguments = $resolver->resolveArguments($reflection, $newArguments);
109         }
110
111         if (!$reflection->hasMethod('__construct')) {
112             return $newArguments;
113         }
114
115         $constructor = $reflection->getConstructor();
116         $newArguments = $this->argumentOrganiser->organiseArguments($constructor, $newArguments);
117         $this->validator->validateArguments($constructor, $newArguments);
118
119         return $newArguments;
120     }
121
122     /**
123      * Creates context instance.
124      *
125      * @param ReflectionClass $reflection
126      * @param array           $arguments
127      *
128      * @return mixed
129      */
130     private function createInstance(ReflectionClass $reflection, array $arguments)
131     {
132         if (count($arguments)) {
133             return $reflection->newInstanceArgs($arguments);
134         }
135
136         return $reflection->newInstance();
137     }
138
139     /**
140      * Initializes context class and returns new context instance.
141      *
142      * @param Context $context
143      */
144     private function initializeInstance(Context $context)
145     {
146         foreach ($this->contextInitializers as $initializer) {
147             $initializer->initializeContext($context);
148         }
149     }
150 }