-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRockDevTools.module.php
More file actions
154 lines (130 loc) · 4.18 KB
/
RockDevTools.module.php
File metadata and controls
154 lines (130 loc) · 4.18 KB
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
<?php
namespace ProcessWire;
use RockDevTools\Assets;
use RockDevTools\LiveReload;
use RockDevTools\RockCSS;
function rockdevtools(): RockDevTools
{
return wire()->modules->get('RockDevTools');
}
/**
* @author Bernhard Baumrock, 14.01.2025
* @license Licensed under MIT
* @link https://www.baumrock.com
*/
require_once __DIR__ . '/vendor/autoload.php';
class RockDevTools extends WireData implements Module, ConfigurableModule
{
public $debugAssetTools = false;
public $autoLogin = false;
/** @var string Redirect path after auto-login. Empty = default (page id 2). */
public $autoLoginRedirect = '';
public $livereload;
private $rockcss = false;
public function __construct()
{
// early exit if not enabled to keep the footprint as low as possible
if (!wire()->config->rockdevtools) return;
// add classloader and load livereload
wire()->classLoader->addNamespace('RockDevTools', __DIR__ . '/classes');
$this->livereload = new LiveReload();
}
public function __debugInfo()
{
return [
'livereload' => $this->livereload->filesToWatch(),
];
}
public function init()
{
// early exit if not enabled to keep the footprint as low as possible
if (!wire()->config->rockdevtools) return;
// minify assets
$this->assets()->minify(__DIR__ . '/src', __DIR__ . '/dst');
// add panel to support livereload on tracy blue screen
$this->livereload->addBlueScreenPanel();
// hooks
wire()->addHookAfter('Modules::refresh', $this, 'resetCache');
wire()->addHookAfter('Page::render', $this->livereload, 'addLiveReloadMarkup');
$this->addAutoLoginHook();
}
/**
* Add auto-login URL hook only when debug + DDEV + autoLogin enabled.
*/
protected function addAutoLoginHook(): void
{
if (!wire()->config->debug) return;
if (!getenv('DDEV_HOSTNAME')) return;
if (!$this->autoLogin) return;
wire()->addHook('/auto-login', $this, 'autoLogin');
}
/**
* Force login as superuser and redirect (called by /auto-login hook).
* Redirect target: autoLoginRedirect if set, otherwise page id 2.
*/
public function autoLogin(HookEvent $event): void
{
$user = wire()->users->get('roles=superuser');
if (!$user->id) return;
wire()->session->forceLogin($user);
$url = trim((string) $this->autoLoginRedirect) !== ''
? wire()->config->urls->root . ltrim($this->autoLoginRedirect, '/')
: wire()->pages->get(2)->url;
wire()->session->redirect($url);
}
public function assets(?string $root = null): Assets
{
return new Assets($root);
}
public function getModuleConfigInputfields(InputfieldWrapper $inputfields)
{
$inputfields->add([
'type' => 'markup',
'label' => 'LiveReload Files List',
'value' => wire()->files->render(__DIR__ . '/markup/livereloadinfo.php'),
'icon' => 'magic',
]);
$inputfields->add([
'type' => 'checkbox',
'label' => 'Debug Asset Tools',
'name' => 'debugAssetTools',
'checked' => $this->debugAssetTools,
'notes' => 'If enabled, the asset tools will log debug information to the Tracy debug bar.',
]);
$inputfields->add([
'type' => 'checkbox',
'label' => 'Auto Login',
'name' => 'autoLogin',
'checked' => $this->autoLogin,
'notes' => 'When enabled (and only when debug mode + DDEV), visiting /auto-login will log in as superuser and redirect. Useful for manual login or browser automation.',
]);
$inputfields->add([
'type' => 'text',
'label' => 'Auto Login Redirect',
'name' => 'autoLoginRedirect',
'value' => $this->autoLoginRedirect,
'placeholder' => '',
'notes' => 'Path to redirect to after auto-login (e.g. /tool). Empty = default (page id 2).',
'showIf' => 'autoLogin=1',
]);
return $inputfields;
}
/**
* Reset cache and recreate all minified files
* @param HookEvent $event
* @return void
* @throws WireException
*/
public function resetCache(HookEvent $event): void
{
wire()->cache->delete('rockdevtools-filenames-*');
}
/**
* @return RockCSS
*/
public function rockcss()
{
if (!$this->rockcss) $this->rockcss = new RockCSS();
return $this->rockcss;
}
}