Skip to content

Commit f1ef6e6

Browse files
committed
extract class
1 parent ab72023 commit f1ef6e6

File tree

2 files changed

+98
-47
lines changed

2 files changed

+98
-47
lines changed

src/Illuminate/View/Component.php

+3-47
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
namespace Illuminate\View;
44

5-
use ArrayIterator;
65
use Closure;
76
use Illuminate\Container\Container;
8-
use Illuminate\Contracts\Support\DeferringDisplayableValue;
9-
use Illuminate\Support\Enumerable;
107
use Illuminate\Support\Str;
11-
use IteratorAggregate;
128
use ReflectionClass;
139
use ReflectionMethod;
1410
use ReflectionProperty;
@@ -194,53 +190,13 @@ protected function createVariableFromMethod(ReflectionMethod $method)
194190
* Create an invokable, toStringable variable for the given component method.
195191
*
196192
* @param string $method
197-
* @return object
193+
* @return \Illuminate\View\InvokableComponentVariable
198194
*/
199195
protected function createInvokableVariable(string $method)
200196
{
201-
return new class(function () use ($method) {
197+
return new InvokableComponentVariable(function () use ($method) {
202198
return $this->{$method}();
203-
}) implements DeferringDisplayableValue, IteratorAggregate {
204-
protected $callable;
205-
206-
public function __construct(Closure $callable)
207-
{
208-
$this->callable = $callable;
209-
}
210-
211-
public function resolveDisplayableValue()
212-
{
213-
return $this->__invoke();
214-
}
215-
216-
public function getIterator()
217-
{
218-
$result = $this->__invoke();
219-
220-
return new ArrayIterator($result instanceof Enumerable ? $result->all() : $result);
221-
}
222-
223-
public function __get($key)
224-
{
225-
return $this->__invoke()->{$key};
226-
}
227-
228-
public function __call($method, $parameters)
229-
{
230-
return $this->__invoke()->{$method}(...$parameters);
231-
}
232-
233-
public function __invoke()
234-
{
235-
return call_user_func($this->callable);
236-
}
237-
238-
public function __toString()
239-
{
240-
return (string) $this->__invoke();
241-
}
242-
243-
};
199+
});
244200
}
245201

246202
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Illuminate\View;
4+
5+
use ArrayIterator;
6+
use Closure;
7+
use Illuminate\Contracts\Support\DeferringDisplayableValue;
8+
use Illuminate\Support\Enumerable;
9+
use IteratorAggregate;
10+
11+
class InvokableComponentVariable implements DeferringDisplayableValue, IteratorAggregate
12+
{
13+
/**
14+
* The callable instance to resolve the variable value.
15+
*
16+
* @var \Closure
17+
*/
18+
protected $callable;
19+
20+
/**
21+
* Create a new variable instance.
22+
*
23+
* @param \Closure $callable
24+
* @return void
25+
*/
26+
public function __construct(Closure $callable)
27+
{
28+
$this->callable = $callable;
29+
}
30+
31+
/**
32+
* Resolve the displayable value that the class is deferring.
33+
*
34+
* @return \Illuminate\Contracts\Support\Htmlable|string
35+
*/
36+
public function resolveDisplayableValue()
37+
{
38+
return $this->__invoke();
39+
}
40+
41+
/**
42+
* Get an interator instance for the variable.
43+
*
44+
* @return \ArrayIterator
45+
*/
46+
public function getIterator()
47+
{
48+
$result = $this->__invoke();
49+
50+
return new ArrayIterator($result instanceof Enumerable ? $result->all() : $result);
51+
}
52+
53+
/**
54+
* Dynamically proxy attribute access to the variable.
55+
*
56+
* @param string $key
57+
* @return mixed
58+
*/
59+
public function __get($key)
60+
{
61+
return $this->__invoke()->{$key};
62+
}
63+
64+
/**
65+
* Dynamically proxy method access to the variable.
66+
*
67+
* @param string $method
68+
* @param array $parameters
69+
* @return mixed
70+
*/
71+
public function __call($method, $parameters)
72+
{
73+
return $this->__invoke()->{$method}(...$parameters);
74+
}
75+
76+
/**
77+
* Resolve the variable.
78+
*
79+
* @return mixed
80+
*/
81+
public function __invoke()
82+
{
83+
return call_user_func($this->callable);
84+
}
85+
86+
/**
87+
* Resolve the variable as a string.
88+
*
89+
* @return mixed
90+
*/
91+
public function __toString()
92+
{
93+
return (string) $this->__invoke();
94+
}
95+
}

0 commit comments

Comments
 (0)