Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / includes / shell / CommandFactory.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Shell;
22
23 use ExecutableFinder;
24 use Psr\Log\LoggerAwareTrait;
25 use Psr\Log\NullLogger;
26
27 /**
28 * Factory facilitating dependency injection for Command
29 *
30 * @since 1.30
31 */
32 class CommandFactory {
33 use LoggerAwareTrait;
34
35 /** @var array */
36 private $limits;
37
38 /** @var string|bool */
39 private $cgroup;
40
41 /** @var bool */
42 private $doLogStderr = false;
43
44 /**
45 * @var string|bool
46 */
47 private $restrictionMethod;
48
49 /**
50 * @var string|bool
51 */
52 private $firejail;
53
54 /**
55 * Constructor
56 *
57 * @param array $limits See {@see Command::limits()}
58 * @param string|bool $cgroup See {@see Command::cgroup()}
59 * @param string|bool $restrictionMethod
60 */
61 public function __construct( array $limits, $cgroup, $restrictionMethod ) {
62 $this->limits = $limits;
63 $this->cgroup = $cgroup;
64 if ( $restrictionMethod === 'autodetect' ) {
65 // On Linux systems check for firejail
66 if ( PHP_OS === 'Linux' && $this->findFirejail() !== false ) {
67 $this->restrictionMethod = 'firejail';
68 } else {
69 $this->restrictionMethod = false;
70 }
71 } else {
72 $this->restrictionMethod = $restrictionMethod;
73 }
74 $this->setLogger( new NullLogger() );
75 }
76
77 private function findFirejail() {
78 if ( $this->firejail === null ) {
79 $this->firejail = ExecutableFinder::findInDefaultPaths( 'firejail' );
80 }
81
82 return $this->firejail;
83 }
84
85 /**
86 * When enabled, text sent to stderr will be logged with a level of 'error'.
87 *
88 * @param bool $yesno
89 * @see Command::logStderr
90 */
91 public function logStderr( $yesno = true ) {
92 $this->doLogStderr = $yesno;
93 }
94
95 /**
96 * Instantiates a new Command
97 *
98 * @return Command
99 */
100 public function create() {
101 if ( $this->restrictionMethod === 'firejail' ) {
102 $command = new FirejailCommand( $this->findFirejail() );
103 } else {
104 $command = new Command();
105 }
106 $command->setLogger( $this->logger );
107
108 return $command
109 ->limits( $this->limits )
110 ->cgroup( $this->cgroup )
111 ->logStderr( $this->doLogStderr );
112 }
113 }