aboutsummaryrefslogtreecommitdiff
path: root/lib/BridgeFactory.php
blob: 3e355b7a899e74ea16c90d7ed58c394e1dda1aa8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

final class BridgeFactory
{
    private $folder;
    private $bridgeNames = [];
    private $whitelist = [];

    public function __construct(string $folder = PATH_LIB_BRIDGES)
    {
        $this->folder = $folder;

        // create names
        foreach (scandir($this->folder) as $file) {
            if (preg_match('/^([^.]+)Bridge\.php$/U', $file, $m)) {
                $this->bridgeNames[] = $m[1];
            }
        }

        // create whitelist
        if (file_exists(WHITELIST)) {
            $contents = trim(file_get_contents(WHITELIST));
        } elseif (file_exists(WHITELIST_DEFAULT)) {
            $contents = trim(file_get_contents(WHITELIST_DEFAULT));
        } else {
            $contents = '';
        }
        if ($contents === '*') { // Whitelist all bridges
            $this->whitelist = $this->getBridgeNames();
        } else {
            foreach (explode("\n", $contents) as $bridgeName) {
                $this->whitelist[] = $this->sanitizeBridgeName($bridgeName);
            }
        }
    }

    public function create(string $name): BridgeInterface
    {
        if (preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) {
            $className = sprintf('%sBridge', $this->sanitizeBridgeName($name));
            return new $className();
        }
        throw new \InvalidArgumentException('Bridge name invalid!');
    }

    public function getBridgeNames(): array
    {
        return $this->bridgeNames;
    }

    public function isWhitelisted($name): bool
    {
        return in_array($this->sanitizeBridgeName($name), $this->whitelist);
    }

    private function sanitizeBridgeName($name)
    {
        if (!is_string($name)) {
            return null;
        }

        // Trim trailing '.php' if exists
        if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
            $name = $matches[1];
        }

        // Trim trailing 'Bridge' if exists
        if (preg_match('/(.+)(?:Bridge)/i', $name, $matches)) {
            $name = $matches[1];
        }

        // Improve performance for correctly written bridge names
        if (in_array($name, $this->getBridgeNames())) {
            $index = array_search($name, $this->getBridgeNames());
            return $this->getBridgeNames()[$index];
        }

        // The name is valid if a corresponding bridge file is found on disk
        if (in_array(strtolower($name), array_map('strtolower', $this->getBridgeNames()))) {
            $index = array_search(strtolower($name), array_map('strtolower', $this->getBridgeNames()));
            return $this->getBridgeNames()[$index];
        }

        Debug::log('Invalid bridge name specified: "' . $name . '"!');
        return null;
    }
}