<commit_before><?php //-->
/**
 * This file is part of the Cradle PHP Library.
 * (c) 2016-2018 Openovate Labs
 *
 * Copyright and license information can be found at LICENSE.txt
 * distributed with this package.
 */

namespace Cradle\Http;

use Cradle\Event\EventTrait;
use Cradle\Event\EventHandler;
use Cradle\Resolver\ResolverTrait;
use Cradle\Http\Request\RequestInterface;
use Cradle\Http\Router\RouterInterface;

/**
 * Handles method-path matching and routing
 *
 * @vendor   Cradle
 * @package  Http
 * @author   <NAME> <<EMAIL>>
 * @standard PSR-2
 */
class Router implements RouterInterface
{
    use EventTrait, ResolverTrait;

    /**
     * Allow to pass a custom EventHandler
     */
    public function __construct(EventHandler $handler = null)
    {
        //but we do need one
        if (is_null($handler)) {
            $handler = $this->resolve(EventHandler::class);
        }

        $this->setEventHandler($handler);
    }

    /**
     * Process routes
     *
     * @param *RequestInterface $request
     * @param mixed             ...$args
     *
     * @return bool
     */
    public function process(RequestInterface $request, ...$args): bool
    {
        $path = $request->getPath('string');
        $method = $request->getMethod();
        $event = $method.' '.$path;

        return $this
            ->getEventHandler()
            ->trigger($event, $request, ...$args)
            ->getMeta();
    }

    /**
     * Adds routing middleware
     *
     * @param *string   $method   The request method
     * @param *string   $pattern  The route pattern
     * @param *callable $callback The middleware handler
     *
     * @return RouterInterface
     */
    public function route(string $method, string $pattern, callable $callback): RouterInterface
    {
        //hard requirement<commit_msg>fix: when a route returns false, we should stop<commit_after><?php //-->
/**
 * This file is part of the Cradle PHP Library.
 * (c) 2016-2018 Openovate Labs
 *
 * Copyright and license information can be found at LICENSE.txt
 * distributed with this package.
 */

namespace Cradle\Http;

use Cradle\Event\EventTrait;
use Cradle\Event\EventHandler;
use Cradle\Resolver\ResolverTrait;
use Cradle\Http\Request\RequestInterface;
use Cradle\Http\Router\RouterInterface;

/**
 * Handles method-path matching and routing
 *
 * @vendor   Cradle
 * @package  Http
 * @author   <NAME> <<EMAIL>>
 * @standard PSR-2
 */
class Router implements RouterInterface
{
    use EventTrait, ResolverTrait;

    /**
     * Allow to pass a custom EventHandler
     */
    public function __construct(EventHandler $handler = null)
    {
        //but we do need one
        if (is_null($handler)) {
            $handler = $this->resolve(EventHandler::class);
        }

        $this->setEventHandler($handler);
    }

    /**
     * Process routes
     *
     * @param *RequestInterface $request
     * @param mixed             ...$args
     *
     * @return bool
     */
    public function process(RequestInterface $request, ...$args): bool
    {
        $path = $request->getPath('string');
        $method = $request->getMethod();
        $event = $method.' '.$path;

        return $this
            ->getEventHandler()
            ->trigger($event, $request, ...$args)
            ->getMeta() !== EventHandler::STATUS_INCOMPLETE;
    }

    /**
     * Adds routing middleware
     *
     * @param *string   $method   The request method
     * @param *string   $pattern  The route pattern
     * @param *callable $callback The middleware handler
     *
     * @return RouterInterface
     */
    public function route(string $method, string $pattern, callable $callback): RouterInterface
    {
        //hard requirement
{'fraction_non_alphanumeric': 0.12078200321371184, 'fraction_numerical': 0.0048205677557579, 'mean_word_length': 2.386219401631913, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '7178683', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1071, 'n_words': 298}