CommandFactory.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. namespace MediaWiki\Shell;
  21. use ExecutableFinder;
  22. use Psr\Log\LoggerAwareTrait;
  23. use Psr\Log\NullLogger;
  24. /**
  25. * Factory facilitating dependency injection for Command
  26. *
  27. * @since 1.30
  28. */
  29. class CommandFactory {
  30. use LoggerAwareTrait;
  31. /** @var array */
  32. private $limits;
  33. /** @var string|bool */
  34. private $cgroup;
  35. /** @var bool */
  36. private $doLogStderr = false;
  37. /**
  38. * @var string|bool
  39. */
  40. private $restrictionMethod;
  41. /**
  42. * @var string|bool
  43. */
  44. private $firejail;
  45. /**
  46. * Constructor
  47. *
  48. * @param array $limits See {@see Command::limits()}
  49. * @param string|bool $cgroup See {@see Command::cgroup()}
  50. * @param string|bool $restrictionMethod
  51. */
  52. public function __construct( array $limits, $cgroup, $restrictionMethod ) {
  53. $this->limits = $limits;
  54. $this->cgroup = $cgroup;
  55. if ( $restrictionMethod === 'autodetect' ) {
  56. // On Linux systems check for firejail
  57. if ( PHP_OS === 'Linux' && $this->findFirejail() !== false ) {
  58. $this->restrictionMethod = 'firejail';
  59. } else {
  60. $this->restrictionMethod = false;
  61. }
  62. } else {
  63. $this->restrictionMethod = $restrictionMethod;
  64. }
  65. $this->setLogger( new NullLogger() );
  66. }
  67. private function findFirejail() {
  68. if ( $this->firejail === null ) {
  69. $this->firejail = ExecutableFinder::findInDefaultPaths( 'firejail' );
  70. }
  71. return $this->firejail;
  72. }
  73. /**
  74. * When enabled, text sent to stderr will be logged with a level of 'error'.
  75. *
  76. * @param bool $yesno
  77. * @see Command::logStderr
  78. */
  79. public function logStderr( $yesno = true ) {
  80. $this->doLogStderr = $yesno;
  81. }
  82. /**
  83. * Instantiates a new Command
  84. *
  85. * @return Command
  86. */
  87. public function create(): Command {
  88. if ( $this->restrictionMethod === 'firejail' ) {
  89. $command = new FirejailCommand( $this->findFirejail() );
  90. $command->restrict( Shell::RESTRICT_DEFAULT );
  91. } else {
  92. $command = new Command();
  93. }
  94. $command->setLogger( $this->logger );
  95. return $command
  96. ->limits( $this->limits )
  97. ->cgroup( $this->cgroup )
  98. ->logStderr( $this->doLogStderr );
  99. }
  100. }