<commit_before><?php

namespace Zeroem\RequestExecutorBundle;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Utility class for parsing a Request object into a cURL request
 * and executing it.
 *
 * @author <NAME> <<EMAIL>>
 */
class Executor
{
  static private $_methodOptionMap = array(
    "GET"=>CURLOPT_GET,
    "POST"=>CURLOPT_POST,
    "HEAD"=>CURLOPT_NOBODY,
    "PUT"=>CURLOPT_PUT
  );

  /**
   * Execute a Request object via cURL
   *
   * @param Request $request the request to execute
   * @param array $options additional curl options to set/override
   * @return Response
   */
  static public function execute(Request $request, array $options = array()) {

    $handle = curl_init($request->getUri());

    curl_setopt($handle,CURLOPT_RETURNTRANSFER,true);

    static::setMethod($handle,$request);

    if ("POST" === $request->getMethod()) {
      static::setPostFields($handle, $request);
    }

    // Provided Options should override interpreted options
    curl_setopt_array($handle, $options);

    $result = curl_exec($handle);
    curl_close($handle);
  }

  static private function setPostFields(resource $handle, Request $request) {
    $postfields = null;
    $content = $request->getContent();

    if (null !== $content) {
      $postfields = $content;
    } else if (count($request->getRequest()->all()) > 0) {
      $postfields = $request->getRequest()->all();
    }

    curl_setopt($handle, CURLOPT_POSTFIELDS, $postfields);
  }

  static private function setMethod(resource $handle, Request $request) {
    $method = $request->getMethod();

    if(isset(static::$_methodOptionMap[$method])) {
      curl_setopt($handle,static::$_methodOptionMap[$method],true);
    } else {
      curl_setopt($handle,CURLOPT_CUSTOMREQUEST,$method);
    }
  }

  static private errorCheckAndHandling(resource $handle) {
      $error_no = curl_errno($handle);

      if(0 !== $error_no) {
          throw new CurlErrorException(curl_error($handle), $error_no);
      }
  }
  
}<commit_msg>normalized indentation to 4 spaces and eliminated tabs.  Added header loading<commit_after><?php

namespace Zeroem\RequestExecutorBundle;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\HeaderBag;

/**
 * Utility class for parsing a Request object into a cURL request
 * and executing it.
 *
 * @author <NAME> <<EMAIL>>
 */
class Executor
{
    static private $_methodOptionMap = array(
        "GET"=>CURLOPT_GET,
        "POST"=>CURLOPT_POST,
        "HEAD"=>CURLOPT_NOBODY,
        "PUT"=>CURLOPT_PUT
    );

    /**
     * Execute a Request object via cURL
     *
     * @param Request $request the request to execute
     * @param array $options additional curl options to set/override
     * @return Response
     */
    static public function execute(Request $request, array $options = array()) {

        $handle = curl_init($request->getUri());

        curl_setopt_array(
            $handle,
            array(
                CURLOPT_RETURNTRANSFER=>true,
                CURLOPT_HTTPHEADER=>static::buildHeadersArray($request->headers)
            )
        );

        static::setMethod($handle,$request);

        if ("POST" === $request->getMethod()) {
            static::setPostFields($handle, $request);
        }

        // Provided Options should override interpreted options
        curl_setopt_array($handle, $options);

        $result = curl_exec($handle);
        
        $exception = static::getErrorException($handle);
        curl_close($handle);
        
        if (false !== $exception) {
            throw $exception;
        }

        return $result;
    }

    static private function setPostFields(resource $handle, Request $request) {
        $postfields = null;
        $content = $request->getContent();

        if (null !== $content) {
            $postfields = $content;
        } else if (count($request->getRequest()->all()) > 0) {
            $postfields = $request->getRequest()->all();
        }

        curl_setopt($handle, CURLOPT_POSTFIELDS, $postfields);
    }

    static private function setMethod(resource $handle, Request $request) {
        $method = $request->getMethod();

        if (isset(static::$_methodOptionMap[$method])) {
            curl_setopt($handle,static::$_methodOptionMap[$method],true);
        } else {
            curl_setopt($handle,CURLOPT_CUSTOMREQUEST,$method);
        }
    }

    static private function getErrorException(resource $handle) {
        $error_no = curl_errno($handle);

        if (0 !== $error_no) {
            return new CurlErrorException(curl_error($handle), $error_no);
        }

        return false;
    }

    static private function buildHeadersArray(HeaderBag $headerBag) {
        $headers = array();

        foreach($headerBag->all() as $header=>$value) {
            $headers[] = makeHeaderName($header) . ": {$value}";
        }

        return $headers;
    }

    static private function makeHeaderName($str) {
        $parts = explode("_",strtolower($str));
        
        foreach($parts as &$part) {
            $part = ucfirst($part);
        }

        return implode("-",$parts);
    }
}
{'fraction_non_alphanumeric': 0.13160897679726133, 'fraction_numerical': 0.0009509319132750095, 'mean_word_length': 2.864070536370316, 'pattern_counts': {'":': 1, '<': 11, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1487078', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1492, 'n_words': 328}