1
|
|
|
<?php |
2
|
|
|
namespace DNADesign\Elemental\Services; |
3
|
|
|
|
4
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use LogicException; |
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
use SilverStripe\Core\ClassInfo; |
9
|
|
|
use SilverStripe\Core\Injector\Injectable; |
10
|
|
|
use SilverStripe\Core\Injector\Injector; |
11
|
|
|
use SilverStripe\GraphQL\Scaffolding\StaticSchema; |
|
|
|
|
12
|
|
|
|
13
|
|
|
class ElementTypeRegistry |
14
|
|
|
{ |
15
|
|
|
use Injectable; |
16
|
|
|
|
17
|
|
|
const CACHE_KEY = 'element-types'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected static $elementTypes; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var CacheInterface |
26
|
|
|
*/ |
27
|
|
|
protected static $cache; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ElementTypeRegistry constructor. |
31
|
|
|
*/ |
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->generateRegistry(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function registerElement($elementClass) |
38
|
|
|
{ |
39
|
|
|
$singleton = singleton($elementClass); |
40
|
|
|
|
41
|
|
|
if (!$singleton instanceof BaseElement) { |
42
|
|
|
throw new LogicException('Only elements that extend ' . BaseElement::class . ' can be registered'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
static::$elementTypes[$elementClass] = [ |
46
|
|
|
'icon' => $singleton::config()->get('icon'), |
47
|
|
|
'name' => str_replace('\\', '-', $elementClass), |
48
|
|
|
'title' => $singleton->getType(), |
49
|
|
|
'inlineEditable' => $singleton->inlineEditable(), |
50
|
|
|
'editTabs' => $this->getTabProvider()->getTabsForElement($elementClass), |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getDefinitions() |
55
|
|
|
{ |
56
|
|
|
return static::$elementTypes; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getDefinition($instanceOrClass) |
60
|
|
|
{ |
61
|
|
|
if ($instanceOrClass instanceof BaseElement) { |
62
|
|
|
$instanceOrClass = get_class($instanceOrClass); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!is_string($instanceOrClass)) { |
66
|
|
|
throw new InvalidArgumentException(sprintf( |
67
|
|
|
'Given argument to %s is not an instance of a class extending %s and is not a string', |
68
|
|
|
__METHOD__, |
69
|
|
|
BaseElement::class |
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$definitions = $this->getDefinitions(); |
74
|
|
|
|
75
|
|
|
if (!isset($definitions[$instanceOrClass])) { |
76
|
|
|
throw new InvalidArgumentException(sprintf('Unknown element "%s"', $instanceOrClass)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $definitions[$instanceOrClass]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return ElementTabProvider |
84
|
|
|
*/ |
85
|
|
|
protected function getTabProvider() |
86
|
|
|
{ |
87
|
|
|
// This is a temporary solution until something client side is implemented to reveal tab names. |
88
|
|
|
return Injector::inst()->get(ElementTabProvider::class); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function generateRegistry() |
92
|
|
|
{ |
93
|
|
|
if (is_array(static::$elementTypes)) { |
|
|
|
|
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Look in a cache (if provided) for type details |
98
|
|
|
if (static::$cache && ($types = static::$cache->get(self::CACHE_KEY))) { |
99
|
|
|
static::$elementTypes = $types; |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Find all element types |
104
|
|
|
$classNames = ClassInfo::getValidSubClasses(BaseElement::class); |
105
|
|
|
foreach ($classNames as $class) { |
106
|
|
|
if ($class === BaseElement::class) { |
107
|
|
|
continue; |
108
|
|
|
} |
109
|
|
|
$this->registerElement($class); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths