-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInjector.php
105 lines (89 loc) · 2.58 KB
/
Injector.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* Copyright (c) 2017. Alexandr Kosarev, @kosarev.by
*/
/**
* Created by PhpStorm.
* User: Alexandr
* Date: 13.12.2017
* Time: 15:44
*/
namespace Joomplace\X\Helper;
use Joomla\CMS\Factory;
use Joomla\Input\Input;
trait Injector
{
protected function injectRequiredArg($arg, $filter = 'RAW', $input = null)
{
$input = $this->getInput($input);
if ($input->get($arg, null, $filter) == null) {
// TODO: improve
throw new \Exception("Injecting undefined $arg", 500);
} else {
return $this->injectArg($arg, null, $filter, $input);
}
}
protected function getInput($input = null){
if($input === null){
if(isset($this->input)){
$input = $this->input;
}else{
$input = Factory::getApplication()->input;
}
$jsonInput = json_decode(file_get_contents('php://input'));
if($jsonInput){
foreach ($jsonInput as $k => $v){
$input->def($k,$v);
}
}
}
return $input;
}
protected function injectArg($arg, $default = null, $filter = 'RAW', $input = null)
{
$input = $this->getInput($input);
if ($input instanceof Input) {
$value = $input->get($arg, $default, $filter);
} else {
$value = (new \Joomla\Filter\InputFilter)->clean($input->get($arg, $default), $filter);
}
return $value;
}
protected function getArgs($method)
{
if ($this->methodExists($method)) {
$ref = new \ReflectionMethod($this->getClassName(), $method);
$result = $ref->getParameters();
} else {
$result = array();
}
return $result;
}
protected function methodExists($method, $in_target = false)
{
if (method_exists($this->getClassName(), $method)) {
if ($in_target) {
$ref = new \ReflectionMethod($this->getClassName(), $method);
if (strtolower($ref->getDeclaringClass()->name)
== strtolower($this->getClassName())
) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
public function getClassName()
{
return get_called_class();
}
protected function methodDefined($method)
{
return $this->methodExists($method, true);
}
}