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

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.     /**
  30.      * @param mixed $resource
  31.      */
  32.     public function supports($resource, ?string $type null): bool
  33.     {
  34.         return self::ROUTE_TYPE_NAME === $type;
  35.     }
  36.     /**
  37.      * @param mixed $resource
  38.      */
  39.     public function load($resource, ?string $type null): SymfonyRouteCollection
  40.     {
  41.         $collection = new SymfonyRouteCollection();
  42.         foreach ($this->pool->getAdminServiceCodes() as $code) {
  43.             $admin $this->pool->getInstance($code);
  44.             foreach ($admin->getRoutes()->getElements() as $route) {
  45.                 $name $route->getDefault('_sonata_name');
  46.                 \assert(\is_string($name));
  47.                 $collection->add($name$route);
  48.             }
  49.             $reflection = new \ReflectionObject($admin);
  50.             if (false !== $reflection->getFileName() && file_exists($reflection->getFileName())) {
  51.                 $collection->addResource(new FileResource($reflection->getFileName()));
  52.             }
  53.         }
  54.         return $collection;
  55.     }
  56. }