Completed
Push — master ( cc9f19...85e40f )
by Pavel
11s
created

PHPConfigLoader::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace App\Common\Config;
4
5
use Symfony\Component\Config\Loader\FileLoader;
6
use Symfony\Component\Config\Exception\FileLoaderLoadException;
7
use Symfony\Component\Config\Definition\Processor;
8
9
/**
10
 * Class PHPConfigLoader
11
 *
12
 * @package App\Common\Config
13
 */
14
class PHPConfigLoader extends FileLoader
15
{
16
    /**
17
     * Import all loadable resources
18
     *
19
     * @return array
20
     * @throws FileLoaderLoadException
21
     */
22
    public function importAll()
23
    {
24
        // in order to import all we need to have locator to locate these "all"
25
        // locator need
26
        if (empty($this->locator)) {
27
            throw new FileLoaderLoadException("importAll()");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal importAll() does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
28
        }
29
30
        // locator must be capable of locating all
31
        if (!is_callable([$this->locator, 'locateAll'])) {
32
            throw new FileLoaderLoadException("locateAll()");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal locateAll() does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
33
        }
34
35
        // build list of 'all' files
36
        $files = $this->locator->locateAll();
0 ignored issues
show
Bug introduced by
The method locateAll() does not exist on Symfony\Component\Config\FileLocatorInterface. Did you maybe mean locate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
37
38
        // and now try to import configuration from this list of 'all' files
39
        $res = [];
40
        foreach ($files as $file) {
41
            if ($this->supports($file)) {
42
                // file is supported - load/import it
43
                $config = $this->load($file);
44
45
                if (!empty($config['definition'])) {
46
                    // loaded config can be verified
47
                    $processor = new Processor();
48
                    $configDefinition = new $config['definition']();
49
                    // process config according to specified definition
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
//file_put_contents('/tmp/log', print_r($config, true), FILE_APPEND);
51
                    $config = $processor->processConfiguration(
52
                        $configDefinition,
53
                        [$config]
54
                    );
55
//file_put_contents('/tmp/log', print_r($config, true), FILE_APPEND);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
                }
57
58
                // combine all configs into one "unified general config"
59
                $res = array_merge($res, $config);
60
            }
61
        }
62
63
        // array of configuration
64
        return $res;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function load($resource, $type = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
71
    {
72
        // for PHP file load process is quite simple - just return config file content
73
        return require_once $resource;
74
    }
75
76
    /**
77
     * Returns whether current loader supports specified resource load
78
     *
79
     * @param mixed $resource path to config file
80
     * @param null $type unsused
81
     * @return bool
82
     */
83
    public function supports($resource, $type = null)
84
    {
85
        // $resource is expected to be path to config file
86
        if (!is_string($resource)) {
87
            return false;
88
        }
89
90
        // specified config file should be readable
91
        if (!@is_file($resource) || !@is_readable($resource)) {
92
            return false;
93
        }
94
95
        // simple check - PHP loader accepts PHP files, thus let's check file extension to be 'php'
96
97
        // fetch config file extension
98
        $extension = pathinfo($resource, PATHINFO_EXTENSION);
99
100
        // PHP loader accepts PHP files
101
        return  $extension == 'php';
102
    }
103
}
104