/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { parent::interact($input, $output); $io = new DrupalStyle($input, $output); // --bundle-of option $bundle_of = $input->getOption('has-bundles'); if (!$bundle_of) { $bundle_of = $io->confirm($this->trans('commands.generate.entity.content.questions.has-bundles'), false); $input->setOption('has-bundles', $bundle_of); } // --is-translatable option $is_translatable = $io->confirm($this->trans('commands.generate.entity.content.questions.is-translatable'), true); $input->setOption('is-translatable', $is_translatable); }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); // --module option $module = $input->getOption('module'); if (!$module) { // @see Drupal\Console\Command\ModuleTrait::moduleQuestion $module = $this->moduleQuestion($output); $input->setOption('module', $module); } // --name $name = $input->getOption('name'); if (!$name) { $name = $io->ask($this->trans('commands.generate.command.questions.name'), sprintf('%s:default', $module)); $input->setOption('name', $name); } // --class option $class = $input->getOption('class'); if (!$class) { $class = $io->ask($this->trans('commands.generate.command.questions.class'), 'DefaultCommand', function ($class) { return $this->getValidator()->validateCommandName($class); }); $input->setOption('class', $class); } // --container-aware option $containerAware = $input->getOption('container-aware'); if (!$containerAware) { $io->confirm($this->trans('commands.generate.command.questions.container-aware'), true); } $input->setOption('container-aware', $containerAware); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $application = $this->getApplication(); $manifest = $input->getOption('manifest') ?: 'http://drupalconsole.com/manifest.json'; $currentVersion = $input->getOption('current-version') ?: $application->getVersion(); $major = $input->getOption('major'); if (!extension_loaded('Phar') || !\Phar::running(false)) { $io->error($this->trans('commands.self-update.messages.not-phar')); $io->block($this->trans('commands.self-update.messages.instructions')); return 1; } $io->info(sprintf($this->trans('commands.self-update.messages.check'), $currentVersion)); $updater = new Updater(null, false); $strategy = new ManifestStrategy($currentVersion, $major, $manifest); $updater->setStrategyObject($strategy); if (!$updater->hasUpdate()) { $io->info(sprintf($this->trans('commands.self-update.messages.current-version'), $currentVersion)); return 0; } $oldVersion = $updater->getOldVersion(); $newVersion = $updater->getNewVersion(); if (!$io->confirm(sprintf($this->trans('commands.self-update.questions.update'), $oldVersion, $newVersion), true)) { return 1; } $io->comment(sprintf($this->trans('commands.self-update.messages.update'), $newVersion)); $updater->update(); $io->success(sprintf($this->trans('commands.self-update.messages.success'), $oldVersion, $newVersion)); // Errors appear if new classes are instantiated after this stage // (namely, Symfony's ConsoleTerminateEvent). This suggests PHP // can't read files properly from the overwritten Phar, or perhaps it's // because the autoloader's name has changed. We avoid the problem by // terminating now. exit; }
/** * @param DrupalStyle $io * * @return bool */ public function confirmGeneration(DrupalStyle $io) { $confirmation = $io->confirm($this->trans('commands.common.questions.confirm'), true); if (!$confirmation) { $io->warning($this->trans('commands.common.messages.canceled')); } return $confirmation; }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { parent::interact($input, $output); $output = new DrupalStyle($input, $output); // --bundle-of option $bundle_of = $input->getOption('has-bundles'); if (!$bundle_of) { $bundle_of = $output->confirm($this->trans('commands.generate.entity.questions.has-bundles'), false); $input->setOption('has-bundles', $bundle_of); } }
/** * @param DrupalStyle $io * * @return mixed */ public function regionQuestion(DrupalStyle $io) { $validators = $this->validator; $regions = []; while (true) { $regionName = $io->ask($this->trans('commands.generate.theme.questions.region-name'), 'Content'); $regionMachineName = $this->stringConverter->createMachineName($regionName); $regionMachineName = $io->ask($this->trans('commands.generate.theme.questions.region-machine-name'), $regionMachineName, function ($regionMachineName) use($validators) { return $validators->validateMachineName($regionMachineName); }); array_push($regions, ['region_name' => $regionName, 'region_machine_name' => $regionMachineName]); if (!$io->confirm($this->trans('commands.generate.theme.questions.region-add'), true)) { break; } } return $regions; }
/** * @param \Drupal\Console\Style\DrupalStyle $io * @param string $className The form class name * @return string * @throws \Exception */ public function menuQuestion(DrupalStyle $io, $className) { if ($io->confirm($this->trans('commands.generate.form.questions.menu_link_gen'), true)) { // now we need to ask them where to gen the form // get the route $menu_options = ['menu_link_gen' => true]; $menu_link_title = $io->ask($menu_link_title = $this->trans('commands.generate.form.questions.menu_link_title'), $className); $menuLinkFile = sprintf('%s/core/modules/system/system.links.menu.yml', $this->getSite()->getSiteRoot()); $config = $this->getApplication()->getConfig(); $menuLinkContent = $config->getFileContents($menuLinkFile); $menu_parent = $io->choiceNoList($menu_parent = $this->trans('commands.generate.form.questions.menu_parent'), array_keys($menuLinkContent), 'system.admin_config_system'); $menu_link_desc = $io->ask($menu_link_desc = $this->trans('commands.generate.form.questions.menu_link_desc'), 'A description for the menu entry'); $menu_options['menu_link_title'] = $menu_link_title; $menu_options['menu_parent'] = $menu_parent; $menu_options['menu_link_desc'] = $menu_link_desc; return $menu_options; } }
/** * @param DrupalStyle $output * * @return mixed */ public function permissionQuestion(DrupalStyle $output) { $permissions = []; $boolOrNone = ['true', 'false', 'none']; while (true) { $permission = $output->ask($this->trans('commands.generate.permission.questions.permission'), 'access content'); $title = $output->ask($this->trans('commands.generate.permission.questions.title'), 'Access content'); $description = $output->ask($this->trans('commands.generate.permission.questions.description'), 'Allow access to my content'); $restrictAccess = $output->choiceNoList($this->trans('commands.generate.permission.questions.restrict-access'), $boolOrNone, 'none'); $permission = $this->stringConverter->camelCaseToLowerCase($permission); $title = $this->stringConverter->anyCaseToUcFirst($title); array_push($permissions, ['permission' => $permission, 'title' => $title, 'description' => $description, 'restrict_access' => $restrictAccess]); if (!$output->confirm($this->trans('commands.generate.permission.questions.add'), true)) { break; } } return $permissions; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $modules = $input->getArgument('module'); $overwriteConfig = $input->getOption('overwrite-config'); $validator = $this->getValidator(); $moduleInstaller = $this->getModuleInstaller(); $invalidModules = $validator->getInvalidModules($modules); if ($invalidModules) { $io->error(sprintf($this->trans('commands.module.install.messages.missing'), implode(', ', $modules), implode(', ', $invalidModules))); return; } $unInstalledModules = $validator->getUninstalledModules($modules); if (!$unInstalledModules) { $io->warning($this->trans('commands.module.install.messages.nothing')); return; } $dependencies = $this->calculateDependencies($unInstalledModules); $missingDependencies = $validator->getInvalidModules($dependencies); if ($missingDependencies) { $io->error(sprintf($this->trans('commands.module.install.messages.missing-dependencies'), implode(', ', $modules), implode(', ', $missingDependencies))); return true; } if ($dependencies) { if (!$io->confirm(sprintf($this->trans('commands.module.install.messages.dependencies'), implode(', ', $dependencies)), false)) { return; } } $moduleList = array_merge($unInstalledModules, $dependencies); try { $moduleInstaller->install($moduleList); $io->success(sprintf($this->trans('commands.module.install.messages.success'), implode(', ', $moduleList))); } catch (PreExistingConfigException $e) { $this->overwriteConfig($io, $e, $moduleList, $overwriteConfig); return; } catch (\Exception $e) { $io->error($e->getMessage()); return; } // Run cache rebuild to see changes in Web UI $this->getChain()->addCommand('cache:rebuild', ['cache' => 'all']); }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); // --module option $module = $input->getOption('module'); if (!$module) { // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion $module = $this->moduleQuestion($output); $input->setOption('module', $module); } //--name option $name = $input->getOption('name'); if (!$name) { $name = $io->ask($this->trans('commands.generate.service.questions.service-name'), $module . '.default'); $input->setOption('name', $name); } // --class option $class = $input->getOption('class'); if (!$class) { $class = $io->ask($this->trans('commands.generate.service.questions.class'), 'DefaultService'); $input->setOption('class', $class); } // --interface option $interface = $input->getOption('interface'); if (!$interface) { $interface = $io->confirm($this->trans('commands.generate.service.questions.interface'), true); $input->setOption('interface', $interface); } // --services option $services = $input->getOption('services'); if (!$services) { // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion $services = $this->servicesQuestion($output); $input->setOption('services', $services); } // --path_service option $path_service = $input->getOption('path_service'); if (!$path_service) { $path_service = $io->ask($this->trans('commands.generate.service.questions.path'), '/modules/custom/' . $module . '/src/'); $input->setOption('path_service', $path_service); } }
/** * @param DrupalStyle $io * * @return mixed */ public function servicesQuestion(DrupalStyle $io) { if ($io->confirm($this->trans('commands.common.questions.services.confirm'), false)) { $service_collection = []; $io->writeln($this->trans('commands.common.questions.services.message')); $services = $this->getServices(); while (true) { $service = $io->choiceNoList($this->trans('commands.common.questions.services.name'), $services, null, true); $service = trim($service); if (empty($service)) { break; } array_push($service_collection, $service); $service_key = array_search($service, $services, true); if ($service_key >= 0) { unset($services[$service_key]); } } return $service_collection; } }
/** * @param DrupalStyle $io * * @return mixed */ public function breakpointQuestion(DrupalStyle $io) { $stringUtils = $this->getStringHelper(); $validators = $this->getValidator(); $breakpoints = []; while (true) { $breakPointName = $io->ask($this->trans('commands.generate.theme.questions.breakpoint-name'), 'narrow', function ($breakPointName) use($validators) { return $validators->validateMachineName($breakPointName); }); $breakPointLabel = $stringUtils->createMachineName($breakPointName); $breakPointLabel = $io->ask($this->trans('commands.generate.theme.questions.breakpoint-label'), $breakPointLabel, function ($breakPointLabel) use($validators) { return $validators->validateMachineName($breakPointLabel); }); $breakPointMediaQuery = $io->ask($this->trans('commands.generate.theme.questions.breakpoint-media-query'), 'all and (min-width: 560px) and (max-width: 850px)'); $breakPointWeight = $io->ask($this->trans('commands.generate.theme.questions.breakpoint-weight'), '1'); $breakPointMultipliers = $io->ask($this->trans('commands.generate.theme.questions.breakpoint-multipliers'), '1x'); array_push($breakpoints, ['breakpoint_name' => $breakPointName, 'breakpoint_label' => $breakPointLabel, 'breakpoint_media_query' => $breakPointMediaQuery, 'breakpoint_weight' => $breakPointWeight, 'breakpoint_multipliers' => $breakPointMultipliers]); if (!$io->confirm($this->trans('commands.generate.theme.questions.breakpoint-add'), true)) { break; } } return $breakpoints; }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); // --module option $module = $input->getOption('module'); if (!$module) { // @see Drupal\Console\Command\ModuleTrait::moduleQuestion $module = $this->moduleQuestion($output); $input->setOption('module', $module); } // --class option $className = $input->getOption('class'); if (!$className) { $className = $io->ask($this->trans('commands.generate.form.questions.class'), 'DefaultForm'); $input->setOption('class', $className); } // --form-id option $formId = $input->getOption('form-id'); if (!$formId) { $formId = $io->ask($this->trans('commands.generate.form.questions.form-id'), $this->getStringHelper()->camelCaseToMachineName($className)); $input->setOption('form-id', $formId); } // --services option // @see use Drupal\Console\Command\ServicesTrait::servicesQuestion $services = $this->servicesQuestion($output); $input->setOption('services', $services); // --inputs option $inputs = $input->getOption('inputs'); if (!$inputs) { // @see \Drupal\Console\Command\FormTrait::formQuestion $inputs = $this->formQuestion($output); $input->setOption('inputs', $inputs); } // --routing option for ConfigFormBase if ($this->formType == 'ConfigFormBase') { $routing = $input->getOption('routing'); if (!$routing) { $routing = $io->confirm($this->trans('commands.generate.form.questions.routing'), true); $input->setOption('routing', $routing); } } // --link option for links.menu if ($this->formType == 'ConfigFormBase') { $menu_options = $this->menuQuestion($output, $className); $menu_link_gen = $input->getOption('menu_link_gen'); $menu_link_title = $input->getOption('menu_link_title'); $menu_parent = $input->getOption('menu_parent'); $menu_link_desc = $input->getOption('menu_link_desc'); if (!$menu_link_gen || !$menu_link_title || !$menu_parent || !$menu_link_desc) { $input->setOption('menu_link_gen', $menu_options['menu_link_gen']); $input->setOption('menu_link_title', $menu_options['menu_link_title']); $input->setOption('menu_parent', $menu_options['menu_parent']); $input->setOption('menu_link_desc', $menu_options['menu_link_desc']); } } }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $config_types = $this->getConfigTypes(); $config_name = $input->getArgument('config-name'); if (!$config_name) { $config_type = $io->choiceNoList($this->trans('commands.config.export.single.questions.config-type'), array_keys($config_types), $this->trans('commands.config.export.single.options.simple-configuration')); $config_names = $this->getConfigNames($config_type); $config_name = $io->choiceNoList($this->trans('commands.config.export.single.questions.config-name'), array_keys($config_names)); if ($config_type !== 'system.simple') { $definition = $this->entityTypeManager->getDefinition($config_type); $config_name = $definition->getConfigPrefix() . '.' . $config_name; } $input->setArgument('config-name', $config_name); } $module = $input->getOption('module'); if ($module) { $optionalConfig = $input->getOption('optional-config'); if (!$optionalConfig) { $optionalConfig = $io->confirm($this->trans('commands.config.export.single.questions.optional-config'), true); $input->setOption('optional-config', $optionalConfig); } } }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $utils = $this->getStringHelper(); // --module option $module = $input->getOption('module'); if (!$module) { // @see Drupal\Console\Command\ModuleTrait::moduleQuestion $module = $this->moduleQuestion($output); $input->setOption('module', $module); } // --class option $className = $input->getOption('class'); if (!$className) { $className = $io->ask($this->trans('commands.generate.controller.questions.class'), 'DefaultController', function ($className) { return $this->validateClassName($className); }); $input->setOption('class', $className); } $routes = []; while (true) { // --title option $title = $input->getOption('title'); if (!$title) { $title = $io->ask($this->trans('commands.generate.controller.questions.title'), $utils->camelCaseToHuman($className), function ($title) use($routes) { if (!empty($routes) && empty($title)) { return false; } if (in_array($title, array_column($routes, 'title'))) { throw new \InvalidArgumentException(sprintf($this->trans('commands.generate.controller.messages.title-already-added'), $title)); } return $title; }); } if ($title === false) { break; } // --method option $method = $input->getOption('method'); if (!$method) { $method = $io->ask($this->trans('commands.generate.controller.questions.method'), 'index', function ($method) use($routes) { if (in_array($method, array_column($routes, 'method'))) { throw new \InvalidArgumentException(sprintf($this->trans('commands.generate.controller.messages.method-already-added'), $method)); } return $method; }); } // --route option option $route = $input->getOption('route'); if (!$route) { $route = $io->ask($this->trans('commands.generate.controller.questions.route'), sprintf('%s/%s/hello/{name}', $module, $method), function ($route) use($routes) { if (in_array($route, array_column($routes, 'route'))) { throw new \InvalidArgumentException(sprintf($this->trans('commands.generate.controller.messages.route-already-added'), $route)); } return $route; }); } $routes[] = ['title' => $title, 'method' => $method, 'route' => $route]; if (!$output->confirm($this->trans('commands.generate.controller.questions.controller-add'), true)) { break; } } $input->setOption('title', array_column($routes, 'title')); $input->setOption('method', array_column($routes, 'method')); $input->setOption('route', array_column($routes, 'route')); // --test option $test = $input->getOption('test'); if (!$test) { $test = $io->confirm($this->trans('commands.generate.controller.questions.test'), true); $input->setOption('test', $test); } // --services option // @see use Drupal\Console\Command\ServicesTrait::servicesQuestion $services = $this->servicesQuestion($output); $input->setOption('services', $services); }
protected function execute(InputInterface $input, OutputInterface $output) { $output = new DrupalStyle($input, $output); $extension_config = $this->getConfigFactory()->getEditable('core.extension'); $this->moduleInstaller = $this->getModuleInstaller(); // Get info about modules available $module_data = system_rebuild_module_data(); $modules = $input->getArgument('module'); $overwrite_config = $input->getOption('overwrite-config'); $module_list = array_combine($modules, $modules); // Determine if some module request is missing if ($missing_modules = array_diff_key($module_list, $module_data)) { $output->error(sprintf($this->trans('commands.module.install.messages.missing'), implode(', ', $modules), implode(', ', $missing_modules))); return true; } // Only process currently uninstalled modules. $installed_modules = $extension_config->get('module') ?: array(); if (!($module_list = array_diff_key($module_list, $installed_modules))) { $output->warning($this->trans('commands.module.install.messages.nothing')); return; } // Calculate dependencies and missing dependencies $dependencies = array(); $missing_dependencies = array(); while (list($module) = each($module_list)) { foreach (array_keys($module_data[$module]->requires) as $dependency) { if (!isset($module_data[$dependency])) { $missing_dependencies[] = $dependency; } // Skip already installed modules. if (!isset($module_list[$dependency]) && !isset($installed_modules[$dependency])) { $module_list[$dependency] = $dependency; $dependencies[] = $dependency; } } } // Error if there are missing dependencies if (!empty($missing_dependencies)) { $output->error(sprintf($this->trans('commands.module.install.messages.missing-dependencies'), implode(', ', $modules), implode(', ', $missing_dependencies))); return true; } // Confirm if user want to install dependencies uninstalled if ($dependencies) { if (!$output->confirm(sprintf($this->trans('commands.module.install.messages.dependencies'), implode(', ', $dependencies)), false)) { return; } } // Installing modules try { // Install the modules. $this->moduleInstaller->install($module_list); system_rebuild_module_data(); $output->success(sprintf($this->trans('commands.module.install.messages.success'), implode(', ', array_merge($modules, $dependencies)))); } catch (PreExistingConfigException $e) { $this->overwriteConfig($e, $module_list, $modules, $dependencies, $overwrite_config, $output); return; } catch (\Exception $e) { $output->error($e->getMessage()); return; } // Run cache rebuild to see changes in Web UI $this->getChain()->addCommand('cache:rebuild', ['cache' => 'all']); }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $validator = $this->validator; try { $module = $input->getOption('module') ? $this->validateModuleName($input->getOption('module')) : null; } catch (\Exception $error) { $io->error($error->getMessage()); return; } if (!$module) { $module = $io->ask($this->trans('commands.generate.module.questions.module'), null, function ($module) use($validator) { return $validator->validateModuleName($module); }); $input->setOption('module', $module); } try { $machineName = $input->getOption('machine-name') ? $this->validateModule($input->getOption('machine-name')) : null; } catch (\Exception $error) { $io->error($error->getMessage()); } if (!$machineName) { $machineName = $io->ask($this->trans('commands.generate.module.questions.machine-name'), $this->stringConverter->createMachineName($module), function ($machine_name) use($validator) { return $validator->validateMachineName($machine_name); }); $input->setOption('machine-name', $machineName); } $modulePath = $input->getOption('module-path'); if (!$modulePath) { $drupalRoot = $this->appRoot; $modulePath = $io->ask($this->trans('commands.generate.module.questions.module-path'), '/modules/custom', function ($modulePath) use($drupalRoot, $machineName) { $modulePath = ($modulePath[0] != '/' ? '/' : '') . $modulePath; $fullPath = $drupalRoot . $modulePath . '/' . $machineName; if (file_exists($fullPath)) { throw new \InvalidArgumentException(sprintf($this->trans('commands.generate.module.errors.directory-exists'), $fullPath)); } return $modulePath; }); } $input->setOption('module-path', $modulePath); $description = $input->getOption('description'); if (!$description) { $description = $io->ask($this->trans('commands.generate.module.questions.description'), 'My Awesome Module'); } $input->setOption('description', $description); $package = $input->getOption('package'); if (!$package) { $package = $io->ask($this->trans('commands.generate.module.questions.package'), 'Custom'); } $input->setOption('package', $package); $core = $input->getOption('core'); if (!$core) { $core = $io->ask($this->trans('commands.generate.module.questions.core'), '8.x', function ($core) { // Only allow 8.x and higher as core version. if (!preg_match('/^([0-9]+)\\.x$/', $core, $matches) || $matches[1] < 8) { throw new \InvalidArgumentException(sprintf($this->trans('commands.generate.module.errors.invalid-core'), $core)); } return $core; }); $input->setOption('core', $core); } $moduleFile = $input->getOption('module-file'); if (!$moduleFile) { $moduleFile = $io->confirm($this->trans('commands.generate.module.questions.module-file'), true); $input->setOption('module-file', $moduleFile); } $featuresBundle = $input->getOption('features-bundle'); if (!$featuresBundle) { $featuresSupport = $io->confirm($this->trans('commands.generate.module.questions.features-support'), false); if ($featuresSupport) { $featuresBundle = $io->ask($this->trans('commands.generate.module.questions.features-bundle'), 'default'); } $input->setOption('features-bundle', $featuresBundle); } $composer = $input->getOption('composer'); if (!$composer) { $composer = $io->confirm($this->trans('commands.generate.module.questions.composer'), true); $input->setOption('composer', $composer); } $dependencies = $input->getOption('dependencies'); if (!$dependencies) { $addDependencies = $io->confirm($this->trans('commands.generate.module.questions.dependencies'), false); if ($addDependencies) { $dependencies = $io->ask($this->trans('commands.generate.module.options.dependencies')); } $input->setOption('dependencies', $dependencies); } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $type = $input->getArgument('type'); if (!$type) { $io->error($this->trans('commands.config.delete.errors.type')); return 1; } $name = $input->getArgument('name'); if (!$name) { $io->error($this->trans('commands.config.delete.errors.name')); return 1; } $configStorage = 'active' === $type ? $this->configStorage : $this->configStorageSync; if (!$configStorage) { $io->error($this->trans('commands.config.delete.errors.config-storage')); return 1; } if ('all' === $name) { $io->commentBlock($this->trans('commands.config.delete.warnings.undo')); if ($io->confirm($this->trans('commands.config.delete.questions.sure'))) { if ($configStorage instanceof FileStorage) { $configStorage->deleteAll(); } else { foreach ($this->yieldAllConfig() as $name) { $this->removeConfig($name); } } $io->success($this->trans('commands.config.delete.messages.all')); return 0; } } if ($configStorage->exists($name)) { if ($configStorage instanceof FileStorage) { $configStorage->delete($name); } else { $this->removeConfig($name); } $io->success(sprintf($this->trans('commands.config.delete.messages.deleted'), $name)); return 0; } $message = sprintf($this->trans('commands.config.delete.errors.not-exists'), $name); $io->error($message); return 1; }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $output = new DrupalStyle($input, $output); $stringUtils = $this->getStringHelper(); $validators = $this->getValidator(); $drupal = $this->getDrupalHelper(); try { $module = $input->getOption('module') ? $this->validateModuleName($input->getOption('module')) : null; } catch (\Exception $error) { $output->error($error->getMessage()); return; } if (!$module) { $module = $output->ask($this->trans('commands.generate.module.questions.module'), null, function ($module) use($validators) { return $validators->validateModuleName($module); }); $input->setOption('module', $module); } try { $machineName = $input->getOption('machine-name') ? $this->validateModule($input->getOption('machine-name')) : null; } catch (\Exception $error) { $output->error($error->getMessage()); } if (!$machineName) { $machineName = $output->ask($this->trans('commands.generate.module.questions.machine-name'), $stringUtils->createMachineName($module), function ($machine_name) use($validators) { return $validators->validateMachineName($machine_name); }); $input->setOption('machine-name', $machineName); } $modulePath = $input->getOption('module-path'); if (!$modulePath) { $drupalRoot = $drupal->getRoot(); $modulePath = $output->ask($this->trans('commands.generate.module.questions.module-path'), '/modules/custom', function ($modulePath) use($drupalRoot, $machineName) { $modulePath = ($modulePath[0] != '/' ? '/' : '') . $modulePath; $fullPath = $drupalRoot . $modulePath . '/' . $machineName; if (file_exists($fullPath)) { throw new \InvalidArgumentException(sprintf($this->trans('commands.generate.module.errors.directory-exists'), $fullPath)); } return $modulePath; }); } $input->setOption('module-path', $modulePath); $description = $input->getOption('description'); if (!$description) { $description = $output->ask($this->trans('commands.generate.module.questions.description'), 'My Awesome Module'); } $input->setOption('description', $description); $package = $input->getOption('package'); if (!$package) { $package = $output->ask($this->trans('commands.generate.module.questions.package'), 'Other'); } $input->setOption('package', $package); $core = $input->getOption('core'); if (!$core) { $core = $output->ask($this->trans('commands.generate.module.questions.core'), '8.x', '8.x'); } $input->setOption('core', $core); $feature = $input->getOption('feature'); if (!$feature) { $feature = $output->confirm($this->trans('commands.generate.module.questions.feature'), false); } $input->setOption('feature', $feature); $composer = $input->getOption('composer'); if (!$composer) { $composer = $output->confirm($this->trans('commands.generate.module.questions.composer'), true); } $input->setOption('composer', $composer); $dependencies = $input->getOption('dependencies'); if (!$dependencies) { $addDependencies = $output->confirm($this->trans('commands.generate.module.questions.dependencies'), false); if ($addDependencies) { $dependencies = $output->ask($this->trans('commands.generate.module.options.dependencies')); } } $input->setOption('dependencies', $dependencies); }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); //$stringUtils = $this->getStringHelper(); $validators = $this->validator; try { // A profile is technically also a module, so we can use the same // validator to check the name. $profile = $input->getOption('profile') ? $this->validateModuleName($input->getOption('profile')) : null; } catch (\Exception $error) { $io->error($error->getMessage()); return; } if (!$profile) { $profile = $io->ask($this->trans('commands.generate.profile.questions.profile'), '', function ($profile) use($validators) { return $validators->validateModuleName($profile); }); $input->setOption('profile', $profile); } try { $machine_name = $input->getOption('machine-name') ? $this->validateModule($input->getOption('machine-name')) : null; } catch (\Exception $error) { $io->error($error->getMessage()); return; } if (!$machine_name) { $machine_name = $io->ask($this->trans('commands.generate.profile.questions.machine-name'), $this->stringConverter->createMachineName($profile), function ($machine_name) use($validators) { return $validators->validateMachineName($machine_name); }); $input->setOption('machine-name', $machine_name); } $description = $input->getOption('description'); if (!$description) { $description = $io->ask($this->trans('commands.generate.profile.questions.description'), 'My Useful Profile'); $input->setOption('description', $description); } $core = $input->getOption('core'); if (!$core) { $core = $io->ask($this->trans('commands.generate.profile.questions.core'), '8.x'); $input->setOption('core', $core); } $dependencies = $input->getOption('dependencies'); if (!$dependencies) { if ($io->confirm($this->trans('commands.generate.profile.questions.dependencies'), true)) { $dependencies = $io->ask($this->trans('commands.generate.profile.options.dependencies'), ''); } $input->setOption('dependencies', $dependencies); } $distribution = $input->getOption('distribution'); if (!$distribution) { if ($io->confirm($this->trans('commands.generate.profile.questions.distribution'), false)) { $distribution = $io->ask($this->trans('commands.generate.profile.options.distribution'), 'My Kick-ass Distribution'); $input->setOption('distribution', $distribution); } } }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); // --module option $module = $input->getOption('module'); if (!$module) { // @see Drupal\Console\Command\ModuleTrait::moduleQuestion $module = $this->moduleQuestion($output); $input->setOption('module', $module); } // --class option $class = $input->getOption('class'); if (!$class) { $class = $io->ask($this->trans('commands.generate.controller.questions.class'), 'DefaultController', function ($class) { return $this->validateClassName($class); }); $input->setOption('class', $class); } $routes = $input->getOption('routes'); if (!$routes) { while (true) { $title = $io->askEmpty($this->trans('commands.generate.controller.questions.title'), function ($title) use($routes) { if ($routes && empty(trim($title))) { return false; } if (!$routes && empty(trim($title))) { throw new \InvalidArgumentException($this->trans('commands.generate.controller.messages.title-empty')); } if (in_array($title, array_column($routes, 'title'))) { throw new \InvalidArgumentException(sprintf($this->trans('commands.generate.controller.messages.title-already-added'), $title)); } return $title; }); if ($title === '') { break; } $method = $io->ask($this->trans('commands.generate.controller.questions.method'), 'hello', function ($method) use($routes) { if (in_array($method, array_column($routes, 'method'))) { throw new \InvalidArgumentException(sprintf($this->trans('commands.generate.controller.messages.method-already-added'), $method)); } return $method; }); $path = $io->ask($this->trans('commands.generate.controller.questions.path'), sprintf('/%s/hello/{name}', $module), function ($path) use($routes) { if (in_array($path, array_column($routes, 'path'))) { throw new \InvalidArgumentException(sprintf($this->trans('commands.generate.controller.messages.path-already-added'), $path)); } return $path; }); $routes[] = ['title' => $title, 'method' => $method, 'path' => $path]; } $input->setOption('routes', $routes); } // --test option $test = $input->getOption('test'); if (!$test) { $test = $io->confirm($this->trans('commands.generate.controller.questions.test'), true); $input->setOption('test', $test); } // --services option // @see use Drupal\Console\Command\ServicesTrait::servicesQuestion $services = $this->servicesQuestion($output); $input->setOption('services', $services); }
protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $entity_manager = $this->getEntityManager(); $entity_types = $entity_manager->getEntityTypeLabels(true); // --module option $module = $input->getOption('module'); if (!$module) { // @see Drupal\Console\Command\ModuleTrait::moduleQuestion $module = $this->moduleQuestion($output); } $input->setOption('module', $module); // --class option $class = $input->getOption('class'); if (!$class) { $class = $io->ask($this->trans('commands.generate.plugin.condition.questions.class'), 'ExampleCondition'); $input->setOption('class', $class); } // --plugin label option $label = $input->getOption('label'); if (!$label) { $label = $io->ask($this->trans('commands.generate.plugin.condition.questions.label'), $this->getStringHelper()->camelCaseToHuman($class)); $input->setOption('label', $label); } // --plugin-id option $pluginId = $input->getOption('plugin-id'); if (!$pluginId) { $pluginId = $io->ask($this->trans('commands.generate.plugin.condition.questions.plugin-id'), $this->getStringHelper()->camelCaseToUnderscore($class)); $input->setOption('plugin-id', $pluginId); } $context_definition_id = $input->getOption('context-definition-id'); if (!$context_definition_id) { $context_type = array('language' => 'Language', "entity" => "Entity"); $context_type_sel = $io->choice($this->trans('commands.generate.plugin.condition.questions.context-type'), array_values($context_type)); $context_type_sel = array_search($context_type_sel, $context_type); if ($context_type_sel == 'language') { $context_definition_id = $context_type_sel; $context_definition_id_value = ucfirst($context_type_sel); } else { $content_entity_types_sel = $io->choice($this->trans('commands.generate.plugin.condition.questions.context-entity-type'), array_keys($entity_types)); $contextDefinitionIdList = $entity_types[$content_entity_types_sel]; $context_definition_id_sel = $io->choice($this->trans('commands.generate.plugin.condition.questions.context-definition-id'), array_values($contextDefinitionIdList)); $context_definition_id_value = array_search($context_definition_id_sel, $contextDefinitionIdList); $context_definition_id = 'entity:' . $context_definition_id_value; } $input->setOption('context-definition-id', $context_definition_id); } $context_definition_label = $input->getOption('context-definition-label'); if (!$context_definition_label) { $context_definition_label = $io->ask($this->trans('commands.generate.plugin.condition.questions.context-definition-label'), $context_definition_id_value ?: null); $input->setOption('context-definition-label', $context_definition_label); } $context_definition_required = $input->getOption('context-definition-required'); if (empty($context_definition_required)) { $context_definition_required = $io->confirm($this->trans('commands.generate.plugin.condition.questions.context-definition-required'), true); $input->setOption('context-definition-required', $context_definition_required); } }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); try { $theme = $input->getOption('theme') ? $this->validator->validateModuleName($input->getOption('theme')) : null; } catch (\Exception $error) { $io->error($error->getMessage()); return; } if (!$theme) { $validators = $this->validator; $theme = $io->ask($this->trans('commands.generate.theme.questions.theme'), '', function ($theme) use($validators) { return $validators->validateModuleName($theme); }); $input->setOption('theme', $theme); } try { $machine_name = $input->getOption('machine-name') ? $this->validator->validateModule($input->getOption('machine-name')) : null; } catch (\Exception $error) { $io->error($error->getMessage()); return; } if (!$machine_name) { $machine_name = $io->ask($this->trans('commands.generate.module.questions.machine-name'), $this->stringConverter->createMachineName($theme), function ($machine_name) use($validators) { return $validators->validateMachineName($machine_name); }); $input->setOption('machine-name', $machine_name); } $theme_path = $input->getOption('theme-path'); if (!$theme_path) { $drupalRoot = $this->appRoot; $theme_path = $io->ask($this->trans('commands.generate.theme.questions.theme-path'), '/themes/custom', function ($theme_path) use($drupalRoot, $machine_name) { $theme_path = ($theme_path[0] != '/' ? '/' : '') . $theme_path; $full_path = $drupalRoot . $theme_path . '/' . $machine_name; if (file_exists($full_path)) { throw new \InvalidArgumentException(sprintf($this->trans('commands.generate.theme.errors.directory-exists'), $full_path)); } else { return $theme_path; } }); $input->setOption('theme-path', $theme_path); } $description = $input->getOption('description'); if (!$description) { $description = $io->ask($this->trans('commands.generate.theme.questions.description'), 'My Awesome theme'); $input->setOption('description', $description); } $package = $input->getOption('package'); if (!$package) { $package = $io->ask($this->trans('commands.generate.theme.questions.package'), 'Other'); $input->setOption('package', $package); } $core = $input->getOption('core'); if (!$core) { $core = $io->ask($this->trans('commands.generate.theme.questions.core'), '8.x'); $input->setOption('core', $core); } $base_theme = $input->getOption('base-theme'); if (!$base_theme) { $themes = $this->themeHandler->rebuildThemeData(); $themes['false'] = ''; uasort($themes, 'system_sort_modules_by_info_name'); $base_theme = $io->choiceNoList($this->trans('commands.generate.theme.options.base-theme'), array_keys($themes)); $input->setOption('base-theme', $base_theme); } $global_library = $input->getOption('global-library'); if (!$global_library) { $global_library = $io->ask($this->trans('commands.generate.theme.questions.global-library'), 'global-styling'); $input->setOption('global-library', $global_library); } // --regions option. $regions = $input->getOption('regions'); if (!$regions) { if ($io->confirm($this->trans('commands.generate.theme.questions.regions'), true)) { // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion $regions = $this->regionQuestion($io); $input->setOption('regions', $regions); } } // --breakpoints option. $breakpoints = $input->getOption('breakpoints'); if (!$breakpoints) { if ($io->confirm($this->trans('commands.generate.theme.questions.breakpoints'), true)) { // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion $breakpoints = $this->breakpointQuestion($io); $input->setOption('breakpoints', $breakpoints); } } }
/** * @param DrupalStyle $output * * @return mixed */ public function formQuestion(DrupalStyle $output) { if ($output->confirm($this->trans('commands.common.questions.inputs.confirm'), true)) { $input_types = ['fieldset']; $elementInfoManager = $this->hasGetService('plugin.manager.element_info'); if (!$elementInfoManager) { return false; } foreach ($elementInfoManager->getDefinitions() as $definition) { $type = $definition['id']; $elementInfo = $elementInfoManager->getInfo($type); if (isset($elementInfo['#input']) && $elementInfo['#input']) { if (!in_array($type, $input_types)) { $input_types[] = $type; } } } sort($input_types); $inputs = []; $fieldSets = []; while (true) { $input_type = $output->choiceNoList($this->trans('commands.common.questions.inputs.type'), $input_types, null, true); if (empty($input_type)) { break; } // Label for input $inputLabelMessage = $input_type == 'fieldset' ? $this->trans('commands.common.questions.inputs.title') : $this->trans('commands.common.questions.inputs.label'); $input_label = $output->ask($inputLabelMessage, null); // Machine name $input_machine_name = $this->getStringHelper()->createMachineName($input_label); $input_name = $output->ask($this->trans('commands.common.questions.inputs.machine_name'), $input_machine_name); if ($input_type == 'fieldset') { $fieldSets[$input_machine_name] = $input_label; } $inputFieldSet = ''; if ($input_type != 'fieldset' && !empty($fieldSets)) { $inputFieldSet = $output->choiceNoList($this->trans('commands.common.questions.inputs.fieldset'), $fieldSets, null, true); $inputFieldSet = array_search($inputFieldSet, $fieldSets); } $maxlength = null; $size = null; if (in_array($input_type, array('textfield', 'password', 'password_confirm'))) { $maxlength = $output->ask('Maximum amount of characters', '64'); $size = $output->ask('Width of the textfield (in characters)', '64'); } if ($input_type == 'select') { $size = $output->askEmpty('Size of multiselect box (in lines)', '5'); } $input_options = ''; if (in_array($input_type, array('checkboxes', 'radios', 'select'))) { $input_options = $output->ask('Input options separated by comma'); } // Prepare options as an array if (strlen(trim($input_options))) { // remove spaces in options and empty options $input_options = array_filter(array_map('trim', explode(',', $input_options))); // Create array format for options foreach ($input_options as $key => $value) { $input_options_output[$key] = "'{$value}' => \$this->t('" . $value . "')"; } $input_options = 'array(' . implode(', ', $input_options_output) . ')'; } // Description for input $input_description = $output->askEmpty($this->trans('commands.common.questions.inputs.description')); if ($input_type != 'fieldset') { // Default value for input $default_value = $output->askEmpty($this->trans('commands.common.questions.inputs.default-value')); } // Weight for input $weight = $output->ask($this->trans('commands.common.questions.inputs.weight'), '0'); array_push($inputs, ['name' => $input_name, 'type' => $input_type, 'label' => $input_label, 'options' => $input_options, 'description' => $input_description, 'maxlength' => $maxlength, 'size' => $size, 'default_value' => $default_value, 'weight' => $weight, 'fieldset' => $inputFieldSet]); } return $inputs; } return null; }