-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lbaey
committed
Mar 11, 2024
1 parent
8d63904
commit ed7a293
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |