Plugin::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
ccs 1
cts 1
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php 
2
3
namespace Helmut\Forms;
4
5
use Helmut\Forms\Utility\Reflect;
6
use Helmut\Forms\Utility\Str;
7
8
abstract class Plugin {
9
	
10
    /**
11
     * Autoload path for plugin.
12
     *
13
     * @var string
14
     */
15
    protected $path;
16
17
    /**
18
     * Create a new plugin instance.
19 134
     *
20
     * @param  mixed  $config
21 134
     */
22 134
    public function __construct($config = null)
23
    {
24
        $this->configure($config);
25
26
        $this->setPath();
27
	}
28
29 134
    /**
30
     * Configure the plugin.
31 134
     *
32 134
     * @param  array  $config
33 134
     * @return void
34
     */   
35
    public function configure($config)
36
    {
37
        foreach ((array) $config as $key => $value) {
38
            $method = 'set'.Str::studly($key);
39
            if (method_exists($this, $method)) {
40
                call_user_func_array([$this, $method], [$value]);
41
            }
42
        }
43
    }
44
45
    /**
46
     * Set the plugin autoload path.
47
     *
48
     * @return void
49
     */
50
    public function setPath()
51
    {
52
        $this->path = Reflect::getDirectory($this);
53
    }
54
55
    /**
56
     * Get autoload path.
57
     *
58
     * @param string $append
59
     * @return string
60
     */
61
    public function path($append = null) 
62
    {
63
        return is_null($append) ? $this->path 
64
            : $this->path.'/'.ltrim(rtrim($append, '/'), '/').'/';
65
    }
66
67
    /**
68
     * Get paths to templates.
69
     *
70
     * @return array
71
     */
72
	public function templatePaths() 
73
	{
74 134
		$path = $this->path('templates');
75
		return [$path];
76 134
	}
77
78 134
    /**
79 134
     * Trigger an event callback. This allows
80
     * you to hook into events from within the plugin.
81
     * - onLoad
82 134
     * - onDefine
83
     * - onRender
84
     * - onSubmitted
85
     * - onCompleted
86
     * - onValidate
87
     * - onValidated
88
     * - onValid
89
     * - onInvalid
90
     *
91
     * @param  \Helmut\Forms\Form  $form
92
     * @param  string  $name
93
     * @param  array  $params
94
     * @return mixed
95
     */
96
    public function event($form, $name, $params = [])
97
    {
98
        $name = Str::studly($name);
99
100
        if (method_exists($this, 'on'.$name)) {
101
            return call_user_func_array(array($this, 'on'.$name), [$form, $params]);
102
        }
103
    }
104
105
}
106