|
15 | 15 |
|
16 | 16 | use Sylius\Component\Grid\Definition\Grid; |
17 | 17 | use Sylius\Resource\Metadata\RegistryInterface; |
18 | | -use Symfony\Component\HttpFoundation\Request; |
19 | | -use Symfony\Component\HttpFoundation\RequestStack; |
20 | 18 |
|
21 | | -final class ExportableChecker implements ExportableCheckerInterface |
| 19 | +final readonly class ExportableChecker implements ExportableCheckerInterface |
22 | 20 | { |
23 | | - /** |
24 | | - * @param array<array-key, string> $allowedSections |
25 | | - * @param array<array-key, string> $allowedResources |
26 | | - */ |
| 21 | + /** @param array<string, array{provider: string, sections: string[]}> $exportResourcesConfig */ |
27 | 22 | public function __construct( |
28 | | - private RequestStack $requestStack, |
29 | 23 | private RegistryInterface $resourceRegistry, |
30 | | - private array $allowedSections, |
31 | | - private array $allowedResources, |
| 24 | + private array $exportResourcesConfig, |
32 | 25 | ) { |
33 | 26 | } |
34 | 27 |
|
35 | | - public function canBeExported(Grid $grid): bool |
| 28 | + public function canBeExported(Grid $grid, object|string|null $section): bool |
36 | 29 | { |
37 | 30 | $resourceClass = $grid->getDriverConfiguration()['class'] ?? null; |
38 | 31 | if (null === $resourceClass) { |
39 | 32 | return false; |
40 | 33 | } |
41 | 34 |
|
42 | | - $request = $this->requestStack->getMainRequest(); |
43 | | - if (!$request instanceof Request) { |
44 | | - return false; |
| 35 | + if (null === $section) { |
| 36 | + return true; |
45 | 37 | } |
46 | 38 |
|
47 | | - if (!$request->attributes->has('_sylius')) { |
48 | | - return false; |
49 | | - } |
| 39 | + $resourceAlias = $this->resourceRegistry->getByClass($resourceClass)->getAlias(); |
50 | 40 |
|
51 | | - $syliusAttributes = $request->attributes->all()['_sylius']; |
52 | | - if (!in_array($syliusAttributes['section'] ?? null, $this->allowedSections)) { |
| 41 | + $resourceConfig = $this->exportResourcesConfig[$resourceAlias] ?? false; |
| 42 | + if (false === $resourceConfig) { |
53 | 43 | return false; |
54 | 44 | } |
55 | 45 |
|
56 | | - $resourceMetadata = $this->resourceRegistry->getByClass($resourceClass); |
| 46 | + foreach ($resourceConfig['sections'] as $configSection) { |
| 47 | + if (is_a($section, $configSection, true) || $section === $configSection) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + } |
57 | 51 |
|
58 | | - return in_array($resourceMetadata->getAlias(), $this->allowedResources); |
| 52 | + return false; |
59 | 53 | } |
60 | 54 | } |
0 commit comments