Skip to content

Commit

Permalink
fix(urls): saner urls for projects and releases
Browse files Browse the repository at this point in the history
  • Loading branch information
ewinslow committed Oct 6, 2014
1 parent 843f6ac commit 87f9170
Show file tree
Hide file tree
Showing 19 changed files with 1,192 additions and 306 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
/.settings
/.project
/.buildpath
/vendor/
70 changes: 70 additions & 0 deletions classes/Elgg/CommunityPlugins/UriTemplate.php
@@ -0,0 +1,70 @@
<?php

namespace Elgg\CommunityPlugins;


/**
* Represents a pattern for a URI with named parameters.
*
* Examples:
* * "/blog" (matches only "/blog" exactly)
* * "/blog/{name}" (matches "/blog/anything" but not "/blog/anything/more")
*/
class UriTemplate {

/** @type string */
private $template;

/**
* @param string $template
*/
public function __construct($template) {
$this->template = $template;
}


/**
* @example
*
* $template = new UriTemplate("/foo/{bar}")
* $result = $template->match("/foo/baz")
*
* // $result == array('bar' => 'baz')
*
* @example
*
* $template = new UriTemplate("/foo/{bar}")
* $result = $template->match("/baz/bop")
*
* // $result == NULL
*
*
* @param string $string The URI to match against
*
* @return null|array Null if not a match, otherwise associative array of
* matching parameters.
*/
public function match($string) {
// Convert {name} shorthand to regex for matching against $path
// E.g., "/blog/{guid}" => "/blog/(?<guid>[^/]+)"
$regex = preg_replace('/\{([a-z_]+)\}/', '(?<$1>[^/]+)', $this->template);

$matches = array();
// TODO(ewinslow): This fails to support uris with the # in them
$count = preg_match("#^$regex$#", $string, $matches);
if (!$count) {
return null;
}

// We want to keep just the named parameters, so:
// Convert: array(0 => '/blog/12345', 1 => 12345, 'guid' => 12345)
// To: array('guid' => 12345)
$result = array();
foreach ($matches as $key => $value) {
if (!is_int($key)) {
$result[$key] = $value;
}
}
return $result;
}
}
8 changes: 7 additions & 1 deletion composer.json
Expand Up @@ -9,6 +9,9 @@
],
"homepage": "http://www.elgg.org/",
"license": "GPL-2.0",
"autoload": {
"psr-0": {"": "classes/"}
},
"authors": [
{
"name": "Elgg core developers",
Expand All @@ -18,5 +21,8 @@
],
"require": {
"composer/installers": ">=1.0.8"
},
"require-dev": {
"phpunit/phpunit": "~4.3"
}
}
}

0 comments on commit 87f9170

Please sign in to comment.