我想我可以与你们分享我所做的事情。我注意到 Slim\Slim 中的每个路由方法有时都称为方法 mapRoute
(为了清楚起见,我更改了官方源代码的缩进)
Slim.php
protected function mapRoute($args)
{
$pattern = array_shift($args);
$callable = array_pop($args);
$route = new \Slim\Route(
$pattern,
$callable,
$this->settings['routes.case_sensitive']
);
$this->router->map($route);
if (count($args) > 0) {
$route->setMiddleware($args);
}
return $route;
}
反过来,Slim\Route 构造函数称为 setCallable
Route.php
public function setCallable($callable)
{
$matches = [];
$app = $this->app;
if (
is_string($callable) &&
preg_match(
'!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!',
$callable,
$matches
)
) {
$class = $matches[1];
$method = $matches[2];
$callable = function () use ($class, $method) {
static $obj = null;
if ($obj === null) {
$obj = new $class;
}
return call_user_func_array([$obj, $method], func_get_args());
};
}
if (!is_callable($callable)) {
throw new \InvalidArgumentException('Route callable must be callable');
}
$this->callable = $callable;
}
基本上是这样的
- 如果
$callable 是一个字符串并且(注意单冒号)格式为ClassName:method 那么它是非静态的,所以 Slim 将实例化类,然后调用它的方法。
- 如果不可调用,则抛出异常(足够合理)
- 否则,无论它是什么(ClassName::staticMethod、闭包、函数名),都将按原样使用。
ClassName应该是FQCN,所以更像\MyProject\Controllers\ClassName。
控制器(或其他)实例化的点是注入 App 实例的好机会。所以,对于初学者,我overrode mapRoute 将应用实例注入到它:
\Util\MySlim
protected function mapRoute($args)
{
$pattern = array_shift($args);
$callable = array_pop($args);
$route = new \Util\MyRoute(
$this, // <-- now my routes have a reference to the App
$pattern,
$callable,
$this->settings['routes.case_sensitive']
);
$this->router->map($route);
if (count($args) > 0) {
$route->setMiddleware($args);
}
return $route;
}
所以基本上\Util\MyRoute 是\Slim\Route 在它的构造函数中有一个额外的参数,我存储为$this->app
此时,getCallable 可以将应用注入到每个需要实例化的控制器中
\Util\MyRoute.php
public function setCallable($callable)
{
$matches = [];
$app = $this->app;
if (
is_string($callable) &&
preg_match(
'!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!',
$callable,
$matches
)
) {
$class = $matches[1];
$method = $matches[2];
$callable = function () use ($app, $class, $method) {
static $obj = null;
if ($obj === null) {
$obj = new $class($app); // <--- now they have the App too!!
}
return call_user_func_array([$obj, $method], func_get_args());
};
}
if (!is_callable($callable)) {
throw new \InvalidArgumentException('Route callable must be callable');
}
$this->callable = $callable;
}
原来如此。使用这两个类,我可以将$app 注入到我在路由上声明的任何控制器中,只要我使用单个冒号将控制器与方法分开即可。使用 paamayim nekudotayim 会将该方法调用为静态方法,因此如果我尝试访问其中的 $this->app 会引发错误。
我使用blackfire.io 进行了测试,并且...性能提升可以忽略不计。
优点:
- 这让我免去了在每个静态方法调用上调用 $app = \Slim\Slim::getInstance() 的痛苦,总共大约 100 行文本。
- 它为进一步优化开辟了道路,让每个控制器都继承自一个抽象控制器类,而抽象控制器类又将应用程序方法包装成便利方法。
- 它让我更好地理解了 Slim 的请求和响应生命周期。
缺点:
- 性能提升可以忽略不计
- 您必须将所有路由转换为使用单个冒号而不是 paamayin,并将所有控制器方法从静态转换为动态。
- 在推出 v 3.0.0 时,从 Slim 基类的继承可能会中断
结语:(4 年后)
在 Slim v3 中,他们删除了静态访问器。反过来,如果您使用相同的约定FQCN\ClassName:method,控制器将使用应用程序的容器进行实例化。此外,该方法从路由接收请求、响应和$args。这样的DI,很多IoC。我很喜欢。
回顾我对 Slim 2 的做法,它打破了替换掉线的最基本原则(Liskov Substitution)。
class Route extends \Slim\Route
{
protected $app;
public function __construct($app, $pattern, $callable, $caseSensitive = true) {
...
}
}
应该是的
class Route extends \Slim\Route
{
protected $app;
public function __construct($pattern, $callable, $caseSensitive = true, $app = null) {
...
}
}
所以它不会违反合同并且可以透明地使用。