-
Notifications
You must be signed in to change notification settings - Fork 11.2k
/
Copy pathComponent.php
121 lines (105 loc) · 3.1 KB
/
Component.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Illuminate\View;
use Closure;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
abstract class Component
{
/**
* That properties / methods that should not be exposed to the component.
*
* @var array
*/
protected $except = [];
/**
* The component attributes.
*
* @var \Illuminate\View\ComponentAttributeBag
*/
public $attributes;
/**
* Get the view that represents the component.
*
* @return string
*/
abstract public function view();
/**
* Get the data that should be supplied to the view.
*
* @author Freek Van der Herten
* @author Brent Roose
*
* @return array
*/
public function data()
{
$this->attributes = $this->attributes ?: new ComponentAttributeBag;
$class = new ReflectionClass($this);
$publicProperties = collect($class->getProperties(ReflectionProperty::IS_PUBLIC))
->reject(function (ReflectionProperty $property) {
return $this->shouldIgnore($property->getName());
})
->mapWithKeys(function (ReflectionProperty $property) {
return [$property->getName() => $this->{$property->getName()}];
});
$publicMethods = collect($class->getMethods(ReflectionMethod::IS_PUBLIC))
->reject(function (ReflectionMethod $method) {
return $this->shouldIgnore($method->getName());
})
->mapWithKeys(function (ReflectionMethod $method) {
return [$method->getName() => $this->createVariableFromMethod($method)];
});
return $publicProperties->merge($publicMethods)->toArray();
}
/**
* Create a callable variable from the given method.
*
* @param \ReflectionMethod $method
* @return mixed
*/
protected function createVariableFromMethod(ReflectionMethod $method)
{
return $method->getNumberOfParameters() === 0
? $this->{$method->getName()}()
: Closure::fromCallable([$this, $method->getName()]);
}
/**
* Set the extra attributes that the component should make available.
*
* @param array $attributes
* @return $this
*/
public function withAttributes(array $attributes)
{
$this->attributes = $this->attributes ?: new ComponentAttributeBag;
$this->attributes->setAttributes($attributes);
return $this;
}
/**
* Determine if the given property / method should be ignored.
*
* @param string $name
* @return bool
*/
protected function shouldIgnore($name)
{
return Str::startsWith($name, '__') ||
in_array($name, $this->ignoredMethods());
}
/**
* Get the methods that should be ignored.
*
* @return array
*/
protected function ignoredMethods()
{
return array_merge([
'view',
'data',
'withAttributes',
], $this->except);
}
}