2015-09-14 15:49:20 -04:00
|
|
|
<?php
|
2015-09-14 19:54:34 -04:00
|
|
|
/**
|
|
|
|
* Base class for routing to make namespaced config settings
|
|
|
|
* easier to work with
|
|
|
|
*/
|
2015-09-15 13:19:29 -04:00
|
|
|
namespace Aviat\AnimeClient;
|
2015-09-14 15:49:20 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
|
|
|
|
2015-09-14 19:54:34 -04:00
|
|
|
/**
|
|
|
|
* Base for routing/url classes
|
|
|
|
*/
|
2015-09-14 15:49:20 -04:00
|
|
|
class RoutingBase {
|
2015-10-09 14:34:55 -04:00
|
|
|
|
|
|
|
use \Aviat\Ion\StringWrapper;
|
|
|
|
|
2015-09-14 15:49:20 -04:00
|
|
|
/**
|
|
|
|
* Injection Container
|
|
|
|
* @var Container $container
|
|
|
|
*/
|
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Config Object
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Routing array
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $routes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2015-10-06 10:24:48 -04:00
|
|
|
* @param ContainerInterface $container
|
2015-09-14 15:49:20 -04:00
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
public function __construct(ContainerInterface $container)
|
2015-09-14 15:49:20 -04:00
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
$this->config = $container->get('config');
|
2015-10-06 11:38:20 -04:00
|
|
|
$this->routes = $this->config->get('routes');
|
2015-09-14 15:49:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive the appropriate value for the routing key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($key)
|
|
|
|
{
|
2015-10-06 11:38:20 -04:00
|
|
|
$routing_config = $this->config->get('routing');
|
2015-09-14 15:49:20 -04:00
|
|
|
|
|
|
|
if (array_key_exists($key, $routing_config))
|
|
|
|
{
|
|
|
|
return $routing_config[$key];
|
|
|
|
}
|
|
|
|
}
|
2015-10-09 14:34:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current url path
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function path()
|
|
|
|
{
|
|
|
|
$request = $this->container->get('request');
|
|
|
|
$path = $request->server->get('REQUEST_URI');
|
|
|
|
$cleaned_path = $this->string($path)
|
|
|
|
->trim()
|
|
|
|
->trimRight('/')
|
|
|
|
->ensureLeft('/');
|
|
|
|
|
2015-10-09 14:55:15 -04:00
|
|
|
return (string)$cleaned_path;
|
2015-10-09 14:34:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the url segments
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function segments()
|
|
|
|
{
|
|
|
|
$path = $this->path();
|
|
|
|
$segments = explode('/', $path);
|
|
|
|
|
|
|
|
return $segments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a segment of the current url
|
|
|
|
*
|
|
|
|
* @param int $num
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function get_segment($num)
|
|
|
|
{
|
|
|
|
$segments = $this->segments();
|
|
|
|
return (array_key_exists($num, $segments)) ? $segments[$num] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the last url segment
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function last_segment()
|
|
|
|
{
|
|
|
|
$segments = $this->segments();
|
|
|
|
return end($segments);
|
|
|
|
}
|
2015-09-14 15:49:20 -04:00
|
|
|
}
|
|
|
|
// End of RoutingBase.php
|