Skip to content

Commit 443d392

Browse files
committed
Remove FoF Extend
1 parent d2f63b9 commit 443d392

File tree

3 files changed

+114
-4
lines changed

3 files changed

+114
-4
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"type": "flarum-extension",
88
"license": "MIT",
99
"require": {
10-
"flarum/core": ">=0.1.0-beta.16 <0.1.0-beta.17",
11-
"fof/extend": "^0.3.1"
10+
"flarum/core": "^1.0.0"
1211
},
1312
"authors": [
1413
{

extend.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Davwheat\CustomSidenavLinks;
1313

1414
use Flarum\Extend;
15-
use FoF\Extend\Extend as FoFExtend;
15+
use Davwheat\CustomSidenavLinks\Extend\ExtensionSettings;
1616

1717
return [
1818
(new Extend\Frontend('forum'))->js(__DIR__ . '/js/dist/forum.js')->css(__DIR__ . '/less/forum.less'),
@@ -21,7 +21,7 @@
2121

2222
new Extend\Locales(__DIR__ . '/locale'),
2323

24-
(new FoFExtend\ExtensionSettings())
24+
(new ExtensionSettings())
2525
->addKey('davwheat-custom-sidenav-links.link-data')
2626
->addKey('davwheat-custom-sidenav-links.position')
2727
->addKey('davwheat-custom-sidenav-links.top-spacer')

src/Extend/ExtensionSettings.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
/**
4+
* Copyright (c) 2021 David Wheatley
5+
* Copyright (c) 2019 FriendsOfFlarum
6+
*
7+
* For the full copyright and license information, please view the LICENSE.md
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Davwheat\CustomSidenavLinks\Extend;
12+
13+
use Flarum\Extend\ExtenderInterface;
14+
use Flarum\Extension\Extension;
15+
use Flarum\Frontend\Document;
16+
use Flarum\Frontend\Frontend;
17+
use Flarum\Settings\SettingsRepositoryInterface;
18+
use Illuminate\Contracts\Container\Container;
19+
20+
class ExtensionSettings implements ExtenderInterface
21+
{
22+
/**
23+
* @var string
24+
*/
25+
private $prefix = '';
26+
27+
/**
28+
* @var array
29+
*/
30+
private $keys = [];
31+
32+
/**
33+
* @var array
34+
*/
35+
private $defaults = [];
36+
37+
public function extend(Container $container, Extension $extension = null)
38+
{
39+
$container->resolving(
40+
'flarum.frontend.forum',
41+
function (Frontend $frontend, Container $container) {
42+
/** @var SettingsRepositoryInterface settings */
43+
$settings = $container->make(SettingsRepositoryInterface::class);
44+
45+
$frontend->content(function (Document $document) use ($settings) {
46+
foreach ($this->keys as $index => $key) {
47+
$document->payload[$key] = $settings->get($key, $this->defaults[$index]);
48+
}
49+
});
50+
}
51+
);
52+
}
53+
54+
/**
55+
* Set extension keys prefix.
56+
*
57+
* @param string $prefix
58+
*
59+
* @return self
60+
*/
61+
public function setPrefix($prefix): self
62+
{
63+
$this->prefix = $prefix;
64+
65+
return $this;
66+
}
67+
68+
/**
69+
* Add setting key.
70+
*
71+
* @param string $key
72+
* @param string|null $default
73+
*
74+
* @return self
75+
*/
76+
public function addKey($key, $default = null): self
77+
{
78+
$this->addKeys([$key => $default]);
79+
80+
return $this;
81+
}
82+
83+
/**
84+
* Add multiple setting keys. Supports a callable.
85+
*
86+
* @param array|callable $input
87+
*
88+
* @return self
89+
*/
90+
public function addKeys($input): self
91+
{
92+
if (is_callable($input)) {
93+
$input = (array) app()->call($input);
94+
}
95+
96+
$keys = array_keys($input);
97+
$values = array_values($input);
98+
99+
foreach ($keys as $index => $key) {
100+
if (is_numeric($key)) {
101+
$this->keys[] = $this->prefix . $values[$index];
102+
$this->defaults[] = null;
103+
} else {
104+
$this->keys[] = $this->prefix . $key;
105+
$this->defaults[] = $values[$index];
106+
}
107+
}
108+
109+
return $this;
110+
}
111+
}

0 commit comments

Comments
 (0)