Source of file RouteConfig.php

Size: 7,790 Bytes - Last Modified: 2016-08-18T01:16:22+00:00

../src/Aviat/Director/RouteConfig.php

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
Covered by 2 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #14
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #15
5455565758596061626364656667
Covered by 2 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #12
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #13
6869707172737475767778798081
Covered by 2 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #0
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #1
8283848586878889909192939495
Covered by 2 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #2
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #3
96979899100101102103104105106107108109
Covered by 2 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #10
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #11
110111112113114115116117118119120121122
Covered by 2 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #8
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #9
123124125126127128129130131132133134135136
Covered by 2 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #4
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #5
137138139140141142143144145146147148149150
Covered by 2 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #6
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #7
151152153154155156157158159160161162
Covered by 16 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #0
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #1
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #2
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #3
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #4
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #5
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #6
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #7
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #8
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #9
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #10
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #11
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #12
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #13
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #14
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #15
163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
Covered by 16 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #0
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #1
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #2
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #3
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #4
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #5
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #6
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #7
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #8
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #9
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #10
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #11
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #12
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #13
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #14
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #15
284
Covered by 16 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #0
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #1
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #2
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #3
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #4
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #5
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #6
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #7
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #8
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #9
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #10
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #11
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #12
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #13
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #14
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #15
285286287
Covered by 16 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #0
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #1
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #2
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #3
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #4
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #5
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #6
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #7
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #8
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #9
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #10
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #11
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #12
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #13
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #14
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #15
288289
Covered by 8 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #1
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #3
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #5
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #7
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #9
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #11
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #13
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #15
290291292
Covered by 16 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #0
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #1
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #2
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #3
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #4
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #5
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #6
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #7
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #8
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #9
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #10
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #11
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #12
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #13
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #14
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #15
293
Covered by 16 test(s):
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #0
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #1
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #2
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #3
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #4
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #5
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #6
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #7
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #8
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #9
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #10
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #11
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #12
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #13
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #14
  • RouteConfigTest::testGetPostPutPatchOptionsDeleteAll with data set #15
294
<?php
/**
 * Director
 *
 * A general purpose url router and generator
 *
 * @package     Director
 * @author      Timothy J. Warren
 * @copyright   Copyright (c) 2016
 * @link        https://git.timshomepage.net/timw4mail/director
 * @license     MIT
 */

namespace Aviat\Director;

/**
 * Class to configure routes
 */
class RouteConfig {

	/**
	 * Route mapping
	 *
	 * @var array
	 */
	protected $map = [
		'base' => [
			'namespace' => '',
			'class' => '',
			'method' => 'index',
		],
		'routes' => [
			'get' => [],
			'post' => [],
			'put' => [],
			'patch' => [],
			'options' => [],
			'any' => [],
		]
	];
	
	/**
	 * Add a route matching any HTTP verb
	 *
	 * @param string $name - The name of the route
	 * @param string $path - The path or pattern to match
	 * @param array|callable $callback - The callback to dispatch on match
	 * @param [array] $options - additional route options, such as placeholders
	 * @return RouteConfig
	 */
	public function any($name, $path, $callback, array $options = [])
	{
		return $this->addRouteToMap('any', $name, $path, $callback, $options);
	}
	
	/**
	 * Add a route matching a DELETE HTTP request
	 *
	 * @param string $name - The name of the route
	 * @param string $path - The path or pattern to match
	 * @param array|callable $callback - The callback to dispatch on match
 	 * @param [array] $options - additional route options, such as placeholders
	 * @return RouteConfig
	 */
	public function delete($name, $path, $callback, array $options = [])
	{
		return $this->addRouteToMap('delete', $name, $path, $callback, $options);
	}

	/**
	 * Add a route matching a GET HTTP request
	 *
	 * @param string $name - The name of the route
	 * @param string $path - The path or pattern to match
	 * @param array|callable $callback - The callback to dispatch on match
	 * @param [array] $options - additional route options, such as placeholders
	 * @return RouteConfig
	 */
	public function get($name, $path, $callback, array $options = [])
	{
		return $this->addRouteToMap('get', $name, $path, $callback, $options);
	}
	
	/**
	 * Add a route matching a HEAD HTTP request
	 *
	 * @param string $name - The name of the route
	 * @param string $path - The path or pattern to match
	 * @param array|callable $callback - The callback to dispatch on match
	 * @param [array] $options - additional route options, such as placeholders
	 * @return RouteConfig
	 */
	public function head($name, $path, $callback, array $options = [])
	{
		return $this->addRouteToMap('head', $name, $path, $callback, $options);
	}
	
	/**
	 * Add a route matching a OPTIONS HTTP request
	 *
	 * @param string $name - The name of the route
	 * @param string $path - The path or pattern to match
	 * @param array|callable $callback - The callback to dispatch on match
 	 * @param [array] $options - additional route options, such as placeholders
	 * @return RouteConfig
	 */
	public function options($name, $path, $callback, array $options = [])
	{
		return $this->addRouteToMap('options', $name, $path, $callback, $options);
	}
	
