added fragment support to URI #711

This commit is contained in:
Andy Miller
2016-03-04 16:33:18 -07:00
parent 42222d1be4
commit 14353c50d8
2 changed files with 35 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ class Uri
protected $scheme;
protected $port;
protected $query;
protected $fragment;
protected $root;
protected $root_path;
protected $uri;
@@ -196,6 +197,7 @@ class Uri
$this->host = [];
$this->root = [];
$this->url = [];
$this->fragment = [];
$grav = Grav::instance();
@@ -220,6 +222,10 @@ class Uri
parse_str($uri_bits['query'], $this->query);
}
if (isset($uri_bits['fragment'])) {
$this->fragment = $uri_bits['fragment'];
}
$this->base = $this->buildBaseUrl();
$this->host = $this->buildHost();
$this->env = $this->buildEnvironment();
@@ -300,6 +306,11 @@ class Uri
$uri = $bits['path'];
}
//process fragment
if (isset($bits['fragment'])) {
$this->fragment = $bits['fragment'];
}
// remove the extension if there is one set
$parts = pathinfo($uri);
@@ -464,6 +475,21 @@ class Uri
}
}
/**
* Gets the Fragment portion of a URI (eg #target)
*
* @param null $fragment
*
* @return null
*/
public function fragment($fragment = null)
{
if ($fragment !== null) {
$this->fragment = $fragment;
}
return $this->fragment;
}
/**
* Return URL.
*

View File

@@ -177,6 +177,15 @@ class UriTest extends \Codeception\TestCase\Test
$this->assertSame('yyy', $this->uri->param('test'));
}
public function testFragment()
{
$this->uri->initializeWithURL('http://localhost:8080/a/b/c#my-fragment');
$this->assertSame('my-fragment', $this->uri->fragment());
$this->uri->initializeWithURL('http://localhost:8080/a/b/c');
$this->uri->fragment('something-new');
$this->assertSame('something-new', $this->uri->fragment());
}
public function testUrl()
{
$this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper')->init();