]> Raphaël G. Git Repositories - packbundle/blob - Command/RangeCommand.php
Readd getAlias member function
[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 Symfony\Component\Console\Input\InputArgument;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
17
18 use Rapsys\PackBundle\Command;
19
20 /**
21 * {@inheritdoc}
22 *
23 * Shuffle printable character range
24 */
25 class RangeCommand extends Command {
26 /**
27 * Set description
28 *
29 * @description Shown with bin/console list
30 */
31 protected string $description = 'Outputs a shuffled printable characters range';
32
33 /**
34 * Set help
35 *
36 * @description Shown with bin/console --help packbundle:range
37 */
38 protected string $help = 'This command outputs a shuffled printable characters range';
39
40 /**
41 * {@inheritdoc}
42 */
43 public function __construct(protected string $file = '.env.local', 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 //With writeable file
81 if (is_file($file = $input->getArgument('file')) && is_writeable($file)) {
82 //Get file content
83 if (($content = file_get_contents($file, false)) === false) {
84 //Display error
85 error_log(sprintf('Unable to get %s content', $file), 0);
86
87 //Return failure
88 return self::FAILURE;
89 }
90
91 //Set string
92 $string = 'RAPSYSPACK_RANGE="'.strtr(implode($shuffles), ['\\' => '\\\\', '"' => '\\"', '$' => '\\$']).'"';
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 $content .= "\n".$string;
101 }
102
103 //Write file content
104 if (file_put_contents($file, $content) === false) {
105 //Display error
106 error_log(sprintf('Unable to put %s content', $file), 0);
107
108 //Return failure
109 return self::FAILURE;
110 }
111
112 //Print content
113 echo $content;
114 //Without writeable file
115 } else {
116 //Print instruction
117 echo '# Set in '.$file."\n";
118
119 //Print rapsys pack range variable
120 echo 'RAPSYSPACK_RANGE=';
121
122 //Print shuffled range
123 var_export(implode($shuffles));
124 }
125
126 //Return success
127 return self::SUCCESS;
128 }
129 }