• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / di / 6739035766

02 Nov 2023 10:44PM UTC coverage: 52.037% (-41.8%) from 93.846%
6739035766

push

github

dg
x

1009 of 1939 relevant lines covered (52.04%)

0.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

44.74
/src/DI/Config/Loader.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\DI\Config;
11

12
use Nette;
13
use Nette\Utils\Validators;
14

15

16
/**
17
 * Configuration file loader.
18
 */
19
class Loader
20
{
21
        use Nette\SmartObject;
22

23
        private const IncludesKey = 'includes';
24

25
        private $adapters = [
26
                'php' => Adapters\PhpAdapter::class,
27
                'neon' => Adapters\NeonAdapter::class,
28
        ];
29

30
        private $dependencies = [];
31

32
        private $loadedFiles = [];
33

34
        private $parameters = [];
35

36

37
        /**
38
         * Reads configuration from file.
39
         */
40
        public function load(string $file, ?bool $merge = true): array
1✔
41
        {
42
                if (!is_file($file) || !is_readable($file)) {
1✔
43
                        throw new Nette\FileNotFoundException(sprintf("File '%s' is missing or is not readable.", $file));
×
44
                }
45

46
                if (isset($this->loadedFiles[$file])) {
1✔
47
                        throw new Nette\InvalidStateException(sprintf("Recursive included file '%s'", $file));
×
48
                }
49

50
                $this->loadedFiles[$file] = true;
1✔
51

52
                $this->dependencies[] = $file;
1✔
53
                $data = $this->getAdapter($file)->load($file);
1✔
54

55
                $res = [];
1✔
56
                if (isset($data[self::IncludesKey])) {
1✔
57
                        Validators::assert($data[self::IncludesKey], 'list', "section 'includes' in file '$file'");
×
58
                        $includes = Nette\DI\Helpers::expand($data[self::IncludesKey], $this->parameters);
×
59
                        foreach ($includes as $include) {
×
60
                                $include = $this->expandIncludedFile($include, $file);
×
61
                                $res = Nette\Schema\Helpers::merge($this->load($include, $merge), $res);
×
62
                        }
63
                }
64

65
                unset($data[self::IncludesKey], $this->loadedFiles[$file]);
1✔
66

67
                if ($merge === false) {
1✔
68
                        $res[] = $data;
×
69
                } else {
70
                        $res = Nette\Schema\Helpers::merge($data, $res);
1✔
71
                }
72

73
                return $res;
1✔
74
        }
75

76

77
        /** @deprecated */
78
        public function save(array $data, string $file): void
79
        {
80
                trigger_error(__METHOD__ . "() is deprecated, use adapter's dump() method.", E_USER_DEPRECATED);
×
81
                if (file_put_contents($file, $this->getAdapter($file)->dump($data)) === false) {
×
82
                        throw new Nette\IOException(sprintf("Cannot write file '%s'.", $file));
×
83
                }
84
        }
85

86

87
        /**
88
         * Returns configuration files.
89
         */
90
        public function getDependencies(): array
91
        {
92
                return array_unique($this->dependencies);
×
93
        }
94

95

96
        /**
97
         * Expands included file name.
98
         */
99
        public function expandIncludedFile(string $includedFile, string $mainFile): string
100
        {
101
                return preg_match('#([a-z]+:)?[/\\\\]#Ai', $includedFile) // is absolute
×
102
                        ? $includedFile
×
103
                        : dirname($mainFile) . '/' . $includedFile;
×
104
        }
105

106

107
        /**
108
         * Registers adapter for given file extension.
109
         * @param  string|Adapter  $adapter
110
         * @return static
111
         */
112
        public function addAdapter(string $extension, $adapter)
113
        {
114
                $this->adapters[strtolower($extension)] = $adapter;
×
115
                return $this;
×
116
        }
117

118

119
        private function getAdapter(string $file): Adapter
1✔
120
        {
121
                $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
1✔
122
                if (!isset($this->adapters[$extension])) {
1✔
123
                        throw new Nette\InvalidArgumentException(sprintf("Unknown file extension '%s'.", $file));
×
124
                }
125

126
                return is_object($this->adapters[$extension])
1✔
127
                        ? $this->adapters[$extension]
×
128
                        : new $this->adapters[$extension];
1✔
129
        }
130

131

132
        /** @return static */
133
        public function setParameters(array $params)
134
        {
135
                $this->parameters = $params;
×
136
                return $this;
×
137
        }
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc