From e968424fe34c8dbdfe7ae775eae19b84ec7d2253 Mon Sep 17 00:00:00 2001 From: Nick Sagona Date: Tue, 23 Jan 2024 14:06:46 -0600 Subject: [PATCH] Add support for custom_methods key in config --- src/Pop.php | 83 ++++++++++++++++++++++++++++--------------- tests/PopcornTest.php | 6 +++- 2 files changed, 60 insertions(+), 29 deletions(-) diff --git a/src/Pop.php b/src/Pop.php index 6731bdf..53305bc 100644 --- a/src/Pop.php +++ b/src/Pop.php @@ -59,42 +59,55 @@ public function __construct() $args = func_get_args(); foreach ($args as $i => $arg) { - if (is_array($arg) && isset($arg['routes'])) { - // Check for combined route matches - foreach ($arg['routes'] as $key => $value) { - if ($key == '*') { - foreach ($arg['routes'][$key] as $route => $controller) { - $this->addToAll($route, $controller); - } - unset($arg['routes'][$key]); - } else if (str_contains((string)$key, ',')) { - foreach ($arg['routes'][$key] as $route => $controller) { - $this->setRoutes($key, $route, $controller); - } - unset($arg['routes'][$key]); + if (is_array($arg)) { + // Handle custom methods config + if (isset($arg['custom_methods'])) { + if (is_array($arg['custom_methods'])) { + $this->addCustomMethods($arg['custom_methods']); + } else { + $this->addCustomMethod($arg['custom_methods']); } + unset($args[$i]['custom_methods']); } - // Check for direct route method matches - $routeKeys = array_keys($this->routes); - foreach ($routeKeys as $key) { - if (isset($arg['routes'][$key])) { - foreach ($arg['routes'][$key] as $route => $controller) { - $this->setRoute($key, $route, $controller); + // Handle routes config + if (isset($arg['routes'])) { + // Check for combined route matches + foreach ($arg['routes'] as $key => $value) { + if ($key == '*') { + foreach ($arg['routes'][$key] as $route => $controller) { + $this->addToAll($route, $controller); + } + unset($arg['routes'][$key]); + } else if (str_contains((string)$key, ',')) { + foreach ($arg['routes'][$key] as $route => $controller) { + $this->setRoutes($key, $route, $controller); + } + unset($arg['routes'][$key]); } - unset($arg['routes'][$key]); } - } - // Check for static routes that are not assigned to a method, - // and auto-assign them to get,post for a fallback - if (count($arg['routes']) > 0) { - foreach ($arg['routes'] as $route => $controller) { - $this->setRoutes('get,post', $route, $controller); + // Check for direct route method matches + $routeKeys = array_keys($this->routes); + foreach ($routeKeys as $key) { + if (isset($arg['routes'][$key])) { + foreach ($arg['routes'][$key] as $route => $controller) { + $this->setRoute($key, $route, $controller); + } + unset($arg['routes'][$key]); + } } - } - unset($args[$i]['routes']); + // Check for static routes that are not assigned to a method, + // and auto-assign them to get,post for a fallback + if (count($arg['routes']) > 0) { + foreach ($arg['routes'] as $route => $controller) { + $this->setRoutes('get,post', $route, $controller); + } + } + + unset($args[$i]['routes']); + } } } @@ -264,6 +277,20 @@ public function addCustomMethod(string $customMethod): Pop return $this; } + /** + * Add custom methods + * + * @param array $customMethods + * @return Pop + */ + public function addCustomMethods(array $customMethods): Pop + { + foreach ($customMethods as $customMethod) { + $this->addCustomMethod($customMethod); + } + return $this; + } + /** * Has a custom method * diff --git a/tests/PopcornTest.php b/tests/PopcornTest.php index dd91374..7a6b9bb 100644 --- a/tests/PopcornTest.php +++ b/tests/PopcornTest.php @@ -16,6 +16,7 @@ class PopcornTest extends TestCase public function testConstructor() { $app = new Pop([ + 'custom_methods' => ['PURGE'], 'routes' => [ '*' => [ '/' => function() { @@ -28,11 +29,13 @@ public function testConstructor() $this->assertInstanceOf('Popcorn\Pop', $app); $this->assertTrue($app->hasRoute('get', '/')); $this->assertTrue($app->hasRoute('post', '/')); + $this->assertTrue($app->hasCustomMethod('PURGE')); } public function testConstructorRoutes() { $config = [ + 'custom_methods' => 'PURGE', 'routes' => [ 'get' => [ '/' => function() { @@ -77,6 +80,7 @@ public function testConstructorRoutes() $this->assertTrue($app->hasRoute('post', '/something/else')); $this->assertFalse($app->hasRoute('delete', '/something/else')); $this->assertEquals('bar', $app->config()['foo']); + $this->assertTrue($app->hasCustomMethod('PURGE')); } public function testAddGetRoute() @@ -275,7 +279,7 @@ public function testCustomMethod() $_SERVER['REQUEST_METHOD'] = 'PURGE'; $_SERVER['REQUEST_URI'] = 'image'; $app = new Pop(); - $app->addCustomMethod('PURGE'); + $app->addCustomMethods(['PURGE']); $app->purge('image', [ 'controller' => function(){ echo 'image purged';