Skip to content

Updated the router and dispatcher to accept routing to controllers th… #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Lib/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static function dispatch($requestUri, $isCli = false)
}

$result = null;
if (method_exists($ctrl, $action)) {
if (method_exists($ctrl, $action) || is_callable([$ctrl, $action])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it makes sense to just use 'is_callable'? Seems like that covers method_exists.

if ($router->isCallable($ctrl, $action)) {
$result = $ctrl->$action();
} else {
Expand Down
27 changes: 27 additions & 0 deletions src/Lib/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public static function filterURI($uri)
/**
* Uses PHP's ReflectionClass to test the given object for the given method's callability.
* Only public, non-abstract, non-constructor/destructors are considered callable.
* Also supports objects that implement the magic methods __call or __callStatic.
*
* @param Object $obj The object we're searching
* @param string $method The name of the method we're looking for in $obj
Expand All @@ -195,8 +196,32 @@ public static function isCallable($obj, $method, $inheritsFrom = 'Controller')
return false;
}

$reflectedMethod = false;
try {
$reflectedMethod = $reflectedObj->getMethod($method);
} catch (ReflectionException $e) {
// The method does not exist on the object, but could still be available, e.g., via __call
}

// If the method does not exist, but is still callable (e.g. via __call), then only ensure it meets
// the same criteria on inheritance and public uncallables
if ($reflectedMethod === false) {
try {
return (
is_callable([$obj, $method])
&& !in_array(strtolower($method), $publicUncallables)
&& (
$inheritsFrom === null
|| $reflectedObj->getName() == $inheritsFrom
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

===?

|| $reflectedObj->isSubclassOf($inheritsFrom)
)
);
} catch (Exception $e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What Exception instance is possible here? Should probably catch the most specific avalable.

return false;
}
}

try {
$declaredClass = $reflectedMethod->getDeclaringClass();

// A method may be required to inherit from the given class
Expand All @@ -218,6 +243,8 @@ public static function isCallable($obj, $method, $inheritsFrom = 'Controller')
return true;
} catch (ReflectionException $e) {
return false;
} catch (Exception $e) {
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what this catch adds.

}
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Lib/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@ public function testIsCallable()
$this->assertFalse(Router::isCallable($controller, 'preAction'));
$this->assertFalse(Router::isCallable($controller, 'nonexistentMethod'));
$this->assertFalse(Router::isCallable(null, null));

#
# TODO: add tests for __call
# NOTE: phpunit currently cannot handle mocking __call properly
# see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/275
#
/*
$magicController = $this->getMockBuilder('Controller')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not create a test controller that uses magic methods and assert it works as example expected?

->disableOriginalConstructor()
->setMethods(['__call'])
->getMock();

$value = 'testAbc';
$magicController->expects($this->any())
->method('__call')
->willReturnCallback(function($name, $arguments) use ($value) {
return $value;
});

$this->assertTrue(Router::isCallable($magicController, 'index'));
$this->assertTrue(Router::isCallable($magicController, '__construct'));
$this->assertTrue(Router::isCallable($magicController, 'preAction'));
$this->assertTrue(Router::isCallable($magicController, 'nonexistentMethod'));
*
*/
}

/**
Expand Down