-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPlugin.php
196 lines (172 loc) · 5.79 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace Winter\Docs;
use Cms\Classes\Page;
use Cms\Classes\Theme;
use System\Classes\PluginBase;
use Winter\Docs\Classes\DocsManager;
use Winter\Docs\Classes\MarkdownDocumentation;
use Winter\Docs\Classes\MarkdownPageIndex;
use Winter\Docs\Classes\PHPApiPageIndex;
use Winter\Storm\Support\Str;
use Winter\Storm\Support\Facades\Event;
/**
* Docs plugin.
*
* Comprehensive documentation suite for Winter CMS. Allows for the quick generation of docs from Markdown, or a PHP
* API.
*
* @author Ben Thomson <[email protected]>
* @package winter/wn-docs-plugin
*/
class Plugin extends PluginBase
{
/**
* {@inheritDoc}
*/
public function pluginDetails(): array
{
return [
'name' => 'winter.docs::lang.plugin.name',
'description' => 'winter.docs::lang.plugin.description',
'author' => 'Winter CMS',
'icon' => 'icon-tags',
'homepage' => 'https://github.com/wintercms/wn-docs-plugin',
'replaces' => 'RainLab.Docs',
];
}
/**
* {@inheritDoc}
*/
public function registerComponents(): array
{
return [
\Winter\Docs\Components\DocsPage::class => 'docsPage',
\Winter\Docs\Components\DocsList::class => 'docsList',
];
}
/**
* {@inheritDoc}
*/
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->registerCommands();
$this->registerPublishedConfig();
} else {
$this->extendWinterPagesPlugin();
$this->extendWinterSitemapPlugin();
}
// Extend mirror paths to mirror assets
Event::listen('system.console.mirror.extendPaths', function ($paths) {
$paths->wildcards = array_merge($paths->wildcards, [
'storage/app/docs/processed/*/_assets',
]);
});
}
/**
* Register commands.
*/
protected function registerCommands(): void
{
$this->commands([
\Winter\Docs\Console\DocsList::class,
\Winter\Docs\Console\DocsProcess::class,
]);
}
/**
* Register published configurations.
*/
protected function registerPublishedConfig(): void
{
$this->publishes([
__DIR__ . '/config/storage.php' => implode(DIRECTORY_SEPARATOR, [
$this->app->configPath(),
'winter',
'docs',
'storage.php',
])
]);
}
/**
* Register search handlers when the Winter.Search plugin is installed.
*
* These search handlers automatically allow searching of any registered and processed docs.
*/
public function registerSearchHandlers(): array
{
$handlers = [];
$docs = DocsManager::instance()->listDocumentation();
foreach ($docs as $doc) {
if (!$doc['instance']->isProcessed()) {
continue;
}
// Find page connected to this documentation.
$theme = Theme::getActiveTheme();
$page = Page::inTheme($theme)->whereComponent('docsPage', 'docId', $doc['id'])->first();
$handlers['docs.' . $doc['id']] = [
'name' => $doc['name'],
'model' => function () use ($doc) {
if ($doc['instance'] instanceof MarkdownDocumentation) {
MarkdownPageIndex::setPageList($doc['instance']->getPageList());
return new MarkdownPageIndex();
}
PHPApiPageIndex::setPageList($doc['instance']->getPageList());
return new PHPApiPageIndex();
},
'record' => function ($record, $query) use ($page) {
$excerpt = Str::excerpt($record->content, $query);
if (is_null($excerpt)) {
if (Str::length($record->content) > 200) {
$excerpt = Str::substr($record->content, 0, 200) . '...';
} else {
$excerpt = $record->content;
}
}
return [
'group' => ($record->group_1 ?? null),
'label' => (!empty($record->group_3)) ? ($record->group_2 ?? null) : null,
'title' => $record->title,
'description' => $excerpt,
'url' => Page::url($page->getBaseFileName(), ['slug' => $record->path]),
];
},
];
}
return $handlers;
}
/**
* Extends the Winter.Pages plugin
*/
protected function extendWinterPagesPlugin(): void
{
/*
* Register menu items for the Winter.Pages plugin
*/
Event::listen('pages.menuitem.listTypes', function () {
return [
'docs' => 'winter.docs::lang.menuitem.docs',
'docs-page' => 'winter.docs::lang.menuitem.docs-page',
];
});
Event::listen('pages.menuitem.getTypeInfo', function ($type) {
return DocsManager::getMenuTypeInfo($type);
});
Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) {
return DocsManager::resolveMenuItem($type, $item, $url, $theme);
});
}
/**
* Extends the Winter.Sitemap plugin
*/
protected function extendWinterSitemapPlugin(): void
{
Event::listen('winter.sitemap.beforeAddItem', function ($item, array $itemInfo) {
if (
$item->type === 'docs'
&& ($itemInfo['external'] ?? false)
) {
return false;
}
});
}
}