1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webperf plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Monitor the performance of your webpages through real-world user timing data |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\webperf\controllers; |
12
|
|
|
|
13
|
|
|
use nystudio107\webperf\Webperf; |
14
|
|
|
use nystudio107\webperf\assetbundles\webperf\WebperfDashboardAsset; |
15
|
|
|
use nystudio107\webperf\helpers\MultiSite as MultiSiteHelper; |
16
|
|
|
use nystudio107\webperf\helpers\Permission as PermissionHelper; |
17
|
|
|
|
18
|
|
|
use Craft; |
|
|
|
|
19
|
|
|
use craft\helpers\UrlHelper; |
|
|
|
|
20
|
|
|
use craft\web\Controller; |
|
|
|
|
21
|
|
|
|
22
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
23
|
|
|
use yii\web\NotFoundHttpException; |
|
|
|
|
24
|
|
|
use yii\web\Response; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
|
|
|
|
27
|
|
|
* @author nystudio107 |
|
|
|
|
28
|
|
|
* @package Webperf |
|
|
|
|
29
|
|
|
* @since 1.0.0 |
|
|
|
|
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
class SectionsController extends Controller |
32
|
|
|
{ |
33
|
|
|
// Constants |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-webperf/'; |
37
|
|
|
|
38
|
|
|
// Protected Properties |
39
|
|
|
// ========================================================================= |
40
|
|
|
|
41
|
|
|
protected $allowAnonymous = []; |
42
|
|
|
|
43
|
|
|
// Public Methods |
44
|
|
|
// ========================================================================= |
45
|
|
|
|
46
|
|
|
/** |
|
|
|
|
47
|
|
|
* @param string|null $siteHandle |
|
|
|
|
48
|
|
|
* @param bool $showWelcome |
|
|
|
|
49
|
|
|
* |
50
|
|
|
* @return Response |
51
|
|
|
* @throws NotFoundHttpException |
52
|
|
|
* @throws \yii\web\ForbiddenHttpException |
53
|
|
|
*/ |
54
|
|
|
public function actionDashboard(string $siteHandle = null, bool $showWelcome = false): Response |
55
|
|
|
{ |
56
|
|
|
$variables = []; |
57
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
58
|
|
|
// Trim the statistics |
59
|
|
|
Webperf::$plugin->dataSamples->trimOrphanedSamples(1024); |
60
|
|
|
Webperf::$plugin->dataSamples->trimDataSamples(); |
61
|
|
|
// Get the site to edit |
62
|
|
|
$siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle); |
63
|
|
|
$pluginName = Webperf::$settings->pluginName; |
64
|
|
|
$templateTitle = Craft::t('webperf', 'Dashboard'); |
65
|
|
|
$view = Craft::$app->getView(); |
66
|
|
|
// Asset bundle |
67
|
|
|
try { |
68
|
|
|
$view->registerAssetBundle(WebperfDashboardAsset::class); |
69
|
|
|
} catch (InvalidConfigException $e) { |
70
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
71
|
|
|
} |
72
|
|
|
$variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
73
|
|
|
'@nystudio107/webperf/assetbundles/webperf/dist', |
74
|
|
|
true |
75
|
|
|
); |
76
|
|
|
// Enabled sites |
77
|
|
|
MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables); |
78
|
|
|
$variables['controllerHandle'] = 'dashboard'; |
79
|
|
|
|
80
|
|
|
// Basic variables |
81
|
|
|
$variables['fullPageForm'] = false; |
82
|
|
|
$variables['docsUrl'] = self::DOCUMENTATION_URL; |
83
|
|
|
$variables['pluginName'] = $pluginName; |
84
|
|
|
$variables['title'] = $templateTitle; |
85
|
|
|
$variables['crumbs'] = [ |
86
|
|
|
[ |
87
|
|
|
'label' => $pluginName, |
88
|
|
|
'url' => UrlHelper::cpUrl('webperf'), |
89
|
|
|
], |
90
|
|
|
[ |
91
|
|
|
'label' => $templateTitle, |
92
|
|
|
'url' => UrlHelper::cpUrl('webperf/dashboard'), |
93
|
|
|
], |
94
|
|
|
]; |
95
|
|
|
$variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
96
|
|
|
$variables['selectedSubnavItem'] = 'dashboard'; |
97
|
|
|
$variables['showWelcome'] = $showWelcome; |
98
|
|
|
$variables['settings'] = Webperf::$settings; |
99
|
|
|
|
100
|
|
|
// Render the template |
101
|
|
|
return $this->renderTemplate('webperf/dashboard/index', $variables); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
|
|
|
|
105
|
|
|
* @param string|null $siteHandle |
|
|
|
|
106
|
|
|
* |
107
|
|
|
* @return Response |
108
|
|
|
* @throws NotFoundHttpException |
109
|
|
|
* @throws \yii\web\ForbiddenHttpException |
110
|
|
|
*/ |
111
|
|
|
public function actionPagesIndex(string $siteHandle = null): Response |
112
|
|
|
{ |
113
|
|
|
$variables = []; |
114
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:pages'); |
115
|
|
|
// Trim the statistics |
116
|
|
|
Webperf::$plugin->dataSamples->trimOrphanedSamples(1024); |
117
|
|
|
Webperf::$plugin->dataSamples->trimDataSamples(); |
118
|
|
|
// Get the site to edit |
119
|
|
|
$siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle); |
120
|
|
|
$pluginName = Webperf::$settings->pluginName; |
121
|
|
|
$templateTitle = Craft::t('webperf', 'Pages'); |
122
|
|
|
$view = Craft::$app->getView(); |
123
|
|
|
// Asset bundle |
124
|
|
|
try { |
125
|
|
|
$view->registerAssetBundle(WebperfDashboardAsset::class); |
126
|
|
|
} catch (InvalidConfigException $e) { |
127
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
128
|
|
|
} |
129
|
|
|
$variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
130
|
|
|
'@nystudio107/webperf/assetbundles/webperf/dist', |
131
|
|
|
true |
132
|
|
|
); |
133
|
|
|
// Enabled sites |
134
|
|
|
MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables); |
135
|
|
|
$variables['controllerHandle'] = 'pages-index'; |
136
|
|
|
|
137
|
|
|
// Basic variables |
138
|
|
|
$variables['fullPageForm'] = false; |
139
|
|
|
$variables['docsUrl'] = self::DOCUMENTATION_URL; |
140
|
|
|
$variables['pluginName'] = $pluginName; |
141
|
|
|
$variables['title'] = $templateTitle; |
142
|
|
|
$variables['crumbs'] = [ |
143
|
|
|
[ |
144
|
|
|
'label' => $pluginName, |
145
|
|
|
'url' => UrlHelper::cpUrl('webperf'), |
146
|
|
|
], |
147
|
|
|
[ |
148
|
|
|
'label' => $templateTitle, |
149
|
|
|
'url' => UrlHelper::cpUrl('webperf/pages'), |
150
|
|
|
], |
151
|
|
|
]; |
152
|
|
|
$variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
153
|
|
|
$variables['selectedSubnavItem'] = 'pages'; |
154
|
|
|
$variables['settings'] = Webperf::$settings; |
155
|
|
|
|
156
|
|
|
// Render the template |
157
|
|
|
return $this->renderTemplate('webperf/pages/index', $variables); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
|
|
|
|
161
|
|
|
* @param string $pageUrl |
|
|
|
|
162
|
|
|
* @param string|null $siteHandle |
|
|
|
|
163
|
|
|
* |
164
|
|
|
* @return Response |
165
|
|
|
* @throws NotFoundHttpException |
166
|
|
|
* @throws \yii\web\ForbiddenHttpException |
167
|
|
|
*/ |
168
|
|
|
public function actionPageDetail(string $pageUrl, string $siteHandle = null): Response |
169
|
|
|
{ |
170
|
|
|
$variables = []; |
171
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:pages'); |
172
|
|
|
// Trim the statistics |
173
|
|
|
Webperf::$plugin->dataSamples->trimOrphanedSamples(1024); |
174
|
|
|
Webperf::$plugin->dataSamples->trimDataSamples(); |
175
|
|
|
// Get the site to edit |
176
|
|
|
$siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle); |
177
|
|
|
$pluginName = Webperf::$settings->pluginName; |
178
|
|
|
$templateTitle = Craft::t('webperf', 'Pages'); |
179
|
|
|
$view = Craft::$app->getView(); |
180
|
|
|
// Asset bundle |
181
|
|
|
try { |
182
|
|
|
$view->registerAssetBundle(WebperfDashboardAsset::class); |
183
|
|
|
} catch (InvalidConfigException $e) { |
184
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
185
|
|
|
} |
186
|
|
|
$variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
187
|
|
|
'@nystudio107/webperf/assetbundles/webperf/dist', |
188
|
|
|
true |
189
|
|
|
); |
190
|
|
|
// Enabled sites |
191
|
|
|
MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables); |
192
|
|
|
$variables['controllerHandle'] = 'pages-detail'; |
193
|
|
|
|
194
|
|
|
// Basic variables |
195
|
|
|
$variables['fullPageForm'] = false; |
196
|
|
|
$variables['docsUrl'] = self::DOCUMENTATION_URL; |
197
|
|
|
$variables['pluginName'] = $pluginName; |
198
|
|
|
$variables['title'] = $templateTitle; |
199
|
|
|
$variables['crumbs'] = [ |
200
|
|
|
[ |
201
|
|
|
'label' => $pluginName, |
202
|
|
|
'url' => UrlHelper::cpUrl('webperf'), |
203
|
|
|
], |
204
|
|
|
[ |
205
|
|
|
'label' => $templateTitle, |
206
|
|
|
'url' => UrlHelper::cpUrl('webperf/pages'), |
207
|
|
|
], |
208
|
|
|
]; |
209
|
|
|
$variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
210
|
|
|
$variables['selectedSubnavItem'] = 'pages'; |
211
|
|
|
$variables['pageUrl'] = $pageUrl; |
212
|
|
|
$variables['settings'] = Webperf::$settings; |
213
|
|
|
|
214
|
|
|
// Render the template |
215
|
|
|
return $this->renderTemplate('webperf/pages/page-detail', $variables); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|