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