|
| 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\DependencyInjection\Loader\PhpFileLoader\Internal; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Definition; |
| 15 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Nicolas Grekas <p@tchwork.com> |
| 19 | + * |
| 20 | + * @method $this public() |
| 21 | + * @method $this private() |
| 22 | + * |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +abstract class AbstractDefaultsConfigurator |
| 26 | +{ |
| 27 | + protected $file; |
| 28 | + protected $definition; |
| 29 | + |
| 30 | + public function __construct($file, Definition $definition) |
| 31 | + { |
| 32 | + $this->file = $file; |
| 33 | + $this->definition = $definition; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Adds a tag for this definition. |
| 38 | + * |
| 39 | + * @param string $name The tag name |
| 40 | + * @param array $attributes An array of attributes |
| 41 | + * |
| 42 | + * @return $this |
| 43 | + */ |
| 44 | + public function tag($name, array $attributes = array()) |
| 45 | + { |
| 46 | + if (!is_string($name) || '' === $name) { |
| 47 | + throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string "%s".', $this->file)); |
| 48 | + } |
| 49 | + |
| 50 | + foreach ($attributes as $attribute => $value) { |
| 51 | + if (!is_scalar($value) && null !== $value) { |
| 52 | + throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in %s.', $name, $attribute, $this->file)); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + $this->definition->addTag($name, $attributes); |
| 57 | + |
| 58 | + return $this; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return $this |
| 63 | + */ |
| 64 | + public function __call($method, $args) |
| 65 | + { |
| 66 | + if ('public' === strtolower($method)) { |
| 67 | + $this->definition->setPublic(true); |
| 68 | + } elseif ('private' === strtolower($method)) { |
| 69 | + $this->definition->setPublic(false); |
| 70 | + } else { |
| 71 | + throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $method)); |
| 72 | + } |
| 73 | + |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Enables/disables autowiring. |
| 79 | + * |
| 80 | + * @param bool $autowired |
| 81 | + * |
| 82 | + * @return $this |
| 83 | + */ |
| 84 | + public function autowire($autowired = true) |
| 85 | + { |
| 86 | + $this->definition->setAutowired($autowired); |
| 87 | + |
| 88 | + return $this; |
| 89 | + } |
| 90 | +} |
0 commit comments