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

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. final class AdminPoolLoader extends Loader
  20. {
  21.     public const ROUTE_TYPE_NAME 'sonata_admin';
  22.     private Pool $pool;
  23.     public function __construct(Pool $pool)
  24.     {
  25.         // Remove this check when dropping support for support of symfony/symfony-config < 5.3.
  26.         // @phpstan-ignore-next-line
  27.         if (method_exists(parent::class, '__construct')) {
  28.             parent::__construct();
  29.         }
  30.         $this->pool $pool;
  31.     }
  32.     /**
  33.      * NEXT_MAJOR: Add the ?string param typehint when Symfony 4 support is dropped.
  34.      *
  35.      * @param mixed       $resource
  36.      * @param string|null $type
  37.      */
  38.     public function supports($resource$type null): bool
  39.     {
  40.         return self::ROUTE_TYPE_NAME === $type;
  41.     }
  42.     /**
  43.      * NEXT_MAJOR: Add the ?string param typehint when Symfony 4 support is dropped.
  44.      *
  45.      * @param mixed       $resource
  46.      * @param string|null $type
  47.      */
  48.     public function load($resource$type null): SymfonyRouteCollection
  49.     {
  50.         $collection = new SymfonyRouteCollection();
  51.         foreach ($this->pool->getAdminServiceCodes() as $code) {
  52.             $admin $this->pool->getInstance($code);
  53.             foreach ($admin->getRoutes()->getElements() as $route) {
  54.                 $name $route->getDefault('_sonata_name');
  55.                 \assert(\is_string($name));
  56.                 $collection->add($name$route);
  57.             }
  58.             $reflection = new \ReflectionObject($admin);
  59.             if (false !== $reflection->getFileName() && file_exists($reflection->getFileName())) {
  60.                 $collection->addResource(new FileResource($reflection->getFileName()));
  61.             }
  62.         }
  63.         return $collection;
  64.     }
  65. }