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\Config\Loader;
13
14/**
15 * LoaderResolver selects a loader for a given resource.
16 *
17 * A resource can be anything (e.g. a full path to a config file or a Closure).
18 * Each loader determines whether it can load a resource and how.
19 *
20 * @author Fabien Potencier <fabien@symfony.com>
21 */
22class LoaderResolver implements LoaderResolverInterface
23{
24    /**
25     * @var LoaderInterface[] An array of LoaderInterface objects
26     */
27    private $loaders = [];
28
29    /**
30     * @param LoaderInterface[] $loaders An array of loaders
31     */
32    public function __construct(array $loaders = [])
33    {
34        foreach ($loaders as $loader) {
35            $this->addLoader($loader);
36        }
37    }
38
39    /**
40     * {@inheritdoc}
41     */
42    public function resolve($resource, $type = null)
43    {
44        foreach ($this->loaders as $loader) {
45            if ($loader->supports($resource, $type)) {
46                return $loader;
47            }
48        }
49
50        return false;
51    }
52
53    public function addLoader(LoaderInterface $loader)
54    {
55        $this->loaders[] = $loader;
56        $loader->setResolver($this);
57    }
58
59    /**
60     * Returns the registered loaders.
61     *
62     * @return LoaderInterface[] An array of LoaderInterface instances
63     */
64    public function getLoaders()
65    {
66        return $this->loaders;
67    }
68}
69