View source
<?php
namespace Drupal\apigee_edge\Entity;
use Drupal\apigee_edge\Access\AppsPageAccessCheck;
use Drupal\apigee_edge\Controller\DeveloperAppViewControllerForDeveloper;
use Drupal\apigee_edge\Controller\DeveloperAppKeysController;
use Drupal\apigee_edge\Entity\ListBuilder\DeveloperAppListBuilderForDeveloper;
use Drupal\apigee_edge\Form\DeveloperAppAnalyticsFormForDeveloper;
use Drupal\apigee_edge\Form\DeveloperAppApiKeyDeleteForm;
use Drupal\apigee_edge\Form\DeveloperAppApiKeyAddForm;
use Drupal\apigee_edge\Form\DeveloperAppApiKeyRevokeForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Symfony\Component\Routing\Route;
class DeveloperAppRouteProvider extends AppRouteProvider {
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
$entity_type_id = $entity_type
->id();
if ($collection_by_developer = $this
->getCollectionRouteByDeveloper($entity_type)) {
$collection
->add("entity.{$entity_type_id}.collection_by_developer", $collection_by_developer);
}
if ($canonical_by_developer = $this
->getCanonicalRouteByDeveloper($entity_type)) {
$collection
->add("entity.{$entity_type_id}.canonical_by_developer", $canonical_by_developer);
}
if ($add_form_for_developer = $this
->getAddFormRouteForDeveloper($entity_type)) {
$collection
->add("entity.{$entity_type_id}.add_form_for_developer", $add_form_for_developer);
}
if ($edit_form_for_developer = $this
->getEditFormRouteForDeveloper($entity_type)) {
$collection
->add("entity.{$entity_type_id}.edit_form_for_developer", $edit_form_for_developer);
}
if ($delete_form_for_developer = $this
->getDeleteFormRouteForDeveloper($entity_type)) {
$collection
->add("entity.{$entity_type_id}.delete_form_for_developer", $delete_form_for_developer);
}
if ($analytics_for_developer = $this
->getAnalyticsRouteForDeveloper($entity_type)) {
$collection
->add("entity.{$entity_type_id}.analytics_for_developer", $analytics_for_developer);
}
if ($api_keys = $this
->getApiKeyRouteDeveloperApp($entity_type)) {
$collection
->add("entity.{$entity_type_id}.api_keys", $api_keys);
}
if ($add_api_key_form = $this
->getAddApiKeyRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.add_api_key_form", $add_api_key_form);
}
if ($delete_api_key_form = $this
->getDeleteApiKeyRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.delete_api_key_form", $delete_api_key_form);
}
if ($revoke_api_key_form = $this
->getRevokeApiKeyRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.revoke_api_key_form", $revoke_api_key_form);
}
return $collection;
}
protected function getAddFormRoute(EntityTypeInterface $entity_type) {
$route = parent::getAddFormRoute($entity_type);
if ($route) {
$route
->setRequirement('_permission', 'administer developer_app');
}
return $route;
}
protected function getCollectionRoute(EntityTypeInterface $entity_type) {
$route = parent::getCollectionRoute($entity_type);
if ($route) {
$route
->setRequirement('_permission', $route
->getRequirement('_permission') . '+access developer_app overview');
}
return $route;
}
protected function getAddFormRouteForDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('add-form-for-developer')) {
$route = new Route($entity_type
->getLinkTemplate('add-form-for-developer'));
$route
->setDefault('_entity_form', 'developer_app.add_for_developer');
$route
->setDefault('_title_callback', AppTitleProvider::class . '::addTitle');
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureUserParameter($route);
$route
->setRequirement('_custom_access', AppsPageAccessCheck::class . '::createAppAccess');
return $route;
}
}
protected function getEditFormRouteForDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('edit-form-for-developer')) {
$route = new Route($entity_type
->getLinkTemplate('edit-form-for-developer'));
$route
->setDefault('_entity_form', 'developer_app.edit_for_developer');
$route
->setDefault('_title_callback', AppTitleProvider::class . '::editTitle');
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureUserParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'update');
$route
->setOption('apigee_edge_load_unchanged_entity', 'true');
return $route;
}
}
protected function getDeleteFormRouteForDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('delete-form-for-developer')) {
$route = new Route($entity_type
->getLinkTemplate('delete-form-for-developer'));
$route
->setDefault('_entity_form', 'developer_app.delete_for_developer');
$route
->setDefault('_title_callback', AppTitleProvider::class . '::deleteTitle');
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureUserParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'delete');
return $route;
}
}
protected function getCanonicalRouteByDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('canonical-by-developer')) {
$route = new Route($entity_type
->getLinkTemplate('canonical-by-developer'));
$route
->setDefault('_controller', DeveloperAppViewControllerForDeveloper::class . '::view');
$route
->setDefault('_title_callback', AppTitleProvider::class . ':title');
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureUserParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'view');
return $route;
}
}
protected function getCollectionRouteByDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('collection-by-developer')) {
$route = new Route($entity_type
->getLinkTemplate('collection-by-developer'));
$route
->setDefault('_controller', DeveloperAppListBuilderForDeveloper::class . '::render');
$route
->setDefault('_title_callback', DeveloperAppListBuilderForDeveloper::class . '::pageTitle');
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureUserParameter($route);
$route
->setRequirement('_custom_access', AppsPageAccessCheck::class . '::access');
return $route;
}
}
protected function getAnalyticsRouteForDeveloper(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('analytics-for-developer')) {
$route = new Route($entity_type
->getLinkTemplate('analytics-for-developer'));
$route
->setDefault('_form', DeveloperAppAnalyticsFormForDeveloper::class);
$route
->setDefault('_title_callback', AppTitleProvider::class . '::analyticsTitle');
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureUserParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'analytics');
return $route;
}
}
protected function getAddApiKeyRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('add-api-key-form')) {
$route = new Route($entity_type
->getLinkTemplate('add-api-key-form'));
$route
->setDefault('_form', DeveloperAppApiKeyAddForm::class);
$route
->setDefault('_title', 'Add key');
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureUserParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'add_api_key');
return $route;
}
}
protected function getDeleteApiKeyRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('delete-api-key-form')) {
$route = new Route($entity_type
->getLinkTemplate('delete-api-key-form'));
$route
->setDefault('_form', DeveloperAppApiKeyDeleteForm::class);
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureUserParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'delete_api_key');
return $route;
}
}
protected function getRevokeApiKeyRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('revoke-api-key-form')) {
$route = new Route($entity_type
->getLinkTemplate('revoke-api-key-form'));
$route
->setDefault('_form', DeveloperAppApiKeyRevokeForm::class);
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureUserParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'revoke_api_key');
return $route;
}
}
private function ensureUserParameter(Route $route) {
if (strpos($route
->getPath(), '{user}') !== FALSE) {
$route
->setRequirement('user', '\\d+');
$route
->setOption('parameters', [
'user' => [
'type' => 'entity:user',
'converter' => 'paramconverter.entity',
],
]);
}
}
protected function getApiKeyRouteDeveloperApp(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('api-keys')) {
$route = new Route($entity_type
->getLinkTemplate('api-keys'));
$route
->setDefault('_controller', DeveloperAppKeysController::class . '::developerAppKeys');
$route
->setDefault('_title_callback', AppTitleProvider::class . '::title');
$route
->setDefault('entity_type_id', $entity_type
->id());
$this
->ensureUserParameter($route);
$route
->setRequirement('_app_access_check_by_app_name', 'view');
return $route;
}
}
}