]> Raphaël G. Git Repositories - airbundle/commitdiff
Add getName member function
authorRaphaël Gertz <git@rapsys.eu>
Thu, 7 Mar 2024 20:43:28 +0000 (21:43 +0100)
committerRaphaël Gertz <git@rapsys.eu>
Thu, 7 Mar 2024 20:43:28 +0000 (21:43 +0100)
Reorder constructor arguments
Use env variables to set request context host and scheme
Cleanup

Command.php

index 178648c3e19aaf4b24131bb3d005fed514edb4fc..c775499fab5747269334d4ffc4a42968741b8693 100644 (file)
@@ -12,6 +12,7 @@
 namespace Rapsys\AirBundle;
 
 use Doctrine\Persistence\ManagerRegistry;
+
 use Symfony\Component\Console\Command\Command as BaseCommand;
 use Symfony\Component\Routing\RouterInterface;
 use Symfony\Contracts\Translation\TranslatorInterface;
@@ -20,8 +21,13 @@ use Rapsys\AirBundle\RapsysAirBundle;
 
 use Rapsys\PackBundle\Util\SluggerUtil;
 
+/**
+ * {@inheritdoc}
+ */
 class Command extends BaseCommand {
        /**
+        * {@inheritdoc}
+        *
         * Creates new command
         *
         * @param ManagerRegistry $doctrine The doctrine instance
@@ -30,39 +36,61 @@ class Command extends BaseCommand {
         * @param TranslatorInterface $translator The translator instance
         * @param string $locale The default locale
         */
-       public function __construct(protected ManagerRegistry $doctrine, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected string $locale) {
+       public function __construct(protected ManagerRegistry $doctrine, protected string $locale, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected ?string $name = null) {
+               //Fix name
+               $this->name = $this->name ?? static::getName();
+
                //Call parent constructor
-               parent::__construct();
+               parent::__construct($this->name);
+
+               //With description
+               if (!empty($this->description)) {
+                       //Set description
+                       $this->setDescription($this->description);
+               }
+
+               //With help
+               if (!empty($this->help)) {
+                       //Set help
+                       $this->setHelp($this->help);
+               }
 
                //Get router context
                $context = $this->router->getContext();
 
-               //Set host
-               //TODO: set it in env RAPSYSAIR_HOST ?
-               $context->setHost('airlibre.eu');
+               //Set hostname
+               $context->setHost($_ENV['RAPSYSAIR_HOSTNAME']);
 
                //Set scheme
-               //TODO: set it in env RAPSYSAIR_SCHEME ?
-               $context->setScheme('https');
-
-               //With default name
-               //TODO: XXX: see how to make it works
-               /*if (isset(self::$defaultName)) {
-                       $this->name = self::$defaultName;
-               }
-
-               //With default description
-               if (isset(self::$defaultDescription)) {
-                       $this->name = self::$defaultDescription;
-               }*/
+               $context->setScheme($_ENV['RAPSYSAIR_SCHEME'] ?? 'https');
        }
 
        /**
-        * Return the bundle alias
-        *
         * {@inheritdoc}
+        *
+        * Return the command name
         */
-       public function getAlias(): string {
-               return RapsysAirBundle::getAlias();
+       public function getName(): string {
+               //With namespace
+               if ($npos = strrpos(static::class, '\\')) {
+                       //Set name pos
+                       $npos++;
+               //Without namespace
+               } else {
+                       $npos = 0;
+               }
+
+               //With trailing command
+               if (substr(static::class, -strlen('Command'), strlen('Command')) === 'Command') {
+                       //Set bundle pos
+                       $bpos = strlen(static::class) - $npos - strlen('Command');
+               //Without bundle
+               } else {
+                       //Set bundle pos
+                       $bpos = strlen(static::class) - $npos;
+               }
+
+               //Return command alias
+               return RapsysAirBundle::getAlias().':'.strtolower(substr(static::class, $npos, $bpos));
        }
 }