diff --git a/src/Dashboard/ResourcePreview.php b/src/Dashboard/ResourcePreview.php index 37b0e4002..29986b49c 100644 --- a/src/Dashboard/ResourcePreview.php +++ b/src/Dashboard/ResourcePreview.php @@ -4,6 +4,7 @@ namespace MoonShine\Dashboard; +use Closure; use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Support\Collection; use MoonShine\Resources\Resource; @@ -13,10 +14,13 @@ final class ResourcePreview extends DashboardItem { protected static string $view = 'moonshine::blocks.resource_preview'; + /** + * @deprecated Builder $query, use Closure $query + */ public function __construct( protected Resource $resource, string $label = '', - protected ?Builder $query = null, + protected Builder|Closure|null $query = null, ) { $this->setLabel($label); } @@ -34,13 +38,20 @@ public function items(): Collection { $collections = $this->resource() ->resolveQuery() - ->when($this->query, fn () => $this->query) + ->when($this->getQuery(), fn () => $this->getQuery()) ->get(); return $this->resource() ->transformToResources($collections); } + protected function getQuery(): ?Builder + { + return is_callable($this->query) + ? call_user_func($this->query) + : $this->query; + } + public function id(string $index = null): string { return str($this->resource()->routeNameAlias()) diff --git a/src/Metrics/DonutChartMetric.php b/src/Metrics/DonutChartMetric.php index e2f282bce..c6681e16c 100644 --- a/src/Metrics/DonutChartMetric.php +++ b/src/Metrics/DonutChartMetric.php @@ -4,6 +4,8 @@ namespace MoonShine\Metrics; +use Closure; + class DonutChartMetric extends Metric { protected static string $view = 'moonshine::metrics.donut-chart'; @@ -16,11 +18,13 @@ class DonutChartMetric extends Metric ]; /** - * @param $values array + * @param $values array|Closure */ - public function values(array $values): self + public function values(array|Closure $values): self { - $this->values = $values; + $this->values = is_callable($values) + ? $values() + : $values; return $this; } diff --git a/src/Metrics/LineChartMetric.php b/src/Metrics/LineChartMetric.php index 8106f40a4..73122e4cf 100644 --- a/src/Metrics/LineChartMetric.php +++ b/src/Metrics/LineChartMetric.php @@ -4,6 +4,8 @@ namespace MoonShine\Metrics; +use Closure; + class LineChartMetric extends Metric { protected static string $view = 'moonshine::metrics.line-chart'; @@ -22,11 +24,13 @@ public function lines(): array return $this->lines; } - public function line(array $line, string|array $color = '#7843E9'): static + public function line(array|Closure $line, string|array|Closure $color = '#7843E9'): static { - $this->lines[] = $line; + $this->lines[] = is_callable($line) ? $line() : $line; + + $color = is_callable($color) ? $color() : $color; - if(is_string($color)) { + if (is_string($color)) { $this->colors[] = $color; } else { $this->colors = $color; diff --git a/src/Metrics/ValueMetric.php b/src/Metrics/ValueMetric.php index 6ac5d62fe..508c18e61 100644 --- a/src/Metrics/ValueMetric.php +++ b/src/Metrics/ValueMetric.php @@ -4,6 +4,8 @@ namespace MoonShine\Metrics; +use Closure; + class ValueMetric extends Metric { protected static string $view = 'moonshine::metrics.value'; @@ -16,9 +18,9 @@ class ValueMetric extends Metric public int|float $target = 0; - public function valueFormat(string $value): static + public function valueFormat(string|Closure $value): static { - $this->valueFormat = $value; + $this->valueFormat = is_callable($value) ? $value() : $value; return $this; } @@ -37,17 +39,17 @@ public function simpleValue(): string|float return str_replace('{value}', (string) $this->value, $this->valueFormat); } - public function value(int|float $value): static + public function value(int|float|Closure $value): static { - $this->value = $value; + $this->value = is_callable($value) ? $value() : $value; return $this; } - public function progress(int|float $target): static + public function progress(int|float|Closure $target): static { $this->progress = true; - $this->target = $target; + $this->target = is_callable($target) ? $target() : $target; return $this; } diff --git a/src/QueryTags/QueryTag.php b/src/QueryTags/QueryTag.php index 3f6d65e06..6d815d1b6 100644 --- a/src/QueryTags/QueryTag.php +++ b/src/QueryTags/QueryTag.php @@ -18,6 +18,9 @@ final class QueryTag use HasCanSee; use WithLabel; + /** + * @deprecated Builder $builder, use Closure $builder + */ public function __construct( string $label, protected Builder|Closure $builder,