1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Respect\Rest package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Respect\Rest\Routes; |
10
|
|
|
|
11
|
|
|
use ReflectionMethod; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use Respect\Rest\Routable; |
14
|
|
|
|
15
|
|
|
class Factory extends AbstractRoute |
16
|
|
|
{ |
17
|
|
|
public $class = ''; |
18
|
|
|
protected $instance = null; |
19
|
|
|
public $factory = null; |
20
|
|
|
|
21
|
|
|
/** @var ReflectionMethod */ |
22
|
|
|
protected $reflection; |
23
|
|
|
|
24
|
3 |
|
public function __construct($method, $pattern, $class, $factory) |
25
|
|
|
{ |
26
|
3 |
|
$this->factory = $factory; |
27
|
3 |
|
$this->class = $class; |
28
|
3 |
|
parent::__construct($method, $pattern); |
29
|
3 |
|
} |
30
|
|
|
|
31
|
1 |
|
public function getReflection($method) |
32
|
|
|
{ |
33
|
1 |
|
if (empty($this->reflection)) { |
34
|
1 |
|
$this->reflection = new ReflectionMethod( |
35
|
1 |
|
$this->class, |
36
|
|
|
$method |
37
|
1 |
|
); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
1 |
|
return $this->reflection; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public function runTarget($method, &$params) |
44
|
|
|
{ |
45
|
1 |
|
if (is_null($this->instance)) { |
46
|
1 |
|
$this->instance = call_user_func_array($this->factory, array($method, &$params)); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
if (!$this->instance instanceof Routable) { |
50
|
|
|
throw new InvalidArgumentException('Routed classes must implement the Respect\\Rest\\Routable interface'); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return call_user_func_array( |
54
|
1 |
|
array($this->instance, $method), |
55
|
|
|
$params |
56
|
1 |
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|