DynamicWidgetsProvider::withPath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Displore\Widgets;
4
5
class DynamicWidgetsProvider
6
{
7
    /**
8
     * This matches widget names with their providers.
9
     * 
10
     * @var array
11
     */
12
    protected $widgets;
13
14
    /**
15
     * The path where the widget classes are.
16
     *
17
     * @var string
18
     */
19
    public $widgetPath;
20
21
    /**
22
     * The namespace for the widget classes.
23
     * 
24
     * @var string
25
     */
26
    public $namespace;
27
28
    /**
29
     * Set a new widget.
30
     * 
31
     * @param string $name
32
     * @param string $provider
33
     */
34
    public function set($name, $provider)
35
    {
36
        $this->widgets[$name] = $provider;
37
    }
38
39
    /**
40
     * Get a widget.
41
     * 
42
     * @param string     $name
43
     * @param array|null $parameters
44
     *
45
     * @throws WidgetNotFoundException
46
     */
47
    public function get($name, array $parameters = null)
48
    {
49
        if (isset($this->widgets[$name])) {
50
            return $this->getProvider($name)->getWidget($parameters);
51
        }
52
        throw new \Exception('Widget not found');
53
    }
54
55
    /**
56
     * Get the provider for the widget.
57
     * 
58
     * @param string $name
59
     *
60
     * @return object
61
     */
62
    public function getProvider($name)
63
    {
64
        return new $this->widgets[$name]();
65
    }
66
67
    /**
68
     * Get all registered widgets.
69
     * 
70
     * @return array
71
     */
72
    public function getWidgets()
73
    {
74
        return $this->widgets;
75
    }
76
77
    /**
78
     * Scan the widgets folder for classes.
79
     * 
80
     * @throws Exception
81
     *
82
     * @return $this
83
     */
84
    public function scanForProviders()
85
    {
86
        if ( ! isset($this->widgetPath)) {
87
            throw new \Exception('Widgets path not defined');
88
        }
89
90
        $files = glob($this->widgetPath.'/*.php');
91
92
        foreach ($files as $file) {
93
            $this->set($this->makeName($file), $this->makeProvider($file));
94
        }
95
96
        return $this;
97
    }
98
99
    /**
100
     * Make the name for the widget from its filepath.
101
     * 
102
     * @param string $file
103
     *
104
     * @return string
105
     */
106
    public function makeName($file)
107
    {
108
        return pathinfo($file, PATHINFO_FILENAME);
109
    }
110
111
    /**
112
     * Make the namespaced name of the widget provider class.
113
     * 
114
     * @param string $file
115
     *
116
     * @return string
117
     */
118
    public function makeProvider($file)
119
    {
120
        return $this->namespace.'\\'.$this->makeName($file);
121
    }
122
123
    /**
124
     * Define where to search for widgets.
125
     * 
126
     * @param string $widgetPath
127
     *
128
     * @throws Exception
129
     *
130
     * @return $this
131
     */
132
    public function withPath($widgetPath)
133
    {
134
        if (is_dir($widgetPath)) {
135
            $this->widgetPath = $widgetPath;
136
137
            return $this;
138
        }
139
        throw new \Exception('Widgets path not accessible');
140
    }
141
142
    /**
143
     * Define the widgets' namespace.
144
     * 
145
     * @param string $namespace
146
     *
147
     * @return $this
148
     */
149
    public function withNamespace($namespace)
150
    {
151
        $this->namespace = $namespace;
152
153
        return $this;
154
    }
155
}
156