Elgg  Version 3.0
Cli.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
5 use Elgg\Cli\Application as CliApplication;
7 use Exception;
11 
15 class Cli {
16 
17  use Loggable;
18 
22  protected $console;
23 
27  protected $hooks;
28 
32  protected $input;
33 
37  protected $output;
38 
47  public function __construct(
48  CliApplication $console,
49  PluginHooksService $hooks,
50  InputInterface $input,
51  OutputInterface $output
52  ) {
53  $this->console = $console;
54  $this->hooks = $hooks;
55  $this->input = $input;
56  $this->output = $output;
57  }
58 
63  protected function bootstrap() {
64  $commands = $this->hooks->trigger('commands', 'cli', null, []);
65 
66  foreach ($commands as $command) {
67  $this->add($command);
68  }
69  }
70 
79  public function add($command) {
80  if (!class_exists($command)) {
81  return;
82  }
83 
84  if (!is_subclass_of($command, BaseCommand::class)) {
85  return;
86  }
87 
88  $command = new $command();
89  /* @var $command BaseCommand */
90 
91  if ($this->logger) {
92  $command->setLogger($this->logger);
93  }
94 
95  $command->addOption('as', 'u', InputOption::VALUE_OPTIONAL,
96  'Execute the command on behalf of a user with the given username'
97  );
98 
99  $this->console->add($command);
100  }
101 
108  public function run() {
109  $this->bootstrap();
110  $this->console->run($this->input, $this->output);
111  }
112 
117  public function getInput() {
118  return $this->input;
119  }
120 
125  public function getOutput() {
126  return $this->output;
127  }
128 }
$hooks
Definition: Cli.php:27
getOutput()
Returns console output.
Definition: Cli.php:125
__construct(CliApplication $console, PluginHooksService $hooks, InputInterface $input, OutputInterface $output)
Constructor.
Definition: Cli.php:47
trait Loggable
Enables adding a logger.
Definition: Loggable.php:12
Wrapper for console application.
Definition: Application.php:11
catch(LoginException $e) if($request->isXhr()) $output
Definition: login.php:56
Configuration exception.
run()
Bootstrap and run console application.
Definition: Cli.php:108
CLI bootstrap.
Definition: Cli.php:15
getInput()
Returns console input.
Definition: Cli.php:117
$output
Definition: Cli.php:37
$input
Definition: Cli.php:32
$input
Form field view.
Definition: field.php:13
class
Definition: placeholder.php:21
add($command)
Add a new CLI command.
Definition: Cli.php:79
bootstrap()
Add CLI tools to the console application.
Definition: Cli.php:63
$console
Definition: Cli.php:22