Security update for permissions_by_term
[yaffs-website] / vendor / behat / mink-extension / src / Behat / MinkExtension / ServiceContainer / Driver / GoutteFactory.php
1 <?php
2
3 /*
4  * This file is part of the Behat MinkExtension.
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\MinkExtension\ServiceContainer\Driver;
12
13 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
14 use Symfony\Component\DependencyInjection\Definition;
15
16 /**
17  * @author Christophe Coevoet <stof@notk.org>
18  */
19 class GoutteFactory implements DriverFactory
20 {
21     /**
22      * {@inheritdoc}
23      */
24     public function getDriverName()
25     {
26         return 'goutte';
27     }
28
29     /**
30      * {@inheritdoc}
31      */
32     public function supportsJavascript()
33     {
34         return false;
35     }
36
37     /**
38      * {@inheritdoc}
39      */
40     public function configure(ArrayNodeDefinition $builder)
41     {
42         $builder
43             ->children()
44                 ->arrayNode('server_parameters')
45                     ->useAttributeAsKey('key')
46                     ->prototype('variable')->end()
47                 ->end()
48                 ->arrayNode('guzzle_parameters')
49                     ->useAttributeAsKey('key')
50                     ->prototype('variable')->end()
51                     ->info(
52                         "For Goutte 1.x, these are the second argument of the Guzzle3 client constructor.\n".
53                         'For Goutte 2.x, these are the elements passed in the "defaults" key of the Guzzle4 config.'
54                     )
55                 ->end()
56             ->end()
57         ;
58     }
59
60     /**
61      * {@inheritdoc}
62      */
63     public function buildDriver(array $config)
64     {
65         if (!class_exists('Behat\Mink\Driver\GoutteDriver')) {
66             throw new \RuntimeException(
67                 'Install MinkGoutteDriver in order to use goutte driver.'
68             );
69         }
70
71         if ($this->isGoutte1()) {
72             $guzzleClient = $this->buildGuzzle3Client($config['guzzle_parameters']);
73         } else {
74             $guzzleClient = $this->buildGuzzle4Client($config['guzzle_parameters']);
75         }
76
77         $clientDefinition = new Definition('Behat\Mink\Driver\Goutte\Client', array(
78             $config['server_parameters'],
79         ));
80         $clientDefinition->addMethodCall('setClient', array($guzzleClient));
81
82         return new Definition('Behat\Mink\Driver\GoutteDriver', array(
83             $clientDefinition,
84         ));
85     }
86
87     private function buildGuzzle4Client(array $parameters)
88     {
89         // Force the parameters set by default in Goutte to reproduce its behavior
90         $parameters['allow_redirects'] = false;
91         $parameters['cookies'] = true;
92
93         return new Definition('GuzzleHttp\Client', array($parameters));
94
95     }
96
97     private function buildGuzzle3Client(array $parameters)
98     {
99         // Force the parameters set by default in Goutte to reproduce its behavior
100         $parameters['redirect.disable'] = true;
101
102         return new Definition('Guzzle\Http\Client', array(null, $parameters));
103     }
104
105     private function isGoutte1()
106     {
107         $refl = new \ReflectionParameter(array('Goutte\Client', 'setClient'), 0);
108
109         if ($refl->getClass() && 'Guzzle\Http\ClientInterface' === $refl->getClass()->getName()) {
110             return true;
111         }
112
113         return false;
114     }
115 }