diff --git a/src/functions.php b/src/functions.php index f578e65..9c03bf5 100644 --- a/src/functions.php +++ b/src/functions.php @@ -2,63 +2,70 @@ namespace FastRoute; -/** - * @param callable $routeDefinitionCallback - * @param array $options - * - * @return Dispatcher - */ -function simpleDispatcher(callable $routeDefinitionCallback, array $options = []) { - $options += [ - 'routeParser' => 'FastRoute\\RouteParser\\Std', - 'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased', - 'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased', - ]; +if ( ! function_exists('\FastRoute\simpleDispatcher') ) { + /** + * @param callable $routeDefinitionCallback + * @param array $options + * + * @return Dispatcher + */ + function simpleDispatcher( callable $routeDefinitionCallback, array $options = [ ] ) + { + $options += [ + 'routeParser' => 'FastRoute\\RouteParser\\Std', + 'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased', + 'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased', + ]; - $routeCollector = new RouteCollector( - new $options['routeParser'], new $options['dataGenerator'] - ); - $routeDefinitionCallback($routeCollector); + $routeCollector = new RouteCollector( + new $options[ 'routeParser' ], new $options[ 'dataGenerator' ] + ); + $routeDefinitionCallback( $routeCollector ); - return new $options['dispatcher']($routeCollector->getData()); + return new $options[ 'dispatcher' ]( $routeCollector->getData() ); + } } -/** - * @param callable $routeDefinitionCallback - * @param array $options - * - * @return Dispatcher - */ -function cachedDispatcher(callable $routeDefinitionCallback, array $options = []) { - $options += [ - 'routeParser' => 'FastRoute\\RouteParser\\Std', - 'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased', - 'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased', - 'cacheDisabled' => false, - ]; +if ( ! function_exists('\FastRoute\cachedDispatcher') ) { + /** + * @param callable $routeDefinitionCallback + * @param array $options + * + * @return Dispatcher + */ + function cachedDispatcher( callable $routeDefinitionCallback, array $options = [ ] ) + { + $options += [ + 'routeParser' => 'FastRoute\\RouteParser\\Std', + 'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased', + 'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased', + 'cacheDisabled' => false, + ]; - if (!isset($options['cacheFile'])) { - throw new \LogicException('Must specify "cacheFile" option'); - } + if (!isset( $options[ 'cacheFile' ] )) { + throw new \LogicException( 'Must specify "cacheFile" option' ); + } - if (!$options['cacheDisabled'] && file_exists($options['cacheFile'])) { - $dispatchData = require $options['cacheFile']; - if (!is_array($dispatchData)) { - throw new \RuntimeException('Invalid cache file "' . $options['cacheFile'] . '"'); + if (!$options[ 'cacheDisabled' ] && file_exists( $options[ 'cacheFile' ] )) { + $dispatchData = require $options[ 'cacheFile' ]; + if (!is_array( $dispatchData )) { + throw new \RuntimeException( 'Invalid cache file "' . $options[ 'cacheFile' ] . '"' ); + } + + return new $options[ 'dispatcher' ]( $dispatchData ); } - return new $options['dispatcher']($dispatchData); - } - $routeCollector = new RouteCollector( - new $options['routeParser'], new $options['dataGenerator'] - ); - $routeDefinitionCallback($routeCollector); + $routeCollector = new RouteCollector( + new $options[ 'routeParser' ], new $options[ 'dataGenerator' ] + ); + $routeDefinitionCallback( $routeCollector ); - $dispatchData = $routeCollector->getData(); - file_put_contents( - $options['cacheFile'], - 'getData(); + file_put_contents( + $options[ 'cacheFile' ], + '