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 tab names for |
14
|
|
|
*/ |
15
|
|
|
class ElementTabProvider implements Flushable |
16
|
|
|
{ |
17
|
|
|
use Configurable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set to true to refresh the tab cache on flush |
21
|
|
|
* |
22
|
|
|
* @config |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private static $regenerate_on_flush = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var CacheInterface |
29
|
|
|
*/ |
30
|
|
|
protected $cache; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ElementTabProvider constructor. |
34
|
|
|
* @param CacheInterface $cache |
35
|
|
|
*/ |
36
|
|
|
public function __construct(CacheInterface $cache = null) |
37
|
|
|
{ |
38
|
|
|
if (!$cache) { |
39
|
|
|
$cache = Injector::inst()->get(CacheInterface::class . '.ElementTabCache'); |
40
|
|
|
} |
41
|
|
|
$this->cache = $cache; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the top level tab names for the given element class |
46
|
|
|
* |
47
|
|
|
* @param string $elementClass |
48
|
|
|
* @return array Array of the tabs for the element |
49
|
|
|
*/ |
50
|
|
|
public function getTabsForElement($elementClass) |
51
|
|
|
{ |
52
|
|
|
if (null !== ($tabs = $this->cache->get($this->getCacheKey($elementClass)))) { |
53
|
|
|
return $tabs; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->generateTabsForElement($elementClass); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* This function is triggered early in the request if the "flush" query |
61
|
|
|
* parameter has been set. Each class that implements Flushable implements |
62
|
|
|
* this function which looks after it's own specific flushing functionality. |
63
|
|
|
* |
64
|
|
|
* @see FlushMiddleware |
65
|
|
|
*/ |
66
|
|
|
public static function flush() |
67
|
|
|
{ |
68
|
|
|
/** @var static $self */ |
69
|
|
|
$self = singleton(static::class); |
70
|
|
|
|
71
|
|
|
$self->cache->clear(); |
72
|
|
|
|
73
|
|
|
if (self::config()->regenerate_on_flush) { |
74
|
|
|
$self->generateAllTabs(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Identify and regenerate all tab names for all elemental blocks (and cache them) |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
protected function generateAllTabs() |
84
|
|
|
{ |
85
|
|
|
foreach (ClassInfo::subclassesFor(BaseElement::class) as $class) { |
86
|
|
|
$this->generateTabsForElement($class); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Generate top level tab names for the given element class (and cache them) |
92
|
|
|
* |
93
|
|
|
* @param string $elementClass |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function generateTabsForElement($elementClass) |
97
|
|
|
{ |
98
|
|
|
// We set an ID because some tabs only show if ->isInDb() is true |
99
|
|
|
/** @var BaseElement $element */ |
100
|
|
|
$element = Injector::inst()->create($elementClass); |
101
|
|
|
$element->ID = 1; |
102
|
|
|
|
103
|
|
|
/** @var TabSet $tabset */ |
104
|
|
|
$tabset = $element->getCMSFields()->fieldByName('Root'); |
105
|
|
|
|
106
|
|
|
$tabs = $tabset->Tabs()->map()->toArray(); |
107
|
|
|
|
108
|
|
|
$this->cache->set($this->getCacheKey($elementClass), $tabs); |
109
|
|
|
|
110
|
|
|
return $tabs; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Generate a valid cache key from the given element class. |
115
|
|
|
* |
116
|
|
|
* @param string $className |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
protected function getCacheKey($className) |
120
|
|
|
{ |
121
|
|
|
return 'Tabs.' . str_replace(['\\'], '-', $className); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|