added comments and renamed short method to url generators

main
msquare 6 years ago
parent 5a83d4fb8b
commit 86b2937078

@ -2,14 +2,19 @@
namespace Engelsystem\Routing; namespace Engelsystem\Routing;
/**
* Provides urls when webserver rewriting is disabled.
*
* The urls have the form <app url>/index.php?p=<path>&<parameters>
*/
class LegacyUrlGenerator extends UrlGenerator class LegacyUrlGenerator extends UrlGenerator
{ {
/** /**
* @param string $path * @param string $path
* @param array $parameters * @param array $parameters
* @return string * @return string urls in the form <app url>/index.php?p=<path>&<parameters>
*/ */
public function to($path, $parameters = []) public function link_to($path, $parameters = [])
{ {
$page = ltrim($path, '/'); $page = ltrim($path, '/');
if (!empty($page)) { if (!empty($page)) {
@ -17,7 +22,7 @@ class LegacyUrlGenerator extends UrlGenerator
$parameters = array_merge(['p' => $page], $parameters); $parameters = array_merge(['p' => $page], $parameters);
} }
$uri = parent::to('index.php', $parameters); $uri = parent::link_to('index.php', $parameters);
$uri = preg_replace('~(/index\.php)+~', '/index.php', $uri); $uri = preg_replace('~(/index\.php)+~', '/index.php', $uri);
return $uri; return $uri;

@ -1,16 +1,19 @@
<?php <?php
namespace Engelsystem\Routing; namespace Engelsystem\Routing;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
/**
* Registers the url generator depending on config.
*/
class RoutingServiceProvider extends ServiceProvider class RoutingServiceProvider extends ServiceProvider
{ {
public function register() public function register()
{ {
$config = $this->app->get('config'); $config = $this->app->get('config');
$class = UrlGenerator::class; $class = UrlGenerator::class;
if (!$config->get('rewrite_urls', true)) { if (! $config->get('rewrite_urls', true)) {
$class = LegacyUrlGenerator::class; $class = LegacyUrlGenerator::class;
} }

@ -2,14 +2,19 @@
namespace Engelsystem\Routing; namespace Engelsystem\Routing;
/**
* Provides urls when rewriting on the webserver is enabled. (default)
*
* The urls have the form <app url>/<path>?<parameters>
*/
class UrlGenerator implements UrlGeneratorInterface class UrlGenerator implements UrlGeneratorInterface
{ {
/** /**
* @param string $path * @param string $path
* @param array $parameters * @param array $parameters
* @return string * @return string url in the form [app url]/[path]?[parameters]
*/ */
public function to($path, $parameters = []) public function link_to($path, $parameters = [])
{ {
$path = '/' . ltrim($path, '/'); $path = '/' . ltrim($path, '/');
$request = app('request'); $request = app('request');

@ -2,6 +2,9 @@
namespace Engelsystem\Routing; namespace Engelsystem\Routing;
/**
* To switch between different URL schemes.
*/
interface UrlGeneratorInterface interface UrlGeneratorInterface
{ {
/** /**
@ -9,5 +12,5 @@ interface UrlGeneratorInterface
* @param array $parameters * @param array $parameters
* @return string * @return string
*/ */
public function to($path, $parameters = []); public function link_to($path, $parameters = []);
} }

@ -124,7 +124,7 @@ function url($path = null, $parameters = [])
return $urlGenerator; return $urlGenerator;
} }
return $urlGenerator->to($path, $parameters); return $urlGenerator->link_to($path, $parameters);
} }
/** /**

@ -177,7 +177,7 @@ class HelpersTest extends TestCase
$this->assertEquals($urlGeneratorMock, url()); $this->assertEquals($urlGeneratorMock, url());
$urlGeneratorMock->expects($this->once()) $urlGeneratorMock->expects($this->once())
->method('to') ->method('link_to')
->with('foo/bar', ['param' => 'value']) ->with('foo/bar', ['param' => 'value'])
->willReturn('http://lorem.ipsum/foo/bar?param=value'); ->willReturn('http://lorem.ipsum/foo/bar?param=value');

@ -48,7 +48,7 @@ class LegacyUrlGeneratorTest extends TestCase
$urlGenerator = new LegacyUrlGenerator(); $urlGenerator = new LegacyUrlGenerator();
$this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator); $this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator);
$url = $urlGenerator->to($urlToPath, $arguments); $url = $urlGenerator->link_to($urlToPath, $arguments);
$this->assertEquals($expectedUrl, $url); $this->assertEquals($expectedUrl, $url);
} }
} }

@ -48,7 +48,7 @@ class UrlGeneratorTest extends TestCase
$urlGenerator = new UrlGenerator(); $urlGenerator = new UrlGenerator();
$this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator); $this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator);
$url = $urlGenerator->to($urlToPath, $arguments); $url = $urlGenerator->link_to($urlToPath, $arguments);
$this->assertEquals($expectedUrl, $url); $this->assertEquals($expectedUrl, $url);
} }
} }

Loading…
Cancel
Save