Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / lipsum / management / PhabricatorLipsumGenerateWorkflow.php
blob24301640ed4eeeeaf973190ae08bcc06cbb3e6e2
1 <?php
3 final class PhabricatorLipsumGenerateWorkflow
4 extends PhabricatorLipsumManagementWorkflow {
6 protected function didConstruct() {
7 $this
8 ->setName('generate')
9 ->setExamples('**generate**')
10 ->setSynopsis(pht('Generate synthetic test objects.'))
11 ->setArguments(
12 array(
13 array(
14 'name' => 'force',
15 'short' => 'f',
16 'help' => pht(
17 'Generate objects without prompting for confirmation.'),
19 array(
20 'name' => 'quickly',
21 'help' => pht(
22 'Generate objects as quickly as possible.'),
24 array(
25 'name' => 'args',
26 'wildcard' => true,
28 ));
31 public function execute(PhutilArgumentParser $args) {
32 $config_key = 'phabricator.developer-mode';
33 if (!PhabricatorEnv::getEnvConfig($config_key)) {
34 throw new PhutilArgumentUsageException(
35 pht(
36 'lipsum is a development and testing tool and may only be run '.
37 'on installs in developer mode. Enable "%s" in your configuration '.
38 'to enable lipsum.',
39 $config_key));
42 $all_generators = id(new PhutilClassMapQuery())
43 ->setAncestorClass('PhabricatorTestDataGenerator')
44 ->setUniqueMethod('getGeneratorKey')
45 ->execute();
47 $argv = $args->getArg('args');
48 $is_force = $args->getArg('force');
49 $is_quickly = $args->getArg('quickly');
51 $all = 'all';
53 if (isset($all_generators[$all])) {
54 throw new Exception(
55 pht(
56 'A lipsum generator is registered with key "%s". This key is '.
57 'reserved.',
58 $all));
61 if (!$argv) {
62 ksort($all_generators);
64 $names = array();
65 foreach ($all_generators as $generator) {
66 $names[] = tsprintf(
67 '%s (%s)',
68 $generator->getGeneratorKey(),
69 $generator->getGeneratorName());
72 $list = id(new PhutilConsoleList())
73 ->setWrap(false)
74 ->addItems($names);
76 id(new PhutilConsoleBlock())
77 ->addParagraph(
78 pht(
79 'Choose which type or types of test data you want to generate, '.
80 'or select "%s".',
81 $all))
82 ->addList($list)
83 ->draw();
85 return 0;
88 $generators = array();
89 foreach ($argv as $arg_original) {
90 $arg = phutil_utf8_strtolower($arg_original);
92 if ($arg == 'all') {
93 $matches = $all_generators;
94 } else {
95 $matches = array();
96 foreach ($all_generators as $generator) {
97 $name = phutil_utf8_strtolower($generator->getGeneratorKey());
99 // If there's an exact match, select just that generator.
100 if ($arg == $name) {
101 $matches = array($generator);
102 break;
105 // If there's a partial match, match that generator but continue.
106 if (strpos($name, $arg) !== false) {
107 $matches[] = $generator;
111 if (!$matches) {
112 throw new PhutilArgumentUsageException(
113 pht(
114 'Argument "%s" does not match the name of any generators.',
115 $arg_original));
118 if (count($matches) > 1) {
119 throw new PhutilArgumentUsageException(
120 pht(
121 'Argument "%s" is ambiguous, and matches multiple '.
122 'generators: %s.',
123 $arg_original,
124 implode(', ', mpull($matches, 'getGeneratorName'))));
128 foreach ($matches as $match) {
129 $generators[] = $match;
133 $generators = mpull($generators, null, 'getGeneratorKey');
135 echo tsprintf(
136 "**<bg:blue> %s </bg>** %s\n",
137 pht('GENERATORS'),
138 pht(
139 'Selected generators: %s.',
140 implode(', ', mpull($generators, 'getGeneratorName'))));
142 if (!$is_force) {
143 echo tsprintf(
144 "**<bg:yellow> %s </bg>** %s\n",
145 pht('WARNING'),
146 pht(
147 'This command generates synthetic test data, including user '.
148 'accounts. It is intended for use in development environments so '.
149 'you can test features more easily. There is no easy way to delete '.
150 'this data or undo the effects of this command. If you run it in a '.
151 'production environment, it will pollute your data with large '.
152 'amounts of meaningless garbage that you can not get rid of.'));
154 $prompt = pht('Are you sure you want to generate piles of garbage?');
155 if (!phutil_console_confirm($prompt, true)) {
156 return;
160 echo tsprintf(
161 "**<bg:green> %s </bg>** %s\n",
162 pht('LIPSUM'),
163 pht(
164 'Generating synthetic test objects forever. '.
165 'Use ^C to stop when satisfied.'));
167 $this->generate($generators, $is_quickly);
170 protected function generate(array $generators, $is_quickly) {
171 $viewer = $this->getViewer();
173 foreach ($generators as $generator) {
174 $generator->setViewer($this->getViewer());
177 while (true) {
178 $generator = $generators[array_rand($generators)];
180 try {
181 $object = $generator->generateObject();
182 } catch (Exception $ex) {
183 echo tsprintf(
184 "**<bg:yellow> %s </bg>** %s\n",
185 pht('OOPS'),
186 pht(
187 'Generator ("%s") was unable to generate an object.',
188 $generator->getGeneratorName()));
190 echo tsprintf(
191 "%B\n",
192 $ex->getMessage());
194 continue;
197 if (is_string($object)) {
198 $object_phid = $object;
199 } else {
200 $object_phid = $object->getPHID();
203 $handles = $viewer->loadHandles(array($object_phid));
205 echo tsprintf(
206 "%s\n",
207 pht(
208 'Generated "%s": %s',
209 $handles[$object_phid]->getTypeName(),
210 $handles[$object_phid]->getFullName()));
212 if (!$is_quickly) {
213 sleep(1);