Routes.php in Schemata 8
File
src/Routing/Routes.php
View source
<?php
namespace Drupal\schemata\Routing;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class Routes implements ContainerInjectionInterface {
const CONTROLLER = '\\Drupal\\schemata\\Controller\\Controller::serialize';
protected $entityTypeManager;
protected $entityTypeBundleInfo;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'));
}
public function routes() {
$route_collection = new RouteCollection();
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type) {
$entity_type_id = $entity_type
->id();
$route_collection
->add($this
->createRouteName($entity_type_id), $this
->createRoute($entity_type_id));
if ($entity_type
->getBundleEntityType()) {
$bundles_info = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
foreach (array_keys($bundles_info) as $bundle) {
$route_collection
->add($this
->createRouteName($entity_type_id, $bundle), $this
->createRoute($entity_type_id, $bundle));
}
}
}
return $route_collection;
}
protected function createRoute($entity_type_id, $bundle = NULL) {
$path = $this
->getRoutePath($entity_type_id, $bundle);
$route = new Route($path);
$route
->setRequirement('_permission', 'access schemata data models');
$route
->setMethods([
'GET',
]);
$defaults = [
'entity_type_id' => $entity_type_id,
RouteObjectInterface::CONTROLLER_NAME => static::CONTROLLER,
];
if ($bundle) {
$defaults['bundle'] = $bundle;
}
$route
->setDefaults($defaults);
return $route;
}
protected function createRouteName($entity_type_id, $bundle = NULL) {
return $bundle ? sprintf('schemata.%s:%s', $entity_type_id, $bundle) : sprintf('schemata.%s', $entity_type_id);
}
protected function getRoutePath($entity_type_id, $bundle = NULL) {
$path = $bundle ? sprintf('/schemata/%s/%s', $entity_type_id, $bundle) : sprintf('/schemata/%s', $entity_type_id);
return $path;
}
}