1
|
|
|
<?php |
2
|
|
|
namespace DNADesign\Elemental\Services; |
3
|
|
|
|
4
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
5
|
|
|
use Psr\SimpleCache\CacheInterface; |
6
|
|
|
use SilverStripe\Core\ClassInfo; |
7
|
|
|
use SilverStripe\Core\Config\Configurable; |
8
|
|
|
use SilverStripe\Core\Flushable; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\Forms\TabSet; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Provides top-level CMS field tab names for any element that extends BaseElement |
14
|
|
|
* |
15
|
|
|
* Note that this will be replaced by GraphQL form schema caching (see |
16
|
|
|
* http://github.com/silverstripe/silverstripe-admin/issues/627 ). This service may be removed without warning in a |
17
|
|
|
* minor release. |
18
|
|
|
* |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
class ElementTabProvider implements Flushable |
22
|
|
|
{ |
23
|
|
|
use Configurable; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set to true to refresh the tab cache on flush |
27
|
|
|
* |
28
|
|
|
* @config |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private static $regenerate_on_flush = false; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var CacheInterface |
35
|
|
|
*/ |
36
|
|
|
protected $cache; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the top level tab names for the given element class |
40
|
|
|
* |
41
|
|
|
* @param string $elementClass |
42
|
|
|
* @return array Array of the tabs for the element |
43
|
|
|
*/ |
44
|
|
|
public function getTabsForElement($elementClass) |
45
|
|
|
{ |
46
|
|
|
if (null !== ($tabs = $this->getCache()->get($this->getCacheKey($elementClass)))) { |
47
|
|
|
return $tabs; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->generateTabsForElement($elementClass); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This function is triggered early in the request if the "flush" query |
55
|
|
|
* parameter has been set. Each class that implements Flushable implements |
56
|
|
|
* this function which looks after it's own specific flushing functionality. |
57
|
|
|
* |
58
|
|
|
* @see FlushMiddleware |
59
|
|
|
*/ |
60
|
|
|
public static function flush() |
61
|
|
|
{ |
62
|
|
|
/** @var static $self */ |
63
|
|
|
$self = singleton(static::class); |
64
|
|
|
|
65
|
|
|
$self->getCache()->clear(); |
66
|
|
|
|
67
|
|
|
if ($self->config()->get('regenerate_on_flush')) { |
68
|
|
|
$self->generateAllTabs(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param CacheInterface $cache |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setCache($cache) |
77
|
|
|
{ |
78
|
|
|
$this->cache = $cache; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return CacheInterface |
84
|
|
|
*/ |
85
|
|
|
protected function getCache() |
86
|
|
|
{ |
87
|
|
|
return $this->cache; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Identify and regenerate all tab names for all elemental blocks (and cache them) |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
protected function generateAllTabs() |
96
|
|
|
{ |
97
|
|
|
foreach (ClassInfo::subclassesFor(BaseElement::class) as $class) { |
98
|
|
|
$this->generateTabsForElement($class); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Generate top level tab names for the given element class (and cache them) |
104
|
|
|
* |
105
|
|
|
* @param string $elementClass |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
protected function generateTabsForElement($elementClass) |
109
|
|
|
{ |
110
|
|
|
// Create the specified element |
111
|
|
|
/** @var BaseElement $element */ |
112
|
|
|
$element = Injector::inst()->create($elementClass); |
113
|
|
|
|
114
|
|
|
// Generate CMS fields and grab the "Root" tabset. |
115
|
|
|
/** @var TabSet $tabset */ |
116
|
|
|
$tabset = $element->getCMSFields()->fieldByName('Root'); |
117
|
|
|
|
118
|
|
|
// Get and map (ID => name) the tab names |
119
|
|
|
$tabs = $tabset->Tabs()->map()->toArray(); |
120
|
|
|
|
121
|
|
|
// Cache them for next time |
122
|
|
|
$this->getCache()->set($this->getCacheKey($elementClass), $tabs); |
123
|
|
|
|
124
|
|
|
return $tabs; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Generate a valid cache key from the given element class. |
129
|
|
|
* |
130
|
|
|
* @param string $className |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
protected function getCacheKey($className) |
134
|
|
|
{ |
135
|
|
|
return 'Tabs.' . str_replace(['\\'], '-', $className); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|