1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Softius\ResourcesResolver; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class DefaultCallableStrategy |
7
|
|
|
* @package Softius\ResourcesResolver |
8
|
|
|
*/ |
9
|
|
|
class DefaultCallableStrategy implements ResolvableInterface |
10
|
|
|
{ |
11
|
|
|
const DEFAULT_NAMESPACE_SEPARATOR = '\\'; |
12
|
|
|
const DEFAULT_METHOD_SEPARATOR = '::'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var null|string |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
private $namespace_separator; |
19
|
|
|
/** |
20
|
|
|
* @var null|string |
21
|
|
|
*/ |
22
|
|
|
private $method_separator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* DefaultCallableStrategy constructor. |
26
|
|
|
* @param string $namespace_separator |
27
|
|
|
* @param string $method_separator |
28
|
|
|
*/ |
29
|
|
|
public function __construct($namespace_separator = null, $method_separator = null) { |
30
|
|
|
$this->namespace_separator = ($namespace_separator === null) ? self::DEFAULT_NAMESPACE_SEPARATOR : $namespace_separator; |
31
|
|
|
$this->method_separator = ($method_separator === null) ? self::DEFAULT_METHOD_SEPARATOR : $method_separator; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $in |
36
|
|
|
* @return array |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
|
|
public function resolve($in) |
40
|
|
|
{ |
41
|
|
|
$pos = strrpos($in, $this->method_separator); |
42
|
|
|
if ($pos === false) { |
43
|
|
|
throw new \Exception(sprintf('Method separator not found in %s', $in)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($pos === 0) { |
47
|
|
|
// Use backtrace to find the calling Class |
48
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); |
49
|
|
|
$class = $trace[2]['class']; |
50
|
|
|
$method = substr($in, $pos + strlen($this->method_separator)); |
51
|
|
|
} else { |
52
|
|
|
$class = substr($in, 0, $pos); |
53
|
|
|
$method = substr($in, $pos + strlen($this->method_separator)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return [$class, $method]; |
57
|
|
|
} |
58
|
|
|
} |