Skip to content
Snippets Groups Projects
bootstrap.php 1.55 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kevin Cristiano's avatar
    Kevin Cristiano committed
    <?php
    
    // We switch between a few different container configurations during testing.
    define('CIVICRM_CONTAINER_CACHE', 'never');
    
    ini_set('memory_limit', '2G');
    // phpcs:disable
    eval(cv('php:boot', 'phpcode'));
    // phpcs:enable
    assert("CIVICRM_UF === 'WordPress'");
    
    /**
     * Call the "cv" command.
     *
     * @param string $cmd
     *   The rest of the command to send.
     * @param string $decode
     *   Ex: 'json' or 'phpcode'.
     * @return string
     *   Response output (if the command executed normally).
     * @throws \RuntimeException
     *   If the command terminates abnormally.
     */
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
    function cv(string $cmd, $decode = 'json'): string {
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
      $cmd = 'cv ' . $cmd;
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
      $descriptorSpec = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => STDERR];
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
      $oldOutput = getenv('CV_OUTPUT');
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
      putenv('CV_OUTPUT=json');
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
      $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
      putenv("CV_OUTPUT=$oldOutput");
      fclose($pipes[0]);
      $result = stream_get_contents($pipes[1]);
      fclose($pipes[1]);
      if (proc_close($process) !== 0) {
        throw new RuntimeException("Command failed ($cmd):\n$result");
      }
      switch ($decode) {
        case 'raw':
          return $result;
    
        case 'phpcode':
          // If the last output is /*PHPCODE*/, then we managed to complete execution.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
          if (substr(trim($result), 0, 12) !== '/*BEGINPHP*/' || substr(trim($result), -10) !== '/*ENDPHP*/') {
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
            throw new \RuntimeException("Command failed ($cmd):\n$result");
          }
          return $result;
    
        case 'json':
          return json_decode($result, 1);
    
        default:
          throw new RuntimeException("Bad decoder format ($decode)");
      }
    }