Completed
Push — 9.0-dev ( bf07f2...007112 )
by Radu
01:34
created

Framework::loadLibraryConfiguration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
namespace WebServCo\Framework;
3
4
final class Framework
5
{
6
    const OS_WINDOWS = 'Windows';
7
    const OS_LINUX = 'Linux';
8
    const OS_UNSUPPORTED = 'Unsupported';
9
    
10
    /**
11
     * Stores library instances.
12
     */
13
    private static $libraries = [];
14
    
15
    private static function getFullClassName($className, $classType)
16
    {
17
        switch ($classType) {
18
            case 'Library':
19
                return __NAMESPACE__ . '\\Libraries\\' . $className;
20
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
21
            default:
22
                return $className;
23
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
24
        }
25
    }
26
    
27
    /**
28
     * Returns the path the framework project is located in.
29
     *
30
     * @return string
31
     */
32
    public static function getPath()
33
    {
34
        return str_replace('src/WebServCo/Framework', '', __DIR__);
35
    }
36
    
37
    private static function loadLibraryConfiguration($configName)
38
    {
39
        if ('Config' == $configName) {
40
            return false;
41
        }
42
        $projectPath = self::getLibrary('Config')->get(
43
            sprintf(
44
                'app%1$spath%1$sproject',
45
                \WebServCo\Framework\Settings::DIVIDER
46
            )
47
        );
48
        if (empty($projectPath)) {
49
            return false;
50
        }
51
        return self::getLibrary('Config')->load($configName, $projectPath);
52
    }
53
    
54
    private static function loadLibrary($className, $fullClassName, $configName = null)
0 ignored issues
show
Coding Style introduced by
loadLibrary uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
loadLibrary uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
55
    {
56
        if (!class_exists($fullClassName)) {
57
            throw new \ErrorException(
58
                sprintf('Library %s not found', $fullClassName)
59
            );
60
        }
61
        $configName = $configName ?: $className;
62
        $config = self::loadLibraryConfiguration($configName);
63
        /**
64
         * Libraries can have custom parameters to constructor,
65
         * however the configuration array is always the first.
66
         * $args = is_array($args) ? array_merge([$config], $args) : [$config];
67
         */
68
        switch ($className) {
69
            case 'Request':
70
                $args = [$config, $_SERVER, $_POST];
71
                break;
72
            default:
73
                $args = [$config];
74
                break;
75
        }
76
        
77
        $reflection = new \ReflectionClass($fullClassName);
78
        return $reflection->newInstanceArgs($args);
79
    }
80
    
81
    public static function getLibrary($className, $storageKey = null, $configName = null)
82
    {
83
        $fullClassName = self::getFullClassName($className, 'Library');
84
        
85
        $storageKey = $storageKey ?: $fullClassName;
86
        
87
        if (!isset(self::$libraries[$storageKey])) {
88
            self::$libraries[$storageKey] = self::loadLibrary($className, $fullClassName, $configName);
89
        }
90
        
91
        return self::$libraries[$storageKey];
92
    }
93
    
94
    /**
95
     * Checks if interface type is CLI
96
     */
97
    public static function isCLI()
98
    {
99
        return 'cli' === PHP_SAPI;
100
    }
101
    
102
    /**
103
     * Get operating system (if supported).
104
     */
105
    public static function getOS()
106
    {
107
        $uname = php_uname('s');
108
        if (0 === strncasecmp($uname, 'Win', 3)) {
109
            return self::OS_WINDOWS;
110
        } elseif (0 === strncasecmp($uname, 'Linux', 5)) {
111
            return self::OS_LINUX;
112
        } else {
113
            return self::OS_UNSUPPORTED;
114
        }
115
    }
116
}
117