Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit dc4d980

Browse files
committed
M structure navigation
1 parent 1aa961c commit dc4d980

File tree

3 files changed

+127
-6
lines changed

3 files changed

+127
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Spiral\NavigationBuilder\Builders;
4+
5+
use Spiral\NavigationBuilder\Services\LinkWrapper;
6+
7+
class StructureBuilder extends TreeBuilder
8+
{
9+
/**
10+
* @param array $data
11+
* @return array
12+
*/
13+
protected function packRow(array $data): array
14+
{
15+
return parent::packRow($data) + [
16+
'depth' => $data['depth'],
17+
'text' => $data['link']['text'],
18+
'href' => $data['link']['href'],
19+
'attributes' => LinkWrapper::unpackAttributes($data['link']['attributes']),
20+
];
21+
}
22+
23+
/**
24+
* @param array $data
25+
* @return array
26+
*/
27+
protected function packLink(array $data): array
28+
{
29+
return parent::packLink($data) + [
30+
'depth' => $data['depth'],
31+
'text' => $data['text'],
32+
'href' => $data['href'],
33+
'attributes' => $data['attributes'],
34+
];
35+
}
36+
37+
/**
38+
* @param string $domain
39+
* @return array
40+
*/
41+
protected function getTree(string $domain): array
42+
{
43+
return $this->source->findDomainTree($domain)->orderBy('order', 'ASC')->fetchData();
44+
}
45+
}

source/NavigationBuilder/Navigation.php

+45-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Spiral\NavigationBuilder;
44

55
use Spiral\NavigationBuilder\Builders\HtmlBuilder;
6+
use Spiral\NavigationBuilder\Builders\StructureBuilder;
67
use Spiral\NavigationBuilder\Builders\TreeBuilder;
78

89
/**
@@ -23,6 +24,9 @@ class Navigation
2324
/** @var HtmlBuilder */
2425
private $htmlBuilder;
2526

27+
/** @var StructureBuilder */
28+
private $structureBuilder;
29+
2630
/** @var RendererInterface */
2731
private $renderer;
2832

@@ -34,29 +38,36 @@ class Navigation
3438
*
3539
* @param TreeBuilder $treeBuilder
3640
* @param HtmlBuilder $htmlBuilder
41+
* @param StructureBuilder $structureBuilder
3742
* @param RendererInterface $renderer
3843
* @param Storage $storage
3944
*/
4045
public function __construct(
4146
TreeBuilder $treeBuilder,
4247
HtmlBuilder $htmlBuilder,
48+
StructureBuilder $structureBuilder,
4349
RendererInterface $renderer,
4450
Storage $storage
4551
) {
4652
$this->treeBuilder = $treeBuilder;
4753
$this->htmlBuilder = $htmlBuilder;
54+
$this->structureBuilder = $structureBuilder;
4855
$this->storage = $storage;
4956

5057
$this->setRenderer($renderer);
5158
}
5259

5360
/**
5461
* @param RendererInterface $renderer
62+
* @return Navigation
5563
*/
56-
public function setRenderer(RendererInterface $renderer)
64+
public function setRenderer(RendererInterface $renderer): Navigation
5765
{
58-
$this->renderer = $renderer;
59-
$this->htmlBuilder->setRenderer($renderer);
66+
$navigation = clone $this;
67+
$navigation->renderer = $renderer;
68+
$navigation->htmlBuilder->setRenderer($renderer);
69+
70+
return $navigation;
6071
}
6172

6273
/**
@@ -108,6 +119,25 @@ public function getTree(string $domain, bool $cache = true): array
108119
return $tree;
109120
}
110121

122+
/**
123+
* @param string $domain
124+
* @param bool $cache
125+
* @return array
126+
*/
127+
public function getStructure(string $domain, bool $cache = true): array
128+
{
129+
if (empty($cache)) {
130+
return $this->structureBuilder->build($domain);
131+
}
132+
133+
$structure = $this->storage->getStructureCache($domain);
134+
if (empty($structure)) {
135+
$structure = $this->buildAndCacheTree($domain);
136+
}
137+
138+
return $structure;
139+
}
140+
111141
/**
112142
* @param string $domain
113143
* @return array
@@ -120,6 +150,18 @@ protected function buildAndCacheTree(string $domain): array
120150
return $tree;
121151
}
122152

153+
/**
154+
* @param string $domain
155+
* @return array
156+
*/
157+
protected function buildAndCacheStructure(string $domain): array
158+
{
159+
$structure = $this->structureBuilder->build($domain);
160+
$this->storage->setStructureCache($domain, $structure);
161+
162+
return $structure;
163+
}
164+
123165
/**
124166
* @param string $domain
125167
* @return string

source/NavigationBuilder/Storage.php

+37-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
*/
1212
class Storage
1313
{
14-
const TREE_CACHE = 'navigation::tree';
15-
const HTML_CACHE = 'navigation::html';
16-
const LIFETIME = 86400 * 365 * 10; // 10 years is enough
14+
const TREE_CACHE = 'navigation::tree';
15+
const STRUCTURE_CACHE = 'navigation::structure';
16+
const HTML_CACHE = 'navigation::html';
17+
const LIFETIME = 86400 * 365 * 10; // 10 years is enough
1718

1819
/** @var CacheInterface */
1920
private $cache;
@@ -62,6 +63,28 @@ public function setTreeCache(string $domain, array $data)
6263
$this->cache->set($this->treeCache($domain), $data, self::LIFETIME);
6364
}
6465

66+
/**
67+
* Get domain structure cache. Supposed to be array.
68+
*
69+
* @param string $domain
70+
* @return mixed
71+
*/
72+
public function getStructureCache(string $domain)
73+
{
74+
return $this->cache->get($this->structureCache($domain), []);
75+
}
76+
77+
/**
78+
* Set domain structure cache.
79+
*
80+
* @param string $domain
81+
* @param array $data
82+
*/
83+
public function setStructureCache(string $domain, array $data)
84+
{
85+
$this->cache->set($this->structureCache($domain), $data, self::LIFETIME);
86+
}
87+
6588
/**
6689
* Get domain html cache. Supposed to be string.
6790
*
@@ -97,6 +120,17 @@ protected function treeCache(string $domain): string
97120
return self::TREE_CACHE . '::' . $domain;
98121
}
99122

123+
/**
124+
* Tree cache name.
125+
*
126+
* @param string $domain
127+
* @return string
128+
*/
129+
protected function structureCache(string $domain): string
130+
{
131+
return self::STRUCTURE_CACHE . '::' . $domain;
132+
}
133+
100134
/**
101135
* HTML cache name (used renderer).
102136
*

0 commit comments

Comments
 (0)