Skip to content

Commit

Permalink
Fix CrudRoutesAttributesLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
lbaey committed Mar 11, 2024
1 parent 8d63904 commit ed7a293
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Resources/config/services/route_loader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@
<service class="Sylius\Bundle\ResourceBundle\Routing\RouteFactory" />
</argument>
</service>

<service id="sylius.routing.loader.crud_routes_attributes" class="Mobizel\Bundle\SyliusExportPlugin\Routing\CrudRoutesAttributesLoader" public="false">
<argument>%sylius.resource.mapping%</argument>
<argument type="service" id="sylius.routing.loader.resource" />
<tag name="routing.route_loader" />
</service>
<service id="Mobizel\Bundle\SyliusExportPlugin\Routing\CrudRoutesAttributesLoader" alias="sylius.routing.loader.crud_routes_attributes" public="false" />
</services>
</container>
62 changes: 62 additions & 0 deletions src/Routing/CrudRoutesAttributesLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Mobizel\SyliusExportPlugin\Routing;

use Sylius\Component\Resource\Annotation\SyliusCrudRoutes;
use Sylius\Component\Resource\Reflection\ClassReflection;
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Yaml\Yaml;

final class CrudRoutesAttributesLoader implements RouteLoaderInterface
{
private array $mapping;

private ResourceLoader $resourceLoader;


public function __construct(
array $mapping,
ResourceLoader $resourceLoader,
)
{
$this->mapping = $mapping;
$this->resourceLoader = $resourceLoader;
$this->crudRoutesAttributesLoader = $crudRoutesAttributesLoader;
}

public function __invoke(): RouteCollection
{
$routeCollection = new RouteCollection();
$paths = $this->mapping['paths'] ?? [];

/** @var string $className */
foreach (ClassReflection::getResourcesByPaths($paths) as $className) {
$this->addRoutesForSyliusCrudRoutesAttributes($routeCollection, $className);
}

return $routeCollection;
}

private function addRoutesForSyliusCrudRoutesAttributes(RouteCollection $routeCollection, string $className): void
{
$attributes = ClassReflection::getClassAttributes($className, SyliusCrudRoutes::class);

foreach ($attributes as $reflectionAttribute) {
$resource = Yaml::dump($reflectionAttribute->getArguments());
$resourceRouteCollection = $this->resourceLoader->load($resource);
$routeCollection->addCollection($resourceRouteCollection);
}
}
}

0 comments on commit ed7a293

Please sign in to comment.