1 | <?php |
||||
2 | |||||
3 | namespace SleepingOwl\Admin\Wysiwyg; |
||||
4 | |||||
5 | use Illuminate\Config\Repository; |
||||
6 | use Illuminate\Contracts\Foundation\Application; |
||||
7 | use Illuminate\Support\Collection; |
||||
8 | use SleepingOwl\Admin\Contracts\Wysiwyg\WysiwygEditorInterface; |
||||
9 | use SleepingOwl\Admin\Contracts\Wysiwyg\WysiwygFilterInterface; |
||||
10 | use SleepingOwl\Admin\Contracts\Wysiwyg\WysiwygMangerInterface; |
||||
11 | use SleepingOwl\Admin\Exceptions\WysiwygException; |
||||
12 | |||||
13 | class Manager implements WysiwygMangerInterface |
||||
14 | { |
||||
15 | /** |
||||
16 | * @var |
||||
17 | */ |
||||
18 | protected $config; |
||||
19 | |||||
20 | /** |
||||
21 | * Available wysiwyg editors. |
||||
22 | * |
||||
23 | * @var Collection|WysiwygEditorInterface[] |
||||
24 | */ |
||||
25 | protected $filters; |
||||
26 | |||||
27 | /** |
||||
28 | * @var Application |
||||
29 | */ |
||||
30 | protected $app; |
||||
31 | |||||
32 | /** |
||||
33 | * Manager constructor. |
||||
34 | * |
||||
35 | * @param Application $application |
||||
36 | */ |
||||
37 | 285 | public function __construct(Application $application) |
|||
38 | { |
||||
39 | 285 | $this->filters = new Collection(); |
|||
40 | 285 | $this->app = $application; |
|||
41 | |||||
42 | 285 | $this->config = new Repository( |
|||
43 | 285 | $this->app['config']->get('sleeping_owl.wysiwyg', []) |
|||
44 | 285 | ); |
|||
45 | 285 | } |
|||
46 | |||||
47 | /** |
||||
48 | * @return string|null |
||||
49 | */ |
||||
50 | public function getDefaultEditorId() |
||||
51 | { |
||||
52 | return $this->config->get('default', 'ckeditor'); |
||||
53 | } |
||||
54 | |||||
55 | /** |
||||
56 | * @param string $editorId |
||||
57 | * @param WysiwygFilterInterface|null $filter |
||||
58 | * @param string|null $name |
||||
59 | * |
||||
60 | * @return WysiwygEditorInterface |
||||
61 | */ |
||||
62 | 285 | public function register($editorId, WysiwygFilterInterface $filter = null, $name = null) |
|||
63 | { |
||||
64 | 285 | $this->getFilters()->push( |
|||
65 | 285 | $editor = new Editor($editorId, $name, $filter, $this->config->get($editorId, [])) |
|||
66 | 285 | ); |
|||
67 | |||||
68 | 285 | return $editor; |
|||
69 | } |
||||
70 | |||||
71 | /** |
||||
72 | * @return Collection|WysiwygEditorInterface[] |
||||
73 | */ |
||||
74 | 285 | public function getFilters() |
|||
75 | { |
||||
76 | 285 | return $this->filters; |
|||
77 | } |
||||
78 | |||||
79 | /** |
||||
80 | * @param string $editorId |
||||
81 | * |
||||
82 | * @return WysiwygEditorInterface|null |
||||
83 | */ |
||||
84 | public function getEditor($editorId) |
||||
85 | { |
||||
86 | return $this->getFilters()->filter(function (WysiwygEditorInterface $editor) use ($editorId) { |
||||
87 | return $editor->getId() == $editorId; |
||||
88 | })->first(); |
||||
89 | } |
||||
90 | |||||
91 | public function loadDefaultEditor() |
||||
92 | { |
||||
93 | $this->loadEditor($this->getDefaultEditor()); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
94 | } |
||||
95 | |||||
96 | /** |
||||
97 | * @param string $editorId |
||||
98 | * |
||||
99 | * @return bool |
||||
100 | */ |
||||
101 | public function loadEditor($editorId) |
||||
102 | { |
||||
103 | if (! is_null($editor = $this->getEditor($editorId))) { |
||||
104 | if ($editor->isUsed()) { |
||||
105 | return true; |
||||
106 | } |
||||
107 | |||||
108 | return $editor->load(); |
||||
0 ignored issues
–
show
Are you sure the usage of
$editor->load() targeting SleepingOwl\Admin\Contra...EditorInterface::load() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
109 | } |
||||
110 | |||||
111 | return false; |
||||
112 | } |
||||
113 | |||||
114 | /** |
||||
115 | * @param string $editorId |
||||
116 | * @param string $text |
||||
117 | * |
||||
118 | * @return string string |
||||
119 | * @throws WysiwygException |
||||
120 | */ |
||||
121 | public function applyFilter($editorId, $text) |
||||
122 | { |
||||
123 | if (! is_null($editor = $this->getEditor($editorId))) { |
||||
124 | return $editor->applyFilter($text); |
||||
125 | } |
||||
126 | |||||
127 | throw new WysiwygException("Editor [{$editorId}] not found"); |
||||
128 | } |
||||
129 | |||||
130 | /** |
||||
131 | * @return array |
||||
132 | */ |
||||
133 | public function getFiltersList() |
||||
134 | { |
||||
135 | return $this->getFilters()->pluck('name', 'id')->all(); |
||||
136 | } |
||||
137 | |||||
138 | /** |
||||
139 | * @return WysiwygEditorInterface |
||||
140 | */ |
||||
141 | protected function getDefaultEditor() |
||||
142 | { |
||||
143 | return $this->getEditor($this->getDefaultEditor()); |
||||
0 ignored issues
–
show
$this->getDefaultEditor() of type SleepingOwl\Admin\Contra...\WysiwygEditorInterface is incompatible with the type string expected by parameter $editorId of SleepingOwl\Admin\Wysiwyg\Manager::getEditor() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
144 | } |
||||
145 | } |
||||
146 |