1
|
|
|
<?php namespace Anomaly\Streams\Platform\Support; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\AddonCollection; |
4
|
|
|
use Illuminate\Contracts\Config\Repository; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Locator |
8
|
|
|
* |
9
|
|
|
* @link http://pyrocms.com/ |
10
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
11
|
|
|
* @author Ryan Thompson <[email protected]> |
12
|
|
|
* @package Anomaly\Streams\Platform\Support |
13
|
|
|
*/ |
14
|
|
|
class Locator |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The addon collection. |
19
|
|
|
* |
20
|
|
|
* @var AddonCollection |
21
|
|
|
*/ |
22
|
|
|
protected $addons; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The config repository. |
26
|
|
|
* |
27
|
|
|
* @var Repository |
28
|
|
|
*/ |
29
|
|
|
protected $config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new Locator instance. |
33
|
|
|
* |
34
|
|
|
* @param AddonCollection $addons |
35
|
|
|
* @param Repository $config |
36
|
|
|
*/ |
37
|
|
|
public function __construct(AddonCollection $addons, Repository $config) |
38
|
|
|
{ |
39
|
|
|
$this->addons = $addons; |
40
|
|
|
$this->config = $config; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Locate the addon containing an object. |
45
|
|
|
* Returns the addon's dot namespace. |
46
|
|
|
* |
47
|
|
|
* @param $object |
48
|
|
|
* @return null|string |
49
|
|
|
*/ |
50
|
|
|
public function locate($object) |
51
|
|
|
{ |
52
|
|
|
if (!is_object($object)) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$class = explode('\\', get_class($object)); |
57
|
|
|
|
58
|
|
|
$vendor = snake_case(array_shift($class)); |
59
|
|
|
$addon = snake_case(array_shift($class)); |
60
|
|
|
|
61
|
|
|
foreach ($this->config->get('streams::addons.types') as $type) { |
62
|
|
|
if (ends_with($addon, $type)) { |
63
|
|
|
|
64
|
|
|
$addon = str_replace('_' . $type, '', $addon); |
65
|
|
|
|
66
|
|
|
$namespace = "{$vendor}.{$type}.{$addon}"; |
67
|
|
|
|
68
|
|
|
return $this->addons->has($namespace) ? $namespace : null; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return the located addon instance. |
77
|
|
|
* |
78
|
|
|
* @param $object |
79
|
|
|
* @return \Anomaly\Streams\Platform\Addon\Addon|mixed|null |
80
|
|
|
*/ |
81
|
|
|
public function resolve($object) |
82
|
|
|
{ |
83
|
|
|
if (!$namespace = $this->locate($object)) { |
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->addons->get($namespace); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|