Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/Elgg/Cli/Command.php (1 issue)

1
<?php
2
3
namespace Elgg\Cli;
4
5
use Elgg\Logger;
6
use Error;
7
use Exception;
8
use RuntimeException;
9
use Symfony\Component\Console\Command\Command as SymfonyCommand;
10
use Symfony\Component\Console\Helper\FormatterHelper;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Abstract command with some utility methods
16
 */
17
abstract class Command extends SymfonyCommand {
18
19
	use ConsoleInteractions;
20
21
	/**
22
	 * {@inheritdoc}
23
	 */
24
	final public function execute(InputInterface $input, OutputInterface $output) {
25
26
		set_error_handler([
27
			$this,
28
			'handleErrors'
29
		]);
30
		set_exception_handler([
31
			$this,
32
			'handleException'
33
		]);
34
35
		Logger::$verbosity = $output->getVerbosity();
36
37
		$this->input = $input;
38
		$this->output = $output;
39
40
		elgg_set_config('debug', 'INFO');
41
42
		_elgg_services()->responseFactory->setTransport(new ResponseTransport($this));
43
44
		$this->login();
45
46
		$result = $this->command();
47
		if (is_callable($result)) {
48
			$result = call_user_func($result, $this);
49
		}
50
51
		$this->dumpRegisters();
52
53
		$this->logout();
54
55
		return (int) $result;
56
	}
57
58
	/**
59
	 * Command to be executed
60
	 *
61
	 * This method method should return an integer code of the error (or 0 for success).
62
	 * Optionally, the method can return a callable that will receive the instance of this command as an argument
63
	 *
64
	 * @return mixed
65
	 * @see Command::execute()
66
	 */
67
	abstract protected function command();
68
69
	/**
70
	 * Login a user defined by --as option
71
	 *
72
	 * @return void
73
	 * @throws RuntimeException
74
	 */
75
	final protected function login() {
76
		if (!$this->getDefinition()->hasOption('as')) {
77
			return;
78
		}
79
		$username = $this->option('as');
80
		if (!$username) {
81
			return;
82
		}
83
		$user = get_user_by_username($username);
84
		if (!$user) {
85
			throw new RuntimeException("User with username $username not found");
86
		}
87
		if (!login($user)) {
88
			throw new RuntimeException("Unable to login as $username");
89
		}
90
		elgg_log("Logged in as $username [guid: $user->guid]");
91
	}
92
93
	/**
94
	 * Logout a user
95
	 * @return void
96
	 */
97
	final protected function logout() {
98
		if (elgg_is_logged_in()) {
99
			logout();
100
		}
101
	}
102
103
	/**
104
	 * Handler errors
105
	 *
106
	 * @param int    $errno    The level of the error raised
107
	 * @param string $errmsg   The error message
108
	 * @param string $filename The filename the error was raised in
109
	 * @param int    $linenum  The line number the error was raised at
110
	 * @param array  $vars     An array that points to the active symbol table where error occurred
111
	 *
112
	 * @return true
113
	 * @throws \Exception
114
	 * @access private
115
	 */
116
	public function handleErrors($errno, $errmsg, $filename, $linenum, $vars) {
0 ignored issues
show
The parameter $vars is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

116
	public function handleErrors($errno, $errmsg, $filename, $linenum, /** @scrutinizer ignore-unused */ $vars) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
		$error = date("Y-m-d H:i:s (T)") . ": \"$errmsg\" in file $filename (line $linenum)";
118
119
		switch ($errno) {
120
			case E_USER_ERROR:
121
				throw new Exception($error);
122
123
			default:
124
				$formatter = new FormatterHelper();
125
				$message = $formatter->formatBlock($error, 'error');
126
				$this->output->writeln($message);
127
				break;
128
		}
129
130
		return true;
131
	}
132
133
	/**
134
	 * Handle exceptions
135
	 *
136
	 * @param Exception|Error $exception
137
	 *
138
	 * @return void
139
	 * @throws Exception
140
	 */
141
	public function handleException($exception) {
142
		$this->setCode(function () use ($exception) {
143
			return $exception->getCode() ? : 1;
144
		});
145
146
		$timestamp = time();
147
		$error = "Exception at time $timestamp: $exception";
148
149
		$formatter = new FormatterHelper();
150
		$message = $formatter->formatBlock($error, 'error');
151
		$this->output->writeln($message);
152
	}
153
154
}
155