PhpLoader   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 49
ccs 21
cts 21
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A includedScript() 0 3 1
A contentPath() 0 16 4
A load() 0 4 2
1
<?php
2
3
namespace kalanis\kw_scripts\Loaders;
4
5
6
use kalanis\kw_paths\Interfaces\IPaths;
7
use kalanis\kw_paths\Path;
8
use kalanis\kw_routed_paths\RoutedPath;
9
use kalanis\kw_scripts\Interfaces\ILoader;
10
11
12
/**
13
 * Class PhpLoader
14
 * @package kalanis\kw_scripts\Loaders
15
 * Load scripts from predefined paths
16
 */
17
class PhpLoader implements ILoader
18
{
19
    /** @var string[] */
20
    protected array $pathMasks = [
21
        '%2$s%1$s%3$s%1$s%4$s%1$s%5$s%1$s%6$s%1$s%7$s%1$s%8$s', # user dir, user module with conf name
22
        '%2$s%1$s%3$s%1$s%4$s%1$s%5$s%1$s%6$s%1$s%7$s%1$s%7$s', # user dir, user module
23
        '%2$s%1$s%3$s%1$s%4$s%1$s%5$s%1$s%6$s%1$s%7$s', # user dir, all user confs
24
        '%2$s%1$s%3$s%1$s%4$s%1$s%5$s%1$s%6$s', # user dir, conf named by module
25
        '%2$s%1$s%5$s%1$s%6$s%1$s%7$s%1$s%8$s', # all modules, main script with name
26
        '%2$s%1$s%5$s%1$s%6$s%1$s%7$s%1$s%7$s', # all modules, default main script
27
        '%2$s%1$s%5$s%1$s%6$s%1$s%7$s', # all modules, scripts in root
28
    ];
29
30
    protected Path $pathLib;
31
    protected RoutedPath $routedLib;
32
33 3
    public function __construct(Path $pathLib, RoutedPath $routedLib)
34
    {
35 3
        $this->pathLib = $pathLib;
36 3
        $this->routedLib = $routedLib;
37 3
    }
38
39 2
    public function load(string $module, string $wantedPath = ''): string
40
    {
41 2
        $includingPath = $this->contentPath($module, $wantedPath);
42 2
        return (!empty($includingPath)) ? $this->includedScript($includingPath) : '';
43
    }
44
45 2
    public function contentPath(string $module, string $conf = ''): ?string
46
    {
47 2
        $basicLookupDir = $this->pathLib->getDocumentRoot() . $this->pathLib->getPathToSystemRoot();
48 2
        foreach ($this->pathMasks as $pathMask) {
49 2
            $unmasked = sprintf( $pathMask,
50 2
                DIRECTORY_SEPARATOR, $basicLookupDir,
51 2
                IPaths::DIR_USER, $this->routedLib->getUser(),
52 2
                IPaths::DIR_MODULE, $module,
53 2
                IPaths::DIR_STYLE, $conf
54
            );
55 2
            $path = realpath($unmasked);
56 2
            if ($path && is_file($path)) {
57 1
                return $path;
58
            }
59
        }
60 1
        return null;
61
    }
62
63 1
    protected function includedScript(string $path): string
64
    {
65 1
        return (string) @file_get_contents($path);
66
    }
67
}
68