<commit_before><?php

declare(strict_types=1);

/**
 * Siler routing facilities.
 */

namespace Siler\Route;

use Psr\Http\Message\ServerRequestInterface;
use Siler\Container;
use Siler\Http;
use Siler\Http\Request;
use function Siler\require_fn;

/**
 * Define a new route using the GET HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function get(string $path, $callback, $request = null)
{
    return route('get', $path, $callback, $request);
}

/**
 * Define a new route using the POST HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function post(string $path, $callback, $request = null)
{
    return route('post', $path, $callback, $request);
}

/**
 * Define a new route using the PUT HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function put(string $path, $callback, $request = null)
{
    return route('put', $path, $callback, $request);
}

/**
 * Define a new route using the DELETE HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function delete(string $path, $callback, $request = null)
{
    return route('delete', $path, $callback, $request);
}

/**
 * Define a new route using the OPTIONS HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function options(string $path, $callback, $request = null)
{
    return route('options', $path, $callback, $request);
}

/**
 * Define a new route using the any HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function any(string $path, $callback, $request = null)
{
    return route('any', $path, $callback, $request);
}

/**
 * Define a new route.
 *
 * @param string|array                      $method   The HTTP request method to listen on
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  Null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function route($method, string $path, $callback, $request = null)
{
    if (Container\get('route_match', false) && Container\get('route_stop_propagation', false)) {
        return null;
    }

    $path = regexify($path);

    if (is_string($callback)) {
        $callback = require_fn($callback);
    }

    $methodPath = method_path($request);

    if (count($methodPath) >= 2 && (Request\method_is($method, $methodPath[0]) || $method == 'any') &&
        preg_match($path, $methodPath[1], $params)) {
        Container\set('route_match', true);

        return $callback($params);
    }

    return null;
}

/**
 * @internal Used to guess the given request method and path.
 *
 * @param array|ServerRequestInterface|null $request  Null, array[method, path] or Psr7 Request Message
 *<commit_msg>Apply fixes from StyleCI (#146)<commit_after><?php

declare(strict_types=1);

/**
 * Siler routing facilities.
 */

namespace Siler\Route;

use Psr\Http\Message\ServerRequestInterface;
use Siler\Container;
use Siler\Http;
use Siler\Http\Request;
use function Siler\require_fn;

/**
 * Define a new route using the GET HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function get(string $path, $callback, $request = null)
{
    return route('get', $path, $callback, $request);
}

/**
 * Define a new route using the POST HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function post(string $path, $callback, $request = null)
{
    return route('post', $path, $callback, $request);
}

/**
 * Define a new route using the PUT HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function put(string $path, $callback, $request = null)
{
    return route('put', $path, $callback, $request);
}

/**
 * Define a new route using the DELETE HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function delete(string $path, $callback, $request = null)
{
    return route('delete', $path, $callback, $request);
}

/**
 * Define a new route using the OPTIONS HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function options(string $path, $callback, $request = null)
{
    return route('options', $path, $callback, $request);
}

/**
 * Define a new route using the any HTTP method.
 *
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function any(string $path, $callback, $request = null)
{
    return route('any', $path, $callback, $request);
}

/**
 * Define a new route.
 *
 * @param string|array                      $method   The HTTP request method to listen on
 * @param string                            $path     The HTTP URI to listen on
 * @param string|callable                   $callback The callable to be executed or a string to be used with Siler\require_fn
 * @param array|ServerRequestInterface|null $request  Null, array[method, path] or Psr7 Request Message
 *
 * @return mixed|null
 */
function route($method, string $path, $callback, $request = null)
{
    if (Container\get('route_match', false) && Container\get('route_stop_propagation', false)) {
        return null;
    }

    $path = regexify($path);

    if (is_string($callback)) {
        $callback = require_fn($callback);
    }

    $methodPath = method_path($request);

    if (count($methodPath) >= 2 && (Request\method_is($method, $methodPath[0]) || $method == 'any') &&
        preg_match($path, $methodPath[1], $params)) {
        Container\set('route_match', true);

        return $callback($params);
    }

    return null;
}

/**
 * @internal Used to guess the given request method and path.
 *
 * @param array|ServerRequestInterface|null $request Null, array[method, path] or Psr7 Request Message
 *
{'fraction_non_alphanumeric': 0.10678132143245891, 'fraction_numerical': 0.002938935452269511, 'mean_word_length': 2.8965224766751483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5406307', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2545, 'n_words': 942}