-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #334 Sylius route with attributes (loic425)
This PR was merged into the 1.9-dev branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT ```php <?php declare(strict_types=1); namespace App\Entity; use App\Annotation\SyliusCrudRoutes; use App\Annotation\SyliusRoute; use Sylius\Component\Resource\Model\ResourceInterface; #[SyliusCrudRoutes( alias: 'app.book', path: 'library', section: 'backend', redirect: 'index', grid: 'sylius_backend_admin_user', except: ['show'], vars: [ 'all' => [ 'subheader' => 'sylius.ui.manage_users_able_to_access_administration_panel', ], 'index' => [ 'icon' => 'lock', ] ], )] #[SyliusRoute( name: 'app_backend_book_show', path: '/library/{id}', methods: ['GET'], controller: 'app.controller.book::indexAction', template: 'backend/book/show.html.twig', vars: [ 'subheader' => 'sylius.ui.manage_users_able_to_access_administration_panel', ] )] class Book implements ResourceInterface { private ?int $id = null; public function getId(): ?int { return $this->id; } } ``` ## Crud routes - [x] CRUD routes with alias - [x] CRUD routes with section - [x] CRUD routes with criteria - [x] CRUD routes with template - [x] CRUD routes with grid - [x] CRUD routes with vars - [x] CRUD routes with redirect - [x] CRUD routes with persmission - [x] CRUD routes with except - [x] CRUD routes with only ## Single routes - [x] Single route with name, path and controller - [x] Single route with methods - [x] Single route with criteria - [x] Single route with template - [x] Single route with repository - [x] Single route with serializationGroups - [x] Single route with serializationVersion - [x] Single route with requirements - [x] Single route with options - [x] Single route with host - [x] Single route with schemes - [x] Single route with priority - [x] Single route with vars Commits ------- 500410d Sylius route with attributes 35dbac9 Add Single route with template and with repository fb80bcf Add some missing feature for single routes 23e4eae Add some other missing features for single routes 09075ca Add Phpspec tests 23f6301 Add missing headers 66f8512 Make mapping paths configurable 05ee060 Skip priority test on Symfony 4 8246dee Apply suggestions from code review 65c96aa Separate boths routing loaders a007770 Fix Psalm and PHPStan 4bbaf6c Remove testing PHP greater than 8.0 954e199 Fix Phpspec tests 77bff28 Fix Phpspec tests for Symfony 4.4 9b061fe Add Unit tests for Class Reflection 0696b54 Skipped generating routes from resource with priority on Symfony < 5 6c8092b Add route attributes factory
- Loading branch information
Showing
50 changed files
with
2,636 additions
and
5 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
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
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
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,58 @@ | ||
<?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 Sylius\Bundle\ResourceBundle\Routing; | ||
|
||
use Sylius\Component\Resource\Annotation\SyliusCrudRoutes; | ||
use Sylius\Component\Resource\Reflection\ClassReflection; | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
final class CrudRoutesAttributesLoader | ||
{ | ||
private array $mapping; | ||
|
||
private ResourceLoader $resourceLoader; | ||
|
||
public function __construct( | ||
array $mapping, | ||
ResourceLoader $resourceLoader | ||
) { | ||
$this->mapping = $mapping; | ||
$this->resourceLoader = $resourceLoader; | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |
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,75 @@ | ||
<?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 Sylius\Bundle\ResourceBundle\Routing; | ||
|
||
use Sylius\Component\Resource\Annotation\SyliusRoute; | ||
use Sylius\Component\Resource\Reflection\ClassReflection; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class RouteAttributesFactory implements RouteAttributesFactoryInterface | ||
{ | ||
public function createRouteForClass(RouteCollection $routeCollection, string $className): void | ||
{ | ||
$attributes = ClassReflection::getClassAttributes($className, SyliusRoute::class); | ||
|
||
foreach ($attributes as $reflectionAttribute) { | ||
$arguments = $reflectionAttribute->getArguments(); | ||
|
||
Assert::keyExists($arguments, 'name', 'Your route should have a name attribute.'); | ||
|
||
$syliusOptions = []; | ||
|
||
if (isset($arguments['template'])) { | ||
$syliusOptions['template'] = $arguments['template']; | ||
} | ||
|
||
if (isset($arguments['vars'])) { | ||
$syliusOptions['vars'] = $arguments['vars']; | ||
} | ||
|
||
if (isset($arguments['criteria'])) { | ||
$syliusOptions['criteria'] = $arguments['criteria']; | ||
} | ||
|
||
if (isset($arguments['repository'])) { | ||
$syliusOptions['repository'] = $arguments['repository']; | ||
} | ||
|
||
if (isset($arguments['serializationGroups'])) { | ||
$syliusOptions['serialization_groups'] = $arguments['serializationGroups']; | ||
} | ||
|
||
if (isset($arguments['serializationVersion'])) { | ||
$syliusOptions['serialization_version'] = $arguments['serializationVersion']; | ||
} | ||
|
||
$route = new Route( | ||
$arguments['path'], | ||
[ | ||
'_controller' => $arguments['controller'] ?? null, | ||
'_sylius' => $syliusOptions, | ||
], | ||
$arguments['requirements'] ?? [], | ||
$arguments['options'] ?? [], | ||
$arguments['host'] ?? '', | ||
$arguments['schemes'] ?? [], | ||
$arguments['methods'] ?? [] | ||
); | ||
|
||
$routeCollection->add($arguments['name'], $route, $arguments['priority'] ?? 0); | ||
} | ||
} | ||
} |
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,21 @@ | ||
<?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 Sylius\Bundle\ResourceBundle\Routing; | ||
|
||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
interface RouteAttributesFactoryInterface | ||
{ | ||
public function createRouteForClass(RouteCollection $routeCollection, string $className): void; | ||
} |
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,56 @@ | ||
<?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 Sylius\Bundle\ResourceBundle\Routing; | ||
|
||
use Sylius\Component\Resource\Reflection\ClassReflection; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
final class RoutesAttributesLoader | ||
{ | ||
private array $mapping; | ||
|
||
private RouteAttributesFactoryInterface $routesAttributesFactory; | ||
|
||
public function __construct(array $mapping, RouteAttributesFactoryInterface $routesAttributesFactory) | ||
{ | ||
$this->mapping = $mapping; | ||
$this->routesAttributesFactory = $routesAttributesFactory; | ||
} | ||
|
||
public function __invoke(): RouteCollection | ||
{ | ||
$routeCollection = new RouteCollection(); | ||
$paths = $this->mapping['paths'] ?? []; | ||
|
||
/** @var string $className */ | ||
foreach (ClassReflection::getResourcesByPaths($paths) as $className) { | ||
$this->routesAttributesFactory->createRouteForClass($routeCollection, $className); | ||
} | ||
|
||
return $routeCollection; | ||
} | ||
|
||
private function getClasses(): iterable | ||
{ | ||
$paths = $this->mapping['paths'] ?? []; | ||
|
||
foreach ($paths as $resourceDirectory) { | ||
$resources = ClassReflection::getResourcesByPath($resourceDirectory); | ||
|
||
foreach ($resources as $className) { | ||
yield $className; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.