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