1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys PackBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\PackBundle\Command
; 
  14 use Rapsys\PackBundle\Command
; 
  15 use Rapsys\PackBundle\RapsysPackBundle
; 
  17 use Symfony\Component\Console\Input\InputArgument
; 
  18 use Symfony\Component\Console\Input\InputInterface
; 
  19 use Symfony\Component\Console\Output\OutputInterface
; 
  24  * Shuffle printable character range 
  26 class RangeCommand 
extends Command 
{ 
  30          * Shown with bin/console list 
  32         protected string $description = 'Outputs a shuffled printable characters range'; 
  37          * Shown with bin/console --help rapsyspack:range 
  39         protected string $help = 'This command outputs a shuffled printable characters range'; 
  44         public function __construct(protected string $file = '.env.local', protected ?string $name = null) { 
  45                 //Call parent constructor 
  46                 parent
::__construct($this->name
); 
  49                 $this->addArgument('file', InputArgument
::OPTIONAL
, 'Environment file', $this->file
); 
  55          * Output a shuffled printable characters range 
  57         protected function execute(InputInterface 
$input, OutputInterface 
$output): int { 
  58                 //Printable character range 
  59                 $ranges = range(' ', '~'); 
  67                         $offset = rand(0, ($count = count($ranges)) - 1); 
  69                         $length = rand(1, $count - $offset < ($ceil = (int)ceil(($count+
count($shuffles))/rand(5,10))) ? $count - $offset : rand(2, $ceil)); 
  71                         $slices = array_splice($ranges, $offset, $length); 
  74                                 //Reverse sliced array 
  75                                 $slices = array_reverse($slices); 
  78                         $shuffles = array_merge($shuffles, $slices); 
  79                 } while (!empty($ranges)); 
  82                 $string = 'RAPSYSPACK_RANGE="'.strtr(implode($shuffles), ['\\' => '\\\\', '"' => '\\"', '$' => '\\
$']).'"'; 
  85                 if (is_file($file = $input->getArgument('file')) && is_writeable($file)) { 
  87                         if (($content = file_get_contents($file, false)) === false) { 
  89                                 error_log(sprintf('Unable to get %s content', $file), 0); 
  96                         if (preg_match('/^RAPSYSPACK_RANGE=.*$/m', $content, $matches, PREG_OFFSET_CAPTURE
)) { 
  98                                 $content = preg_replace('/^(RAPSYSPACK_RANGE=.*)$/m', '#$1'."\n".strtr($string, ['\\
' => '\\\\
', '\\
$' => '\\\\$']), $content); 
 102                                 $content .= (strlen($content)?"\n\n":'').'###> '.RapsysPackBundle::getBundleAlias().' ###'."\n".$string."\n".'###< '.RapsysPackBundle::getBundleAlias().' ###'; 
 106                         if (file_put_contents($file, $content) === false) { 
 108                                 error_log(sprintf('Unable to put %s content', $file), 0); 
 111                                 return self
::FAILURE
; 
 116                 //Without writeable file 
 119                         echo '# Add to '.$file."\n"; 
 121                         //Print rapsys pack range variable 
 122                         echo '###> '.RapsysPackBundle
::getBundleAlias().' ###'."\n".$string."\n".'###< '.RapsysPackBundle
::getBundleAlias().' ###'; 
 129                 return self
::SUCCESS
;