providers[] = $provider; } public function getRoles(): array { $roles = []; foreach ($this->providers as $provider) { if ($provider->getRoles() !== null) { $roles = array_merge($roles, $provider->getRoles()); } } return $roles; } public function getRolesWithoutScopes(): array { $roles = []; foreach ($this->providers as $provider) { if ($provider->getRolesWithoutScope() !== null) { $roles = array_merge($roles, $provider->getRolesWithoutScope()); } } return $roles; } /** * Get the title for each role. * * @param string $role * * @return string the title of the role */ public function getRoleTitle($role) { $this->initializeRolesTitlesCache(); if (!array_key_exists($role, $this->rolesTitlesCache)) { // this case might happens when the role is not described in // `getRolesWithHierarchy` return null; } return $this->rolesTitlesCache[$role]; } /** * initialize the array for caching role and titles. */ private function initializeRolesTitlesCache() { // break if already initialized if (null !== $this->rolesTitlesCache) { return; } foreach ($this->providers as $provider) { if ($provider instanceof ProvideRoleHierarchyInterface) { foreach ($provider->getRolesWithHierarchy() as $title => $roles) { foreach ($roles as $role) { $this->rolesTitlesCache[$role] = $title; } } } else { if ($provider->getRoles() !== null) { $this->rolesTitlesCache = array_merge( $this->rolesTitlesCache, array_fill_keys($provider->getRoles(), null) ); } } } } }