原本想看laravel的源码,结果看到了pimple就脑路乱了,看不下去了,先拿slim练练手。slim源码地址 https://github.com/slimphp/Slim
这里发生了一个小插曲,本地部署代码之后,发现有命名空间的报错,于是给提了个pull request
image.png
接下来看slim的主要目录结构
image.png
我们主要看Slim目录 example(使用例子),tests(单元测试功能测试),vendor(依赖包)这些这里先不看
主要的文件是Slim文件夹下的App.php。其实看到Slim文件夹下有这些文件不用怕,看一下他们的继承机构,大多最多只是继承了对应的接口。只要按App.php文件一步步往下屡就可以了。接下来就有必要贴一下App.php的代码了……算了,太多了,一段一段的讲吧(前面自动加载和怎么到App.php逻辑就不说了,不会的小伙伴自己先补补)。
namespace Slim;
use Exception;
use Psr\Http\Message\UriInterface;
use Slim\Exception\InvalidMethodException;
use Throwable;
use Closure;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Container\ContainerInterface;
use FastRoute\Dispatcher;
use Slim\Exception\SlimException;
use Slim\Exception\MethodNotAllowedException;
use Slim\Exception\NotFoundException;
use Slim\Http\Uri;
use Slim\Http\Headers;
use Slim\Http\Body;
use Slim\Http\Request;
use Slim\Interfaces\RouteGroupInterface;
use Slim\Interfaces\RouteInterface;
use Slim\Interfaces\RouterInterface;
从引入的命名空间可以看出。slim文件夹下的引用的差不多了
接下来看一下 App的构造方法
/**
* Create new application
*
* @param ContainerInterface|array $container Either a ContainerInterface or an associative array of app settings
* @throws InvalidArgumentException when no container is provided that implements ContainerInterface
*/
public function __construct($container = [])
{
if (is_array($container)) {
$container = new Container($container);
}
if (!$container instanceof ContainerInterface) {
throw new InvalidArgumentException('Expected a ContainerInterface');
}
$this->container = $container;
}
构造方法中就干了一件事,就是创建container容器,那就继续去看Container这个类(框架代码里注释都很全,这点很赞)。
这里我就不贴Contianer 类的代码了,这个类继承PimpleContainer。就是我文章最开头提到的东西。这个是PHP的一个轻量化的注册容器。下面是Contianer 类的构造方法。
public function __construct(array $values = [])
{
parent::__construct($values);
$userSettings = isset($values['settings']) ? $values['settings'] : [];
$this->registerDefaultServices($userSettings);
}
主要的是 registerDefaultServices 方法,从名字上也能看出意思,是注册服务,下面看registerDefaultServices的源码
private function registerDefaultServices($userSettings)
{
$defaultSettings = $this->defaultSettings;
/**
* This service MUST return an array or an
* instance of \ArrayAccess.
*
* @return array|\ArrayAccess
*/
$this['settings'] = function () use ($userSettings, $defaultSettings) {
return new Collection(array_merge($defaultSettings, $userSettings));
};
$defaultProvider = new DefaultServicesProvider();
$defaultProvider->register($this);
}
主要是注册了一些基本且重要的服务,这里主要看 DefaultServicesProvider(),这个是slim默认的服务提供者
/**
* Register Slim's default services.
*
* @param Container $container A DI container implementing ArrayAccess and container-interop.
*/
public function register($container)
{
if (!isset($container['environment'])) {
/**
* This service MUST return a shared instance
* of \Slim\Interfaces\Http\EnvironmentInterface.
*
* @return EnvironmentInterface
*/
$container['environment'] = function () {
return new Environment($_SERVER);
};
}
看register 函数,参数是$container 即DI 容器。register 函数中用匿名函数将许多容器中的服务实例实现











网友评论