4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Behat\HelperContainer\Argument;
13 use Behat\Behat\Context\Argument\ArgumentResolver;
14 use Behat\Behat\HelperContainer\Environment\ServiceContainerEnvironment;
15 use Behat\Behat\Context\Argument\ArgumentResolverFactory;
16 use Behat\Behat\Context\Argument\SuiteScopedResolverFactory;
17 use Behat\Behat\HelperContainer\BuiltInServiceContainer;
18 use Behat\Behat\HelperContainer\Exception\WrongContainerClassException;
19 use Behat\Behat\HelperContainer\Exception\WrongServicesConfigurationException;
20 use Behat\Behat\HelperContainer\ServiceContainer\HelperContainerExtension;
21 use Behat\Testwork\Environment\Environment;
22 use Behat\Testwork\Suite\Suite;
23 use Psr\Container\ContainerInterface;
24 use Symfony\Component\DependencyInjection\TaggedContainerInterface;
27 * Generates ServiceContainer argument resolvers based on suite's `services` setting.
29 * @see ContextEnvironmentHandler
31 * @author Konstantin Kudryashov <ever.zet@gmail.com>
33 final class ServicesResolverFactory implements SuiteScopedResolverFactory, ArgumentResolverFactory
36 * @var TaggedContainerInterface
41 * Initialises factory.
43 * @param TaggedContainerInterface $container
45 public function __construct(TaggedContainerInterface $container)
47 $this->container = $container;
53 * @deprecated as part of SuiteScopedResolverFactory deprecation. Would be removed in 4.0
55 * @throws WrongServicesConfigurationException
56 * @throws WrongContainerClassException
58 public function generateArgumentResolvers(Suite $suite)
61 'SuiteScopedResolverFactory::generateArgumentResolvers() was deprecated and will be removed in 4.0',
65 if (!$suite->hasSetting('services')) {
69 $container = $this->createContainer($suite->getSetting('services'));
71 return $this->createResolvers($container, false);
77 * @throws WrongServicesConfigurationException
78 * @throws WrongContainerClassException
80 public function createArgumentResolvers(Environment $environment)
82 $suite = $environment->getSuite();
84 if (!$suite->hasSetting('services')) {
88 $container = $this->createContainer($suite->getSetting('services'));
89 $autowire = $suite->hasSetting('autowire') && $suite->getSetting('autowire');
91 if ($environment instanceof ServiceContainerEnvironment) {
92 $environment->setServiceContainer($container);
95 return $this->createResolvers($container, $autowire);
99 * Creates container from the setting passed.
101 * @param string $settings
105 * @throws WrongServicesConfigurationException
107 private function createContainer($settings)
109 if (is_string($settings)) {
110 return $this->createContainerFromString($settings);
113 if (is_array($settings)) {
114 return $this->createContainerFromArray($settings);
117 throw new WrongServicesConfigurationException(
118 sprintf('`services` must be either string or an array, but `%s` given.', gettype($settings))
123 * Creates custom container using class/constructor given.
125 * @param string $settings
129 * @throws WrongServicesConfigurationException
131 private function createContainerFromString($settings)
133 if (0 === mb_strpos($settings, '@')) {
134 return $this->loadContainerFromContainer(mb_substr($settings, 1));
137 return $this->createContainerFromClassSpec($settings);
141 * Creates built-in service container with provided settings.
143 * @param array $settings
145 * @return BuiltInServiceContainer
147 private function createContainerFromArray(array $settings)
149 return new BuiltInServiceContainer($settings);
153 * Loads container from string.
155 * @param string $name
159 * @throws WrongServicesConfigurationException
161 private function loadContainerFromContainer($name)
163 $services = $this->container->findTaggedServiceIds(HelperContainerExtension::HELPER_CONTAINER_TAG);
165 if (!array_key_exists($name, $services)) {
166 throw new WrongServicesConfigurationException(
167 sprintf('Service container `@%s` was not found.', $name)
171 return $this->container->get($name);
175 * Creates container from string-based class spec.
177 * @param string $classSpec
181 private function createContainerFromClassSpec($classSpec)
183 $constructor = explode('::', $classSpec);
185 if (2 === count($constructor)) {
186 return call_user_func($constructor);
189 return new $constructor[0];
193 * Checks if container implements the correct interface and creates resolver using it.
195 * @param mixed $container
196 * @param bool $autowire
198 * @return ArgumentResolver[]
200 * @throws WrongContainerClassException
202 private function createResolvers($container, $autowire)
204 if (!$container instanceof ContainerInterface) {
205 throw new WrongContainerClassException(
207 'Service container is expected to implement `Psr\Container\ContainerInterface`, but `%s` does not.',
208 get_class($container)
210 get_class($container)
215 return array(new ServicesResolver($container), new AutowiringResolver($container));
218 return array(new ServicesResolver($container));