vendor/sonata-project/admin-bundle/src/Route/AdminPoolLoader.php line 47

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\AdminBundle\Route;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Symfony\Component\Config\Loader\Loader;
  14. use Symfony\Component\Config\Resource\FileResource;
  15. use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;
  16. /**
  17.  * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  18.  *
  19.  * @psalm-suppress PropertyNotSetInConstructor
  20.  */
  21. final class AdminPoolLoader extends Loader
  22. {
  23.     public const ROUTE_TYPE_NAME 'sonata_admin';
  24.     public function __construct(
  25.         private Pool $pool
  26.     ) {
  27.         parent::__construct();
  28.     }
  29.     public function supports(mixed $resource, ?string $type null): bool
  30.     {
  31.         return self::ROUTE_TYPE_NAME === $type;
  32.     }
  33.     public function load(mixed $resource, ?string $type null): SymfonyRouteCollection
  34.     {
  35.         $collection = new SymfonyRouteCollection();
  36.         foreach ($this->pool->getAdminServiceCodes() as $code) {
  37.             $admin $this->pool->getInstance($code);
  38.             foreach ($admin->getRoutes()->getElements() as $route) {
  39.                 $name $route->getDefault('_sonata_name');
  40.                 \assert(\is_string($name));
  41.                 $collection->add($name$route);
  42.             }
  43.             $reflection = new \ReflectionObject($admin);
  44.             if (false !== $reflection->getFileName() && file_exists($reflection->getFileName())) {
  45.                 $collection->addResource(new FileResource($reflection->getFileName()));
  46.             }
  47.         }
  48.         return $collection;
  49.     }
  50. }