	/**
	 * Add a route matching a PATCH HTTP request
	 *
	 * @param string $name - The name of the route
	 * @param string $path - The path or pattern to match
	 * @param array|callable $callback - The callback to dispatch on match
	 * @return RouteConfig
	 */
	public function patch($name, $path, $callback, array $options = [])
	{
		return $this->addRouteToMap('patch', $name, $path, $callback, $options);
	}
	
	/**
	 * Add a route matching a POST HTTP request
	 *
	 * @param string $name - The name of the route
	 * @param string $path - The path or pattern to match
	 * @param array|callable $callback - The callback to dispatch on match
 	 * @param [array] $options - additional route options, such as placeholders
	 * @return RouteConfig
	 */
	public function post($name, $path, $callback, array $options = [])
	{
		return $this->addRouteToMap('post', $name, $path, $callback, $options);
	}
	
	/**
	 * Add a route matching a PUT HTTP request
	 *
	 * @param string $name - The name of the route
	 * @param string $path - The path or pattern to match
	 * @param array|callable $callback - The callback to dispatch on match
 	 * @param [array] $options - additional route options, such as placeholders
	 * @return RouteConfig
	 */
	public function put($name, $path, $callback, array $options = [])
	{
		return $this->addRouteToMap('put', $name, $path, $callback, $options);
	}
	

	
	/**
	 * Returns the routing config array
	 *
	 * @return array
	 */
	public function getConfig()
	{
		return $this->map;
	}
	
	/**
	 * Set route config defaults
	 *
	 * @param string $namespace - namespace used for controllers, unless explicit
	 * @param [string] $class - controller class to default to, if no routes match
	 * @param [string] $method - default controller method, if not specified in the config item
	 * @return RouteConfig
	 */
	public function setBaseConfig($namespace, $class = '', $method = 'index')
	{
		$this->map['base'] = [
			'namespace' => $namespace,
			'class' => $class,
			'method' => $method
		];
		
		return $this;
	}
	
	/**
	 * Batch set the routes 
	 * 
	 * @example // Route config format
	 *	$routes = [
	 *		'get' => [ // GET routes
	 *			'homepage' => [ // route name => params
	 *				'path' => '/',
	 *				'callback' => [ // Array like so, or callable, like a lamda
	 *					'class' => 'HomeController',
	 *					'method' => 'index',
	 *					'args' => [], // Arguments to pass to controller method
	 *				],
	 *			],
	 *		],
	 *		...
	 *	];
	 *
	 * @param array $routes - the route mapping
	 * @return RouteConfig
	 */
	public function setRouteConfig(array $routes)
	{
		foreach($routes as $verb => $map)
		{
			foreach($map as $path => $setup)
			{
				$this->map['routes'][$verb][$path] = $setup;
			}
		}
		
		return $this;
	}
	
	/**
	 * Batch set all the route options with one array
	 *
	 * If no 'routes' key is set in the array, only the routes config will be loaded.
	 * 
	 * @example 
	 *	// Route config format
	 *	$config = [
	 * 		'base' => [ // Default Controller setup
	 *			'namespace' => 'Foo\\Bar', // namespace used for controllers, unless explicit
	 *			'class' => 'Baz', // controller class to default to, if no routes match
	 *			'method' => 'index', // default controller method, if not specified in the config item
	 *		],
	 *		'routes' => [ // Route mapping
	 *			'get' => [ // GET routes
	 *				'homepage' => [ // route name => params
	 *					'path' => '/',
	 *					'callback' => [ // Array like so, or callable, like a lamda
	 *						'class' => 'HomeController',
	 *						'method' => 'index',
	 *						'args' => [], // Arguments to pass to controller method
	 *					],
	 *				],
	 *			],
	 *			...
	 *		]
	 *	];
	 *
	 * @param array $config - the configuration array
	 * @return RouteConfig
	 */
	public function loadFromArray(array $config)
	{
		if (array_key_exists('routes', $config) && array_key_exists('base', $config))
		{
			$this->setBaseConfig(
				$config['base']['namespace'],
				$config['base']['class'],
				$config['base']['method']
			);
			
			$routes = $config['routes'];
		}
		else
		{
			$routes = $config;
		}
		
		$this->setRouteConfig($routes);
		
		return $this;
	}
	
	/**
	 * Helper method to add a route to the internal map
	 *
	 * @param string $verb - The http verb of the route
	 * @param string $name - The name of the route
	 * @param string $path - The path or pattern to match
	 * @param array|callable $callback - The callback to dispatch on match
	 * @return RouteConfig
	 */
	protected function addRouteToMap($verb, $name, $path, $callback, array $options = [])
	{
		$route = [
			'path' => $path,
			'callback' => $callback,
		];
		
		if ( ! empty($options))
		{
			$route['options'] = $options;
		}
		
		$this->map['routes'][$verb][$name] = $route;
	}
}