Code Monkey home page Code Monkey logo

Comments (17)

WinterSilence avatar WinterSilence commented on May 22, 2024 1

@fenric first, but it's not URI - it's path

from http-router.

fenric avatar fenric commented on May 22, 2024

Hi! Now I prepare the 2.0.0 version . I will try to develop your feature.

from http-router.

DevFelixDorn avatar DevFelixDorn commented on May 22, 2024

Great! Thanks. I could submit a pr if you want.

from http-router.

WinterSilence avatar WinterSilence commented on May 22, 2024
// Defines the pattern of a `<attribute>`
protected const ATTRIBUTE = '<([a-zA-Z0-9_]++)>';
// Matches a URI group and captures the contents
protected const GROUP = '\(((?:(?>[^()]+)|(?R))*)\)';
public const SECTION = '#(?:' . static::ATTRIBUTE . '|' . static::GROUP . ')#u';
/**
 * @var array Stores compiled URIs
 */
protected $compiledUri = [];

/**
 * Recursively compiles a path of a URI specification by replacing the specified attributes and
 * any optional attributes that are needed.
 *
 * @param string $path Part of the URI specification
 * @param bool $required Whether or not attributes are required (initially)
 * @param array $attributes Specified attributes
 * @return array Tuple of the compiled path and whether or not it contained specified attributes
 * @throws InvalidArgumentException
 */
protected function compileUri(string $path, bool $required, array $attributes)
{
    $missing = [];
    
    $uri = preg_replace_callback(
        static::SECTION, 
        function ($matches) use (& $missing, & $required, $attributes) {
            if ($matches[0][0] == '<') {
                // Unwrapped attribute
                $attr = $matches[1];
                if (isset($attributes[$attr])) {
                    // This path is required when a specified attribute does not match the default.
                    if (! $required) {
                        $required = ! isset($this->attributes[$attr]) || $attributes[$attr] !== $this->attributes[$attr];
                    }
                    // Add specified attribute to this result
                    return $attributes[$attr];
                }
                
                // Add default attribute to this result
                if (isset($this->attributes[$attr])) {
                    return $this->attributes[$attr];
                }
                
                // This path is missing a attribute
                $missing[] = $attr;
            } else {
                // Unwrapped group
                $result = $this->compileUri($matches[2], false, $attributes);
                if (! empty($result[1])) {
                    // This path is required when it contains a group that is required
                    $required = true;
                    // Add required groups to this result
                    return $result[0];
                }
                // Note: don't add optional groups to this result!
            }
        }, 
        $path
    );
    
    if ($required && $missing) {
        $missing = implode(', ', $missing);
        $msg = sprintf('Route %s: required attributes (%s) not passed', $this->id, $missing);
        throw new InvalidArgumentException($msg);
    }

    return [$uri, $required];
}

/**
 * Generates a URI for the current route based on the attributes given.
 * 
 * @param array $attributes URI attributes
 * @return string Compiled URI
 */
public function getUri(array $attributes = [])
{
    $key = md5(serialize($attributes));
    
    if (! isset($this->compiledUri[$key])) {
        if ($attributes) {
            $attributes = array_map('rawurlencode', $attributes);
            // Decode slashes back, see Apache docs about AllowEncodedSlashes and AcceptPathInfo
            $attributes = str_replace(['%2F', '%5C'], ['/', '\\'], $attributes);
        }
        
        list($uri) = $this->compileUri($this->path, true, $attributes);
        // Trim all extra slashes from the URI
        $this->compiledUri[$key] = preg_replace('#//+#u', '/', $uri);
    }
    
    return $this->compiledUri[$key];
}

from http-router.

dominikzogg avatar dominikzogg commented on May 22, 2024

chubbyphp/chubbyphp-framework#6

from http-router.

fenric avatar fenric commented on May 22, 2024

what do you think is better?


$router->getRoute('read.entity')->getUri(['id' => 100]);

or

$router->getUri('read.entity', ['id' => 100]);

from http-router.

fenric avatar fenric commented on May 22, 2024

It's will be added in the 2.0 version.

from http-router.

dominikzogg avatar dominikzogg commented on May 22, 2024
$router->getPath('read.entity', ['id' => 100]);

from http-router.

WinterSilence avatar WinterSilence commented on May 22, 2024

@dominikzogg 1. compileRoutePath/generateRoutePath pure for users 2. this not SOLID

from http-router.

fenric avatar fenric commented on May 22, 2024

@fenric first, but it's not URI - it's path

yes, you're right, but... method Route::getPath() already exists... maybe Route::buildPath()?

$router->getRoute('read.entity')->buildPath($attrs = ['id' => 100], $strict = true);

what do you think?

from http-router.

WinterSilence avatar WinterSilence commented on May 22, 2024

@fenric it's not SOLID. in my case current route in request object.

from http-router.

fenric avatar fenric commented on May 22, 2024

it's not SOLID. in my case current route in request object.

but...

$route->buildPath($attrs = ['id' => 100], $strict = true);

or for your case...

$this->getRequest()->getRoute()->buildPath($attrs = ['id' => 100], $strict = true);

it's also correct...

$router->getRoute('read.entity')->buildPath($attrs = ['id' => 100], $strict = true);

from http-router.

WinterSilence avatar WinterSilence commented on May 22, 2024

@fenric my error, incorrectly read your comment

from http-router.

fenric avatar fenric commented on May 22, 2024

the version 2.0.0 contains it feature.

examples:

$route->buildPath(array $attributes, bool $strict);

or...

$router->getRoute('foo')->buildPath(array $attributes, bool $strict);

from http-router.

fenric avatar fenric commented on May 22, 2024

The official release of the new version will be released at the coming days.

from http-router.

dominikzogg avatar dominikzogg commented on May 22, 2024

it seems that buildPath does not support /path(/{optional)}

from http-router.

WinterSilence avatar WinterSilence commented on May 22, 2024

@dominikzogg look https://github.com/sunrise-php/http-router/blob/release/v2.0.0/functions/path_parse.php - it's too hard for me :) I'm write other version mixing my old router with that. When all done, we will compare and maybe combine versions.

from http-router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.