You are here

class ShellManager in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 src/ShellManager.php \Drupal\fillpdf\ShellManager

Manage execution of shell commands.

@internal

Hierarchy

Expanded class hierarchy of ShellManager

2 files declare their use of ShellManager
FillPdfSettingsForm.php in src/Form/FillPdfSettingsForm.php
PdftkPdfBackend.php in src/Plugin/PdfBackend/PdftkPdfBackend.php
1 string reference to 'ShellManager'
fillpdf.services.yml in ./fillpdf.services.yml
fillpdf.services.yml
1 service uses ShellManager
fillpdf.shell_manager in ./fillpdf.services.yml
Drupal\fillpdf\ShellManager

File

src/ShellManager.php, line 12

Namespace

Drupal\fillpdf
View source
class ShellManager implements ShellManagerInterface {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Whether we are running on Windows OS.
   *
   * @var bool
   */
  protected $isWindows;

  /**
   * Constructs a ShellManager object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
    $this->isWindows = substr(PHP_OS, 0, 3) === 'WIN';
  }

  /**
   * {@inheritdoc}
   */
  public function isWindows() {
    return $this->isWindows;
  }

  /**
   * {@inheritdoc}
   */
  public function getInstalledLocales() {
    if ($this
      ->isWindows()) {
      return [];
    }
    $output = [];
    $status = NULL;
    exec("locale -a", $output, $status);
    return array_combine($output, $output);
  }

  /**
   * {@inheritdoc}
   */
  public function escapeShellArg($arg) {

    // Put the configured locale in a static to avoid multiple config get calls
    // in the same request.
    static $config_locale;
    if (!isset($config_locale)) {
      $config_locale = $this->configFactory
        ->get('fillpdf.settings')
        ->get('shell_locale');
    }
    $current_locale = setlocale(LC_CTYPE, 0);
    if ($this
      ->isWindows()) {

      // Temporarily replace % characters.
      $arg = str_replace('%', static::PERCENTAGE_REPLACE, $arg);
    }
    if ($current_locale !== $config_locale) {

      // Temporarily swap the current locale with the configured one, if
      // available. Otherwise fall back.
      setlocale(LC_CTYPE, [
        $config_locale,
        'C.UTF-8',
        $current_locale,
      ]);
    }
    $arg_escaped = escapeshellarg($arg);
    if ($current_locale !== $config_locale) {

      // Restore the current locale.
      setlocale(LC_CTYPE, $current_locale);
    }

    // Get our % characters back.
    if ($this
      ->isWindows()) {
      $arg_escaped = str_replace(static::PERCENTAGE_REPLACE, '%', $arg_escaped);
    }
    return $arg_escaped;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ShellManager::$configFactory protected property The configuration factory.
ShellManager::$isWindows protected property Whether we are running on Windows OS.
ShellManager::escapeShellArg public function Escapes a string. Overrides ShellManagerInterface::escapeShellArg
ShellManager::getInstalledLocales public function Gets the list of locales installed on the server. Overrides ShellManagerInterface::getInstalledLocales
ShellManager::isWindows public function Whether we are running on Windows OS. Overrides ShellManagerInterface::isWindows
ShellManager::__construct public function Constructs a ShellManager object.
ShellManagerInterface::PERCENTAGE_REPLACE constant Replacement for percentage while escaping.