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
12namespace Symfony\Component\DependencyInjection\Loader\Configurator;
13
14use Symfony\Component\DependencyInjection\Definition;
15use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
16
17abstract class AbstractServiceConfigurator extends AbstractConfigurator
18{
19    protected $parent;
20    protected $id;
21    private $defaultTags = [];
22
23    public function __construct(ServicesConfigurator $parent, Definition $definition, string $id = null, array $defaultTags = [])
24    {
25        $this->parent = $parent;
26        $this->definition = $definition;
27        $this->id = $id;
28        $this->defaultTags = $defaultTags;
29    }
30
31    public function __destruct()
32    {
33        // default tags should be added last
34        foreach ($this->defaultTags as $name => $attributes) {
35            foreach ($attributes as $attributes) {
36                $this->definition->addTag($name, $attributes);
37            }
38        }
39        $this->defaultTags = [];
40    }
41
42    /**
43     * Registers a service.
44     */
45    final public function set(string $id, string $class = null): ServiceConfigurator
46    {
47        $this->__destruct();
48
49        return $this->parent->set($id, $class);
50    }
51
52    /**
53     * Creates an alias.
54     */
55    final public function alias(string $id, string $referencedId): AliasConfigurator
56    {
57        $this->__destruct();
58
59        return $this->parent->alias($id, $referencedId);
60    }
61
62    /**
63     * Registers a PSR-4 namespace using a glob pattern.
64     */
65    final public function load(string $namespace, string $resource): PrototypeConfigurator
66    {
67        $this->__destruct();
68
69        return $this->parent->load($namespace, $resource);
70    }
71
72    /**
73     * Gets an already defined service definition.
74     *
75     * @throws ServiceNotFoundException if the service definition does not exist
76     */
77    final public function get(string $id): ServiceConfigurator
78    {
79        $this->__destruct();
80
81        return $this->parent->get($id);
82    }
83
84    /**
85     * Registers a service.
86     */
87    final public function __invoke(string $id, string $class = null): ServiceConfigurator
88    {
89        $this->__destruct();
90
91        return $this->parent->set($id, $class);
92    }
93}
94