-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscoDevBarService.php
More file actions
192 lines (167 loc) · 6.39 KB
/
DiscoDevBarService.php
File metadata and controls
192 lines (167 loc) · 6.39 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
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
<?php
declare(strict_types=1);
/* #############################################################################
*
* █▀▀▄ ▀ █▀▀▄ █▀▀▄
* █ █ ▀█ ▄▀▀▄ ▄▀▀▄ ▄▀▀▄ █ █ ▄▀▀▄ █ █ █▀▀▄ ▄▀▀▄ █▄▀
* █ █ █ ▀▄ █ █ █ █ █ █▀▀ █ █ █ █ ▄▄█ █
* █▄▄▀ ▄█▄ ▀▄▄▀ ▀▄▄▀ ▀▄▄▀ █▄▄▀ ▀▄▄▀ ▀▄▀ █▄▄▀ ▀▄▄▀ █
*
* Customizable developer toolbar for Symfony projects
*
* @author Marcin Orlowski <mail (#) marcinOrlowski (.) com>
* @copyright 2025 Marcin Orlowski
* @license https://opensource.org/license/mit MIT
* @link https://github.com/MarcinOrlowski/php-symfony-discodevbar
*
* ########################################################################## */
namespace MarcinOrlowski\DiscoDevBar\Service;
use Composer\InstalledVersions;
use MarcinOrlowski\DiscoDevBar\Dto\DiscoDevBarData;
use MarcinOrlowski\DiscoDevBar\Dto\Widget;
use Symfony\Component\Yaml\Yaml;
class DiscoDevBarService
{
/**
* List of supported config filenames (in order of preference)
*/
private const CONFIG_FILES = [
'.disco-devbar.yaml',
'.disco-devbar.yml',
'.debug-banner.yaml', // Legacy, kept for backward compatibility
];
/**
* Default Font Awesome version
*/
private const DEFAULT_FONT_AWESOME_VERSION = '6.5.1';
/**
* Default background colors for breathing stripes
*/
private const DEFAULT_BG_COLOR_LIGHT = '#b71c1c';
private const DEFAULT_BG_COLOR_DARK = '#8e0000';
public function __construct(
private readonly string $projectDir
) {
}
public function getDiscoDevBarData(): DiscoDevBarData
{
$configPath = $this->findConfigFile();
$version = $this->getVersion();
// Default: show error message when no config found
if ($configPath === null) {
$errorMessage = 'Config file not found: ' . self::CONFIG_FILES[0];
return new DiscoDevBarData(
left: [],
right: [],
leftExpand: false,
rightExpand: false,
hasError: true,
errorMessage: $errorMessage,
version: $version,
fontAwesomeEnabled: false,
fontAwesomeVersion: self::DEFAULT_FONT_AWESOME_VERSION,
bgColorLight: self::DEFAULT_BG_COLOR_LIGHT,
bgColorDark: self::DEFAULT_BG_COLOR_DARK
);
}
$config = Yaml::parseFile($configPath);
// Ensure config is an array and has the expected structure
if (!\is_array($config)) {
$config = [];
}
// Extract Font Awesome configuration from YAML
$fontAwesomeConfig = $config['font_awesome'] ?? [];
$fontAwesomeEnabled = false;
$fontAwesomeVersion = self::DEFAULT_FONT_AWESOME_VERSION;
if (\is_array($fontAwesomeConfig)) {
$fontAwesomeEnabled = $fontAwesomeConfig['enabled'] ?? false;
// Use user's version if provided and not null, otherwise use default
$userVersion = $fontAwesomeConfig['version'] ?? null;
if ($userVersion !== null && \is_string($userVersion)) {
$fontAwesomeVersion = $userVersion;
}
}
$widgets = $config['widgets'] ?? [];
if (!\is_array($widgets)) {
$widgets = [];
}
$left = $widgets['left'] ?? [];
$right = $widgets['right'] ?? [];
$leftWidgets = $this->loadWidgets(\is_array($left) ? $left : []);
$rightWidgets = $this->loadWidgets(\is_array($right) ? $right : []);
$bgColorLight = $config['bg_color_light'] ?? self::DEFAULT_BG_COLOR_LIGHT;
$bgColorDark = $config['bg_color_dark'] ?? self::DEFAULT_BG_COLOR_DARK;
return new DiscoDevBarData(
left: $leftWidgets,
right: $rightWidgets,
leftExpand: $this->hasExpandingWidget($leftWidgets),
rightExpand: $this->hasExpandingWidget($rightWidgets),
hasError: false,
errorMessage: '',
version: $version,
fontAwesomeEnabled: \is_bool($fontAwesomeEnabled) ? $fontAwesomeEnabled : false,
fontAwesomeVersion: $fontAwesomeVersion,
bgColorLight: \is_string($bgColorLight) ? $bgColorLight : self::DEFAULT_BG_COLOR_LIGHT,
bgColorDark: \is_string($bgColorDark) ? $bgColorDark : self::DEFAULT_BG_COLOR_DARK
);
}
/**
* Find the first existing config file from the list of supported filenames
*
* @return string|null Full path to config file or null if none found
*/
private function findConfigFile(): ?string
{
foreach (self::CONFIG_FILES as $filename) {
$path = $this->projectDir . '/' . $filename;
if (\file_exists($path)) {
return $path;
}
}
return null;
}
/**
* Check if any widget in the array has expand=true
*
* @param array<Widget> $widgets
*/
private function hasExpandingWidget(array $widgets): bool
{
foreach ($widgets as $widget) {
if ($widget->expand) {
return true;
}
}
return false;
}
/**
* Load and normalize widgets from configuration array
*
* @param array<mixed> $widgetsData
*
* @return array<Widget>
*/
private function loadWidgets(array $widgetsData): array
{
return \array_map(
function ($widgetData): Widget {
/** @var array<string, mixed> $data */
$data = \is_array($widgetData) ? $widgetData : [];
return Widget::fromArray($data);
},
$widgetsData
);
}
/**
* Get bundle version from Composer
*/
private function getVersion(): string
{
try {
$version = InstalledVersions::getVersion('marcin-orlowski/symfony-discodevbar');
return $version ?? 'dev';
} catch (\Exception $e) {
return 'dev';
}
}
}