Skip to content

Commit

Permalink
Add support for custom_methods key in config
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Jan 23, 2024
1 parent 8095fa3 commit e968424
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
83 changes: 55 additions & 28 deletions src/Pop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
}

Expand Down Expand Up @@ -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
*
Expand Down
6 changes: 5 additions & 1 deletion tests/PopcornTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PopcornTest extends TestCase
public function testConstructor()
{
$app = new Pop([
'custom_methods' => ['PURGE'],
'routes' => [
'*' => [
'/' => function() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit e968424

Please sign in to comment.