Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / TestSite / Commands / TestSiteUserLoginCommand.php
1 <?php
2
3 namespace Drupal\TestSite\Commands;
4
5 use Drupal\Core\DrupalKernel;
6 use Drupal\Core\Site\Settings;
7 use Symfony\Component\Console\Command\Command;
8 use Symfony\Component\Console\Exception\InvalidArgumentException;
9 use Symfony\Component\Console\Input\InputArgument;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\HttpFoundation\Request;
14
15 /**
16  * Command to generate a login link for the test site.
17  *
18  * @internal
19  */
20 class TestSiteUserLoginCommand extends Command {
21
22   /**
23    * The class loader to use for installation and initialization of setup.
24    *
25    * @var \Symfony\Component\Classloader\Classloader
26    */
27   protected $classLoader;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function configure() {
33     $this->setName('user-login')
34       ->setDescription('Generate a one time login link for an user.')
35       ->addArgument('uid', InputArgument::REQUIRED, 'The ID of the user for whom the link will be generated')
36       ->addOption('site-path', NULL, InputOption::VALUE_REQUIRED, 'The path for the test site.');
37   }
38
39   /**
40    * {@inheritdoc}
41    *
42    * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
43    */
44   protected function execute(InputInterface $input, OutputInterface $output) {
45     $root = dirname(dirname(dirname(dirname(dirname(__DIR__)))));
46     chdir($root);
47
48     $this->classLoader = require 'autoload.php';
49     $kernel = new DrupalKernel('prod', $this->classLoader, FALSE);
50     $kernel::bootEnvironment();
51     $kernel->setSitePath($input->getOption('site-path'));
52     Settings::initialize($kernel->getAppRoot(), $kernel->getSitePath(), $this->classLoader);
53
54     $request = Request::createFromGlobals();
55     $kernel->prepareLegacyRequest($request);
56
57     $kernel->boot();
58
59     $container = $kernel->getContainer();
60     $uid = $input->getArgument('uid');
61     if (!is_numeric($uid)) {
62       throw new InvalidArgumentException(sprintf('The "uid" argument needs to be an integer, but it is "%s".', $uid));
63     }
64     $userEntity = $container->get('entity_type.manager')
65       ->getStorage('user')
66       ->load($uid);
67     $url = user_pass_reset_url($userEntity) . '/login';
68     $output->writeln($url);
69   }
70
71 }