-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRockDevTools.module.php
100 lines (84 loc) · 2.34 KB
/
RockDevTools.module.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
<?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
{
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');
}
public function assets(): Assets
{
return new Assets();
}
/**
* 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;
}
/**
* Ensures that given path is a path within the PW root.
*
* Usage:
* $rockdevtools->toPath("/site/templates/foo.css");
* $rockdevtools->toPath("/var/www/html/site/templates/foo.css");
* @param string $path
* @return string
*/
public function toPath(string $path): string
{
$path = Paths::normalizeSeparators($path);
$root = wire()->config->paths->root;
if (str_starts_with($path, $root)) return $path;
return $root . ltrim($path, '/');
}
}