]> Raphaƫl G. Git Repositories - packbundle/blobdiff - Command/RangeCommand.php
Version 0.5.3
[packbundle] / Command / RangeCommand.php
index e18fafac38aa392750fb0960626d3473c9b0b226..e4f15daf80a9f015f302e8365aeb860657b7be1f 100644 (file)
 
 namespace Rapsys\PackBundle\Command;
 
+use Rapsys\PackBundle\Command;
+use Rapsys\PackBundle\RapsysPackBundle;
+
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 
-use Rapsys\PackBundle\Command;
-
 /**
- * Shuffle printable character range
- *
  * {@inheritdoc}
+ *
+ * Shuffle printable character range
  */
 class RangeCommand extends Command {
        /**
         * Set description
         *
-        * @description Shown with bin/console list
+        * Shown with bin/console list
         */
        protected string $description = 'Outputs a shuffled printable characters range';
 
        /**
         * Set help
         *
-        * @description Shown with bin/console --help packbundle:range
+        * Shown with bin/console --help rapsyspack:range
         */
        protected string $help = 'This command outputs a shuffled printable characters range';
 
        /**
         * {@inheritdoc}
         */
-       public function __construct(protected ?string $name = null, protected string $file = '.env.local') {
+       public function __construct(protected string $file = '.env.local', protected ?string $name = null) {
                //Call parent constructor
                parent::__construct($this->name);
 
@@ -49,9 +50,9 @@ class RangeCommand extends Command {
        }
 
        /**
-        * Output a shuffled printable characters range
-        *
         * {@inheritdoc}
+        *
+        * Output a shuffled printable characters range
         */
        protected function execute(InputInterface $input, OutputInterface $output): int {
                //Printable character range
@@ -77,6 +78,9 @@ class RangeCommand extends Command {
                        $shuffles = array_merge($shuffles, $slices);
                } while (!empty($ranges));
 
+               //Set string
+               $string = 'RAPSYSPACK_RANGE="'.strtr(implode($shuffles), ['\\' => '\\\\', '"' => '\\"', '$' => '\\$']).'"';
+
                //With writeable file
                if (is_file($file = $input->getArgument('file')) && is_writeable($file)) {
                        //Get file content
@@ -88,16 +92,14 @@ class RangeCommand extends Command {
                                return self::FAILURE;
                        }
 
-                       //Set string
-                       $string = 'RAPSYSPACK_RANGE="'.strtr(implode($shuffles), ['\\' => '\\\\', '"' => '\\"', '$' => '\\$']).'"';
-
                        //With match
                        if (preg_match('/^RAPSYSPACK_RANGE=.*$/m', $content, $matches, PREG_OFFSET_CAPTURE)) {
                                //Replace matches
                                $content = preg_replace('/^(RAPSYSPACK_RANGE=.*)$/m', '#$1'."\n".strtr($string, ['\\' => '\\\\', '\\$' => '\\\\$']), $content);
                        //Without match
                        } else {
-                               $content .= "\n".$string;
+                               //Append string
+                               $content .= (strlen($content)?"\n\n":'').'###> '.RapsysPackBundle::getBundleAlias().' ###'."\n".$string."\n".'###< '.RapsysPackBundle::getBundleAlias().' ###';
                        }
 
                        //Write file content
@@ -114,13 +116,13 @@ class RangeCommand extends Command {
                //Without writeable file
                } else {
                        //Print instruction
-                       echo '# Set in '.$file."\n";
+                       echo '# Add to '.$file."\n";
 
                        //Print rapsys pack range variable
-                       echo 'RAPSYSPACK_RANGE=';
+                       echo '###> '.RapsysPackBundle::getBundleAlias().' ###'."\n".$string."\n".'###< '.RapsysPackBundle::getBundleAlias().' ###';
 
-                       //Print shuffled range
-                       var_export(implode($shuffles));
+                       //Add trailing line
+                       echo "\n";
                }
 
                //Return success