Skip to content

Instantly share code, notes, and snippets.

@nicolas-grekas
Last active January 19, 2017 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolas-grekas/952b6276f9d961b3a81f127f449bd3a5 to your computer and use it in GitHub Desktop.
Save nicolas-grekas/952b6276f9d961b3a81f127f449bd3a5 to your computer and use it in GitHub Desktop.
--- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
+++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
@@ -157,6 +157,16 @@ class YamlFileLoader extends FileLoader
if (!is_array($content['services'])) {
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
}
+
+ if (isset($content['services']['_instanceof'])) {
+ if (!is_array($types = $content['services']['_instanceof'])) {
+ throw new InvalidArgumentException(sprintf('The "_instanceof" key must contain an array, "%s" given in "%s".', gettype($types), $file));
+ }
+ foreach ($types as $id => $service) {
+ $this->parseDefinition($id, $service, $file, array(), true);
+ }
+ }
+
if (isset($content['services']['_defaults'])) {
if (!is_array($defaults = $content['services']['_defaults'])) {
throw new InvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file));
@@ -206,7 +216,7 @@ class YamlFileLoader extends FileLoader
}
foreach ($content['services'] as $id => $service) {
- $this->parseDefinition($id, $service, $file, $defaults);
+ $this->parseDefinition($id, $service, $file, $defaults, false);
}
}
@@ -217,10 +227,11 @@ class YamlFileLoader extends FileLoader
* @param array $service
* @param string $file
* @param array $defaults
+ * @param bool $isConditionalType
*
* @throws InvalidArgumentException When tags are invalid
*/
- private function parseDefinition($id, $service, $file, array $defaults)
+ private function parseDefinition($id, $service, $file, array $defaults, $isConditionalType)
{
if (is_string($service) && 0 === strpos($service, '@')) {
$public = isset($defaults['public']) ? $defaults['public'] : true;
@@ -410,7 +421,7 @@ class YamlFileLoader extends FileLoader
}
}
- $this->container->setDefinition($id, $definition);
+ $this->setDefinition($id, $definition, $isConditionalType);
}
/**
diff --git a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
index 90cd6bc..4171d8b 100644
--- a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
+++ b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
@@ -24,6 +24,8 @@ abstract class FileLoader extends BaseFileLoader
{
protected $container;
+ private $conditionalTypes = array();
+
/**
* @param ContainerBuilder $container A ContainerBuilder instance
* @param FileLocatorInterface $locator A FileLocator instance
@@ -34,4 +36,15 @@ abstract class FileLoader extends BaseFileLoader
parent::__construct($locator);
}
+
+ public function setDefinition($id, Definition $definition, $isConditionalType = false)
+ {
+ if ($isConditionalType) {
+ $this->conditionalTypes[$id] = $definition;
+
+ return;
+ }
+
+ $this->container->setDefinition($id, $definition);
+ }
}
diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
index 1329d22..3fe8ad6 100644
--- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
+++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
@@ -157,6 +157,16 @@ class YamlFileLoader extends FileLoader
if (!is_array($content['services'])) {
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
}
+
+ if (isset($content['services']['_instanceof'])) {
+ if (!is_array($types = $content['services']['_instanceof'])) {
+ throw new InvalidArgumentException(sprintf('The "_instanceof" key must contain an array, "%s" given in "%s".', gettype($types), $file));
+ }
+ foreach ($types as $id => $service) {
+ $this->parseDefinition($id, $service, $file, array(), true);
+ }
+ }
+
if (isset($content['services']['_defaults'])) {
if (!is_array($defaults = $content['services']['_defaults'])) {
throw new InvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file));
@@ -206,7 +216,7 @@ class YamlFileLoader extends FileLoader
}
foreach ($content['services'] as $id => $service) {
- $this->parseDefinition($id, $service, $file, $defaults);
+ $this->parseDefinition($id, $service, $file, $defaults, false);
}
}
@@ -217,10 +227,11 @@ class YamlFileLoader extends FileLoader
* @param array $service
* @param string $file
* @param array $defaults
+ * @param bool $isConditionalType
*
* @throws InvalidArgumentException When tags are invalid
*/
- private function parseDefinition($id, $service, $file, array $defaults)
+ private function parseDefinition($id, $service, $file, array $defaults, $isConditionalType)
{
if (is_string($service) && 0 === strpos($service, '@')) {
$public = isset($defaults['public']) ? $defaults['public'] : true;
@@ -410,7 +421,7 @@ class YamlFileLoader extends FileLoader
}
}
- $this->container->setDefinition($id, $definition);
+ $this->setDefinition($id, $definition, $isConditionalType);
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment