Comments (10)
With all my testing, these situations should be fine, however, I will look in to it and ensure they work before the release of 2.0.0
—
Sent from Mailbox
On Sat, Sep 19, 2015 at 5:52 PM, Bernardo [email protected]
wrote:
May I suggest the removal of the callable type hints in the development branch?
When I provide[ SomeClass::class, 'methodName' ]
as a handler (implying that SomeClass be resolved from the DI container), multiple warnings are raised saying that methodName should not be invoked as a static method (because it isn't static). I can work around the warnings by prepending the@
to the map statement, but I don't think that's a decent solution.Worse even, when I provide
'SomeClass::methodName'
as handler (which should be legal), a fatal error is raised.Reply to this email directly or view it on GitHub:
#71
from route.
Is everything left as default for strategies etc? Trying to determine whether the warnings/errors are being triggered in Route or Container as there is a unit test for each of those callable types for the RequestResponseStrategy
from route.
What if... you'd pass some kind of object that is a callable.
class ContainerCallable
{
private $container;
public function __construct(ImmutableContainerInterface $container)
{
$this->container = $container;
}
public function withInvokable($invokable)
{
$that = clone($this);
$that->invokable = $invokable;
return $that;
}
public function __invoke()
{
return $this->container->call($this->invokable);
}
}
$callable = new ContainerCallable($container);
$router->get('/foo', $callable->withInvokable([SomeClass::class, 'methodName']));
Just exploring the use of decoration here.
from route.
Well, is_callable
should actually return true
on those types of callables even if they technically are not, running tests with E_ALL
and they all pass. This is why I'm wondering if it's the ParamStrategy
and it's failing in the container from some sort of oversight on my part?
I'll investigate more this evening. But these tests https://github.com/thephpleague/route/blob/develop/tests/Strategy/RequestResponseStrategyTest.php cover them, and pass on all supported versions. https://travis-ci.org/thephpleague/route/builds/79831704.
from route.
For a quick and dirty example.
<?php
class Something
{
public function action()
{
return 'Hello, World!';
}
}
$callable1 = [Something::class, 'action'];
$callable2 = 'Something::action';
var_dump(is_callable($callable1)); // true
var_dump(is_callable($callable2)); // true
function test(callable $callable) {
return is_callable($callable);
}
var_dump(test($callable1)); // true
var_dump(test($callable2)); // true
Valid across PHP5.5.0 to PHP7.0. Would also be valid in PHP5.4 without the ::class
constant.
from route.
Oh wait, different results when I click the results link. My bad on strict standards, callable
type hint !== is_callable
. I'll fix this.
from route.
Thanks, that's exactly what I meant.
You might also want to consider the third test I've added:
This simulates a scenario where non-class-name names are used in a DI container.
Removing the callable
type hint should be sufficient to fix that, but you might want to incorporate it in to the unit tests.
from route.
As the callable
type hint and is_callable
are not the same (baffled as to why) I'll be removing the type hint throughout the tree and swapping for an is_callable
check until the point where a solid callable is built within the dispatcher. That should cover all bases.
Thanks for picking up on this, it's annoying as my environment has been ignoring E_ALL
for some reason that I can't figure out.
from route.
Great!
Would it also be possible to remove the type hint from the StrategyInterface
?
It would allow $controller
to be null
. This could be useful for dynamic routing strategies, where the controller is determined by the dispatcher based on other information from $this->request
or $vars
.
I can work around this by using a dummy callable, but it's not the most elegant solution.
from route.
I'd prefer it to stay in as by that point a callable has been built and it would require more checks in the default strategies to ensure it can be invoked if it is not there.
from route.
Related Issues (20)
- Feature request: custom attributes for route and route group accessible within a middleware HOT 6
- Catch Exception Best Way HOT 3
- Feature request: map with array of methods HOT 1
- Feature request: make $request available in getOptionsCallable HOT 2
- I cannot make post requests HOT 5
- Url resolve fails with 2 numeric parameter HOT 3
- Add an option to get the target route from the middleware HOT 2
- Why isn't the middleware stack executed for unmatched routes? HOT 2
- psr/simple-cache version constraints HOT 2
- Allow for empty route paths to be treat as the base route path HOT 2
- Nested groups are required HOT 1
- Feature request: Treat Route `$handler` that implements RequestHandlerInterface as callable
- Root exception class (and possibly, its children) not compatible with PHP7+
- Request matcher? guess if ServerRequest can be handled
- Dispatcher: handle and find HOT 1
- Feature: Extra map() options
- Method to get declared routes?
- How can I assign controller to errors http ? HOT 1
- Cannot Change Cache Key
- Is this package still actively maintained? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from route.