|
| 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