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
;
16 use Symfony\Component\Console\Input\InputArgument
;
17 use Symfony\Component\Console\Input\InputInterface
;
18 use Symfony\Component\Console\Output\OutputInterface
;
23 * Shuffle printable character range
25 class RangeCommand
extends Command
{
29 * Shown with bin/console list
31 protected string $description = 'Generates a shuffled printable characters range';
36 * Shown with bin/console --help rapsyspack:range
38 protected string $help = 'This command generates a shuffled printable characters range';
43 public function __construct(protected string $file, protected ?string $name = null) {
44 //Call parent constructor
45 parent
::__construct($this->name
);
48 $this->addArgument('file', InputArgument
::OPTIONAL
, 'Environment file', $this->file
);
54 * Output a shuffled printable characters range
56 protected function execute(InputInterface
$input, OutputInterface
$output): int {
57 //Printable character range
58 $ranges = range(' ', '~');
66 $offset = rand(0, ($count = count($ranges)) - 1);
68 $length = rand(1, $count - $offset < ($ceil = (int)ceil(($count+
count($shuffles))/rand(5,10))) ? $count - $offset : rand(2, $ceil));
70 $slices = array_splice($ranges, $offset, $length);
73 //Reverse sliced array
74 $slices = array_reverse($slices);
77 $shuffles = array_merge($shuffles, $slices);
78 } while (!empty($ranges));
81 $string = 'RAPSYSPACK_RANGE="'.strtr(implode($shuffles), ['\\' => '\\\\', '"' => '\\"', '$' => '\\
$']).'"';
84 if (is_file($file = $input->getArgument('file')) && is_writeable($file)) {
86 if (($content = file_get_contents($file, false)) === false) {
88 error_log(sprintf('Unable to get %s content', $file), 0);
95 if (preg_match('/^RAPSYSPACK_RANGE=.*$/m', $content, $matches, PREG_OFFSET_CAPTURE
)) {
97 $content = preg_replace('/^(RAPSYSPACK_RANGE=.*)$/m', '#$1'."\n".strtr($string, ['\\
' => '\\\\
', '\\
$' => '\\\\$']), $content);
101 $content .= (strlen($content)?"\n\n":'').'###> '.$this->bundle.' ###'."\n".$string."\n".'###< '.$this->bundle.' ###';
105 if (file_put_contents($file, $content) === false) {
107 error_log(sprintf('Unable to put %s content', $file), 0);
110 return self
::FAILURE
;
115 //Without writeable file
118 echo '# Add to '.$file."\n";
120 //Print rapsys pack range variable
121 echo '###> '.$this->bundle
.' ###'."\n".$string."\n".'###< '.$this->bundle
.' ###';
128 return self
::SUCCESS
;