From c26a8dae7bf279375e16134131983f50307f45a2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 11 Aug 2021 20:41:43 +0200 Subject: [PATCH] Generate alias from class name --- RapsysUserBundle.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/RapsysUserBundle.php b/RapsysUserBundle.php index c083bb0..b347c51 100644 --- a/RapsysUserBundle.php +++ b/RapsysUserBundle.php @@ -2,6 +2,36 @@ namespace Rapsys\UserBundle; +use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpKernel\Bundle\Bundle; -class RapsysUserBundle extends Bundle {} +class RapsysUserBundle extends Bundle { + /** + * Return bundle alias + * + * @return string The bundle alias + */ + public function getAlias(): string { + //With namespace + if ($npos = strrpos(static::class, '\\')) { + //Set name pos + $npos++; + //Without namespace + } else { + $npos = 0; + } + + //With trailing bundle + if (substr(static::class, -strlen('Bundle'), strlen('Bundle')) === 'Bundle') { + //Set bundle pos + $bpos = strlen(static::class) - $npos - strlen('Bundle'); + //Without bundle + } else { + //Set bundle pos + $bpos = strlen(static::class) - $npos; + } + + //Return underscored lowercase bundle alias + return Container::underscore(substr(static::class, $npos, $bpos)); + } +} -- 2.41.1