-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscoDevBarService.php
More file actions
134 lines (115 loc) · 3.92 KB
/
DiscoDevBarService.php
File metadata and controls
134 lines (115 loc) · 3.92 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
<?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 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
];
public function __construct(
private readonly string $projectDir
) {
}
public function getDiscoDevBarData(): DiscoDevBarData
{
$configPath = $this->findConfigFile();
// Default: empty widgets
if ($configPath === null) {
return new DiscoDevBarData(
left: [],
right: [],
leftExpand: false,
rightExpand: false
);
}
$config = Yaml::parseFile($configPath);
// Ensure config is an array and has the expected structure
if (!\is_array($config)) {
$config = [];
}
$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 : []);
return new DiscoDevBarData(
left: $leftWidgets,
right: $rightWidgets,
leftExpand: $this->hasExpandingWidget($leftWidgets),
rightExpand: $this->hasExpandingWidget($rightWidgets)
);
}
/**
* 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
);
}
}