Completed
Pull Request — master (#165)
by Paul
03:21
created

IniFileLoader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 2
B load() 0 17 6
1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright  Copyright (c) 2012 Paul Dragoonis <[email protected]>
6
 * @license    http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link       http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\Config\Loader;
12
13
use Symfony\Component\Config\Loader\FileLoader;
14
15
/**
16
 * IniFileLoader loads parameters from INI files.
17
 *
18
 * @author     Vítor Brandão <[email protected]>
19
 */
20
class IniFileLoader extends FileLoader
21
{
22
    /**
23
     * Loads a resource.
24
     *
25
     * @param mixed  $file The resource
26
     * @param string $type The resource type
27
     *
28
     * @throws InvalidArgumentException When ini file is not valid
29
     */
30
    public function load($file, $type = null)
31
    {
32
        $path   = $this->locator->locate($file);
33
        $config = array();
34
35
        $result = parse_ini_file($path, true);
36
        if (false === $result || array() === $result) {
37
            throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $file));
38
        }
39
40
        if (isset($result['parameters']) && is_array($result['parameters'])) {
41
            $config['parameters'] = array();
42
            foreach ($result['parameters'] as $key => $value) {
43
                $config['parameters'][$key] = $value;
44
            }
45
        }
46
    }
47
48
    /**
49
     * Returns true if this class supports the given resource.
50
     *
51
     * @param mixed  $resource A resource
52
     * @param string $type     The resource type
53
     *
54
     * @return bool true if this class supports the given resource, false otherwise
55
     */
56
    public function supports($resource, $type = null)
57
    {
58
        return is_string($resource) && 'ini' === pathinfo($resource, PATHINFO_EXTENSION);
59
    }
60
}
61