1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of laravel.su package. |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace App\Services\ContentRenderer; |
10
|
|
|
|
11
|
|
|
use Illuminate\Config\Repository; |
12
|
|
|
use Illuminate\Contracts\Container\Container; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class RenderersRepository. |
16
|
|
|
*/ |
17
|
|
|
class RenderersRepository extends Repository |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $default; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Container |
26
|
|
|
*/ |
27
|
|
|
private $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* RenderersRepository constructor. |
31
|
|
|
* @param Container $container |
32
|
|
|
* @param array $items |
33
|
|
|
* @throws \InvalidArgumentException |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Container $container, array $items = []) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($items); |
38
|
|
|
|
39
|
|
|
$this->default = $this->getRendererClass($this->get('default')); |
40
|
|
|
$this->container = $container; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $name |
45
|
|
|
* @return string |
46
|
|
|
* @throws \InvalidArgumentException |
47
|
|
|
*/ |
48
|
|
|
private function getRendererClass(string $name): string |
49
|
|
|
{ |
50
|
|
|
$class = $this->get(sprintf('renderers.%s', $name)); |
51
|
|
|
|
52
|
|
|
if ($class === null) { |
53
|
|
|
throw new \InvalidArgumentException("Renderer ${name} not found"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $class; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string|null $name |
61
|
|
|
* @return ContentRenderInterface |
62
|
|
|
* @throws \InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
public function getRenderer(string $name = null): ContentRenderInterface |
65
|
|
|
{ |
66
|
|
|
$class = $name === null ? $this->default : $this->getRendererClass($name); |
67
|
|
|
|
68
|
|
|
return $this->container->make($class); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return ContentRenderInterface |
73
|
|
|
*/ |
74
|
|
|
public function getDefaultRenderer(): ContentRenderInterface |
75
|
|
|
{ |
76
|
|
|
return $this->container->make($this->default); |
77
|
|
|
} |
78
|
|
|
} |