]> Raphaël G. Git Repositories - packbundle/blob - Command/RangeCommand.php
Cleanup trailing extra equals from hash method result
[packbundle] / Command / RangeCommand.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Command;
13
14 use Rapsys\PackBundle\Command;
15 use Rapsys\PackBundle\RapsysPackBundle;
16
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Output\OutputInterface;
20
21 /**
22 * {@inheritdoc}
23 *
24 * Shuffle printable character range
25 */
26 class RangeCommand extends Command {
27 /**
28 * Set description
29 *
30 * Shown with bin/console list
31 */
32 protected string $description = 'Outputs a shuffled printable characters range';
33
34 /**
35 * Set help
36 *
37 * Shown with bin/console --help rapsyspack:range
38 */
39 protected string $help = 'This command outputs a shuffled printable characters range';
40
41 /**
42 * {@inheritdoc}
43 */
44 public function __construct(protected string $file = '.env.local', protected ?string $name = null) {
45 //Call parent constructor
46 parent::__construct($this->name);
47
48 //Add argument
49 $this->addArgument('file', InputArgument::OPTIONAL, 'Environment file', $this->file);
50 }
51
52 /**
53 * {@inheritdoc}
54 *
55 * Output a shuffled printable characters range
56 */
57 protected function execute(InputInterface $input, OutputInterface $output): int {
58 //Printable character range
59 $ranges = range(' ', '~');
60
61 //Range shuffled
62 $shuffles = [];
63
64 //Shuffle range array
65 do {
66 //Set start offset
67 $offset = rand(0, ($count = count($ranges)) - 1);
68 //Set length
69 $length = rand(1, $count - $offset < ($ceil = (int)ceil(($count+count($shuffles))/rand(5,10))) ? $count - $offset : rand(2, $ceil));
70 //Splice array
71 $slices = array_splice($ranges, $offset, $length);
72 //When reverse
73 if (rand(0, 1)) {
74 //Reverse sliced array
75 $slices = array_reverse($slices);
76 }
77 //Append sliced array
78 $shuffles = array_merge($shuffles, $slices);
79 } while (!empty($ranges));
80
81 //Set string
82 $string = 'RAPSYSPACK_RANGE="'.strtr(implode($shuffles), ['\\' => '\\\\', '"' => '\\"', '$' => '\\$']).'"';
83
84 //With writeable file
85 if (is_file($file = $input->getArgument('file')) && is_writeable($file)) {
86 //Get file content
87 if (($content = file_get_contents($file, false)) === false) {
88 //Display error
89 error_log(sprintf('Unable to get %s content', $file), 0);
90
91 //Return failure
92 return self::FAILURE;
93 }
94
95 //With match
96 if (preg_match('/^RAPSYSPACK_RANGE=.*$/m', $content, $matches, PREG_OFFSET_CAPTURE)) {
97 //Replace matches
98 $content = preg_replace('/^(RAPSYSPACK_RANGE=.*)$/m', '#$1'."\n".strtr($string, ['\\' => '\\\\', '\\$' => '\\\\$']), $content);
99 //Without match
100 } else {
101 //Append string
102 $content .= (strlen($content)?"\n\n":'').'###> '.RapsysPackBundle::getBundleAlias().' ###'."\n".$string."\n".'###< '.RapsysPackBundle::getBundleAlias().' ###';
103 }
104
105 //Write file content
106 if (file_put_contents($file, $content) === false) {
107 //Display error
108 error_log(sprintf('Unable to put %s content', $file), 0);
109
110 //Return failure
111 return self::FAILURE;
112 }
113
114 //Print content
115 echo $content;
116 //Without writeable file
117 } else {
118 //Print instruction
119 echo '# Add to '.$file."\n";
120
121 //Print rapsys pack range variable
122 echo '###> '.RapsysPackBundle::getBundleAlias().' ###'."\n".$string."\n".'###< '.RapsysPackBundle::getBundleAlias().' ###';
123
124 //Add trailing line
125 echo "\n";
126 }
127
128 //Return success
129 return self::SUCCESS;
130 }
131 }