X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fdrupal%2Fconsole%2Fsrc%2FCommand%2FGenerate%2FControllerCommand.php;fp=vendor%2Fdrupal%2Fconsole%2Fsrc%2FCommand%2FGenerate%2FControllerCommand.php;h=6ab08915abbec4cc95bc9b38d77139c8713f52c3;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/drupal/console/src/Command/Generate/ControllerCommand.php b/vendor/drupal/console/src/Command/Generate/ControllerCommand.php new file mode 100644 index 000000000..6ab08915a --- /dev/null +++ b/vendor/drupal/console/src/Command/Generate/ControllerCommand.php @@ -0,0 +1,315 @@ +extensionManager = $extensionManager; + $this->generator = $generator; + $this->stringConverter = $stringConverter; + $this->validator = $validator; + $this->routeProvider = $routeProvider; + $this->chainQueue = $chainQueue; + parent::__construct(); + } + + protected function configure() + { + $this + ->setName('generate:controller') + ->setDescription($this->trans('commands.generate.controller.description')) + ->setHelp($this->trans('commands.generate.controller.help')) + ->addOption( + 'module', + '', + InputOption::VALUE_REQUIRED, + $this->trans('commands.common.options.module') + ) + ->addOption( + 'class', + '', + InputOption::VALUE_OPTIONAL, + $this->trans('commands.generate.controller.options.class') + ) + ->addOption( + 'routes', + '', + InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + $this->trans('commands.generate.controller.options.routes') + ) + ->addOption( + 'services', + '', + InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + $this->trans('commands.common.options.services') + ) + ->addOption( + 'test', + '', + InputOption::VALUE_NONE, + $this->trans('commands.generate.controller.options.test') + ); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new DrupalStyle($input, $output); + $yes = $input->hasOption('yes')?$input->getOption('yes'):false; + + // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration + if (!$this->confirmGeneration($io, $yes)) { + return; + } + + $learning = $input->hasOption('learning')?$input->getOption('learning'):false; + $module = $input->getOption('module'); + $class = $input->getOption('class'); + $routes = $input->getOption('routes'); + $test = $input->getOption('test'); + $services = $input->getOption('services'); + + $routes = $this->inlineValueAsArray($routes); + $input->setOption('routes', $routes); + + // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices + $build_services = $this->buildServices($services); + + //$this->generator->setLearning($learning); + $this->generator->generate( + $module, + $class, + $routes, + $test, + $build_services + ); + + // Run cache rebuild to see changes in Web UI + $this->chainQueue->addCommand('router:rebuild', []); + } + + /** + * {@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($io); + $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->validator->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 (count($this->routeProvider->getRoutesByPattern($path)) > 0 + || in_array($path, array_column($routes, 'path')) + ) { + throw new \InvalidArgumentException( + sprintf( + $this->trans( + 'commands.generate.controller.messages.path-already-added' + ), + $path + ) + ); + } + + return $path; + } + ); + $classMachineName = $this->stringConverter->camelCaseToMachineName($class); + $routeName = $module . '.' . $classMachineName . '_' . $method; + if ($this->routeProvider->getRoutesByNames([$routeName]) + || in_array($routeName, $routes) + ) { + $routeName .= '_' . rand(0, 100); + } + + $routes[] = [ + 'title' => $title, + 'name' => $routeName, + '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\Shared\ServicesTrait::servicesQuestion + $services = $this->servicesQuestion($io); + $input->setOption('services', $services); + } + + /** + * @return \Drupal\Console\Generator\ControllerGenerator + */ + protected function createGenerator() + { + return new ControllerGenerator(); + } +}