From ce4ef48d51a312fa5f854e91f3825a699d9325e8 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Rapha=C3=ABl=20Gertz?= <git@rapsys.eu>
Date: Wed, 11 Aug 2021 20:33:30 +0200
Subject: [PATCH] Move alias generation in RapsysPackBundle::getAlias

---
 DependencyInjection/Configuration.php       |  4 ++-
 DependencyInjection/RapsysPackExtension.php |  4 ++-
 Extension/PackExtension.php                 |  5 ++--
 RapsysPackBundle.php                        | 32 ++++++++++++++++++++-
 4 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php
index 0265db3..7217e98 100644
--- a/DependencyInjection/Configuration.php
+++ b/DependencyInjection/Configuration.php
@@ -15,6 +15,8 @@ use Symfony\Component\Config\Definition\Builder\TreeBuilder;
 use Symfony\Component\Config\Definition\ConfigurationInterface;
 use Symfony\Component\Process\ExecutableFinder;
 
+use Rapsys\PackBundle\RapsysPackBundle;
+
 /**
  * This is the class that validates and merges configuration from your app/config files.
  *
@@ -28,7 +30,7 @@ class Configuration implements ConfigurationInterface {
 	 */
 	public function getConfigTreeBuilder(): TreeBuilder {
 		//Get TreeBuilder object
-		$treeBuilder = new TreeBuilder('rapsys_pack');
+		$treeBuilder = new TreeBuilder(RapsysPackBundle::getAlias());
 
 		//Get ExecutableFinder object
 		$finder = new ExecutableFinder();
diff --git a/DependencyInjection/RapsysPackExtension.php b/DependencyInjection/RapsysPackExtension.php
index 5bf3008..94b9fad 100644
--- a/DependencyInjection/RapsysPackExtension.php
+++ b/DependencyInjection/RapsysPackExtension.php
@@ -14,6 +14,8 @@ namespace Rapsys\PackBundle\DependencyInjection;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Symfony\Component\DependencyInjection\Extension\Extension;
 
+use Rapsys\PackBundle\RapsysPackBundle;
+
 /**
  * This is the class that loads and manages your bundle configuration.
  *
@@ -46,6 +48,6 @@ class RapsysPackExtension extends Extension {
 	 * {@inheritdoc}
 	 */
 	public function getAlias(): string {
-		return 'rapsys_pack';
+		return RapsysPackBundle::getAlias();
 	}
 }
diff --git a/Extension/PackExtension.php b/Extension/PackExtension.php
index 118de0c..48f93cd 100644
--- a/Extension/PackExtension.php
+++ b/Extension/PackExtension.php
@@ -16,8 +16,9 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpKernel\Config\FileLocator;
 use Twig\Extension\AbstractExtension;
 
-use Rapsys\PackBundle\Util\SluggerUtil;
 use Rapsys\PackBundle\Parser\TokenParser;
+use Rapsys\PackBundle\RapsysPackBundle;
+use Rapsys\PackBundle\Util\SluggerUtil;
 
 /**
  * {@inheritdoc}
@@ -95,6 +96,6 @@ class PackExtension extends AbstractExtension {
 	 * {@inheritdoc}
 	 */
 	public function getAlias(): string {
-		return 'rapsys_pack';
+		return RapsysPackBundle::getAlias();
 	}
 }
diff --git a/RapsysPackBundle.php b/RapsysPackBundle.php
index 260ce22..ca879af 100644
--- a/RapsysPackBundle.php
+++ b/RapsysPackBundle.php
@@ -11,9 +11,39 @@
 
 namespace Rapsys\PackBundle;
 
+use Symfony\Component\DependencyInjection\Container;
 use Symfony\Component\HttpKernel\Bundle\Bundle;
 
 /**
  * {@inheritdoc}
  */
-class RapsysPackBundle extends Bundle {}
+class RapsysPackBundle 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.3