|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bag\Pipelines; |
| 6 | + |
| 7 | +use Bag\Attributes\MemoizeUsing; |
| 8 | +use Bag\Bag; |
| 9 | +use Bag\Internal\Reflection; |
| 10 | +use Bag\Pipelines\Pipes\CastInputValues; |
| 11 | +use Bag\Pipelines\Pipes\ComputedValues; |
| 12 | +use Bag\Pipelines\Pipes\DebugCollection; |
| 13 | +use Bag\Pipelines\Pipes\ExtraParameters; |
| 14 | +use Bag\Pipelines\Pipes\FillBag; |
| 15 | +use Bag\Pipelines\Pipes\FillNulls; |
| 16 | +use Bag\Pipelines\Pipes\FillOptionals; |
| 17 | +use Bag\Pipelines\Pipes\IsVariadic; |
| 18 | +use Bag\Pipelines\Pipes\LaravelRouteParameters; |
| 19 | +use Bag\Pipelines\Pipes\MapInput; |
| 20 | +use Bag\Pipelines\Pipes\MissingProperties; |
| 21 | +use Bag\Pipelines\Pipes\ProcessArguments; |
| 22 | +use Bag\Pipelines\Pipes\ProcessParameters; |
| 23 | +use Bag\Pipelines\Pipes\StripExtraParameters; |
| 24 | +use Bag\Pipelines\Pipes\Transform; |
| 25 | +use Bag\Pipelines\Pipes\Validate; |
| 26 | +use Bag\Pipelines\Values\BagInput; |
| 27 | +use Illuminate\Support\Collection; |
| 28 | +use Illuminate\Support\Facades\Cache; |
| 29 | +use League\Pipeline\Pipeline; |
| 30 | + |
| 31 | +class MemoizePipeline |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @template T of Bag |
| 35 | + * @param BagInput<T> $input |
| 36 | + * @param string|array<string>|Collection<array-key, string>|null $cacheKeyAttributeNames |
| 37 | + * @return T |
| 38 | + */ |
| 39 | + public static function process(BagInput $input, string|array|Collection|null $cacheKeyAttributeNames = null): Bag |
| 40 | + { |
| 41 | + if ($cacheKeyAttributeNames === null) { |
| 42 | + $attribute = Reflection::getAttribute(Reflection::getClass($input->bagClassname), MemoizeUsing::class); |
| 43 | + if ($attribute !== null) { |
| 44 | + $cacheKeyAttributeNames = Reflection::getAttributeInstance($attribute)?->cacheKeyAttributeNames; |
| 45 | + if ($cacheKeyAttributeNames !== null) { |
| 46 | + $cacheKeyAttributeNames = Collection::wrap($cacheKeyAttributeNames); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if ($cacheKeyAttributeNames === null) { |
| 52 | + return static::runPipeline($input); |
| 53 | + } |
| 54 | + |
| 55 | + $cacheKeyAttributeNames = Collection::wrap($cacheKeyAttributeNames); |
| 56 | + if ($input->input->count() === 1 && isset($input->input[0]) && is_array($input->input[0])) { |
| 57 | + $cacheKey = Collection::wrap($input->input[0])->filter(fn ($value, $key) => $cacheKeyAttributeNames->contains($key))->values()->implode(':'); |
| 58 | + } else { |
| 59 | + $cacheKey = $input->input->filter(fn ($value, $key) => $cacheKeyAttributeNames->contains($key))->values()->implode(':'); |
| 60 | + } |
| 61 | + |
| 62 | + return Cache::driver('array')->rememberForever($input->bagClassname . ':' . $cacheKey, function () use ($input) { |
| 63 | + return static::runPipeline($input); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @template T of Bag |
| 69 | + * @param BagInput<T> $input |
| 70 | + * @return T |
| 71 | + */ |
| 72 | + protected static function runPipeline(BagInput $input): Bag |
| 73 | + { |
| 74 | + $pipeline = new Pipeline( |
| 75 | + null, |
| 76 | + new Transform(), |
| 77 | + new ProcessParameters(), |
| 78 | + new ProcessArguments(), |
| 79 | + new IsVariadic(), |
| 80 | + new MapInput(), |
| 81 | + new LaravelRouteParameters(), |
| 82 | + new FillOptionals(), |
| 83 | + new FillNulls(), |
| 84 | + new MissingProperties(), |
| 85 | + new ExtraParameters(), |
| 86 | + new StripExtraParameters(), |
| 87 | + new Validate(), |
| 88 | + new CastInputValues(), |
| 89 | + new FillBag(), |
| 90 | + new ComputedValues(), |
| 91 | + new DebugCollection(), |
| 92 | + ); |
| 93 | + |
| 94 | + // @phpstan-ignore-next-line property.nonObject |
| 95 | + return $pipeline->process($input)->bag; |
| 96 | + } |
| 97 | +} |
0 commit comments