Completed
Push — master ( 78f34b...2a7e47 )
by Matt
10s
created

LoaderManager::registerLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard;
4
5
use League\JsonGuard\Loaders\CurlWebLoader;
6
use League\JsonGuard\Loaders\FileGetContentsWebLoader;
7
use League\JsonGuard\Loaders\FileLoader;
8
9
class LoaderManager
10
{
11
    /**
12
     * @var Loader[]
13
     */
14
    private $loaders = [];
15
16
    /**
17
     * @param Loader[] $loaders
18
     */
19
    public function __construct(array $loaders = [])
20
    {
21
        if (empty($loaders)) {
22
            $this->registerDefaultFileLoader();
23
            $this->registerDefaultWebLoaders();
24
            return;
25
        }
26
27
        foreach ($loaders as $prefix => $loader) {
28
            $this->registerLoader($prefix, $loader);
29
        }
30
    }
31
32
    /**
33
     * Register a Loader for the given prefix.
34
     *
35
     * @param string $prefix
36
     * @param Loader $loader
37
     */
38
    public function registerLoader($prefix, Loader $loader)
39
    {
40
        $this->loaders[$prefix] = $loader;
41
    }
42
43
    /**
44
     * Get all registered loaders, keyed by the prefix they are registered to load schemas for.
45
     *
46
     * @return Loader[]
47
     */
48
    public function getLoaders()
49
    {
50
        return $this->loaders;
51
    }
52
53
    /**
54
     * Get the loader for the given prefix.
55
     *
56
     * @param string $prefix
57
     *
58
     * @return Loader
59
     * @throws \InvalidArgumentException
60
     */
61
    public function getLoader($prefix)
62
    {
63
        if (!$this->hasLoader($prefix)) {
64
            throw new \InvalidArgumentException(sprintf('A loader is not registered for the prefix "%s"', $prefix));
65
        }
66
67
        return $this->loaders[$prefix];
68
    }
69
70
    /**
71
     * @param string $prefix
72
     *
73
     * @return bool
74
     */
75
    public function hasLoader($prefix)
76
    {
77
        return isset($this->loaders[$prefix]);
78
    }
79
80
    /**
81
     * Register the default file loader.
82
     */
83
    private function registerDefaultFileLoader()
84
    {
85
        $this->loaders['file'] = new FileLoader();
86
    }
87
88
    /**
89
     * Register the default web loaders.  If the curl extension is loaded,
90
     * the CurlWebLoader will be used.  Otherwise the FileGetContentsWebLoader
91
     * will be used.  You can override this by registering your own loader
92
     * for the 'http' and 'https' protocols.
93
     */
94
    private function registerDefaultWebLoaders()
95
    {
96
        if (function_exists('curl_init')) {
97
            $this->loaders['https'] = new CurlWebLoader('https://');
98
            $this->loaders['http']  = new CurlWebLoader('http://');
99
        } else {
100
            $this->loaders['https'] = new FileGetContentsWebLoader('https://');
101
            $this->loaders['http']  = new FileGetContentsWebLoader('http://');
102
        }
103
    }
104
}
105