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 Symfony\Component\Console\Input\InputArgument
;
15 use Symfony\Component\Console\Input\InputInterface
;
16 use Symfony\Component\Console\Output\OutputInterface
;
18 use Rapsys\PackBundle\Command
;
23 * Shuffle printable character range
25 class RangeCommand
extends Command
{
29 * @description Shown with bin/console list
31 protected string $description = 'Outputs a shuffled printable characters range';
36 * @description Shown with bin/console --help packbundle:range
38 protected string $help = 'This command outputs a shuffled printable characters range';
43 public function __construct(protected string $file = '.env.local', 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 if (is_file($file = $input->getArgument('file')) && is_writeable($file)) {
83 if (($content = file_get_contents($file, false)) === false) {
85 error_log(sprintf('Unable to get %s content', $file), 0);
92 $string = 'RAPSYSPACK_RANGE="'.strtr(implode($shuffles), ['\\' => '\\\\', '"' => '\\"', '$' => '\\
$']).'"';
95 if (preg_match('/^RAPSYSPACK_RANGE=.*$/m', $content, $matches, PREG_OFFSET_CAPTURE
)) {
97 $content = preg_replace('/^(RAPSYSPACK_RANGE=.*)$/m', '#$1'."\n".strtr($string, ['\\
' => '\\\\
', '\\
$' => '\\\\$']), $content);
100 $content .= "\n".$string;
104 if (file_put_contents($file, $content) === false) {
106 error_log(sprintf('Unable to put %s content
', $file), 0);
109 return self::FAILURE;
114 //Without writeable file
117 echo '# Set in '.$file."\n";
119 //Print rapsys pack range variable
120 echo 'RAPSYSPACK_RANGE=';
122 //Print shuffled range
123 var_export(implode($shuffles));
127 return self
::SUCCESS
;