hyperf源码分析
本分析基于hyperf2.0
具体详情参考https://hyperf.wiki/2.0/#/
# 1 下载image
dockerpull hyperf/hyperf:latest
# 加载image成为容器
dockerrun -it -v D:\wwwroot\code\demo:/hyperf-skeleton -p 2804:9501 --name=hyperfdemo2804 hyperf/hyperf:latest
composer安装
# 镜像容器运行后,在容器内安装 Composer
wgethttps://mirrors.aliyun.com/composer/composer.phar
chmodu+x composer.phar
mvcomposer.phar /usr/local/bin/composer
# 通过 Composer 安装 hyperf/hyperf-skeleton 项目
composercreate-project hyperf/hyperf-skeleton
# 基本上全部回车就可以了
查看hyperf.php文件
#!/usr/bin/env php
<?php
ini_set('display_errors','on');
ini_set('display_startup_errors','on');
error_reporting(E_ALL);
! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__,1));
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);
requireBASE_PATH .'/vendor/autoload.php';
// Self-called anonymous function that creates its own scope and keep the global namespace clean.
(function(){
Hyperf\Di\ClassLoader::init();
/**@var\Psr\Container\ContainerInterface $container */
$container =requireBASE_PATH .'/config/container.php';
$application = $container->get(\Hyperf\Contract\ApplicationInterface::class);
$application->run();
})();
PHP ini 设置, 按需设置即可, 比如这里还可以设置时区
常量 BASE_PATH, hyperf 只设置了这个一个常量, 用来所有 路径 相关的场景
config/container.php, container 的初始化, 重中之重的内容
Application->run(), 完整的是 Symfony\Component\Console\Application, 用来跑 cli 应用
用了一个闭包的写法,避免外部变量影响内部,主要是实例化一个ioc容器,创建控制台应用程序
重点 重点 重点 (重要的事情说三遍) hyperf所采用的Di容器的概念也就是这里
$container =newContainer((newDefinitionSourceFactory(true))());
这里是实例化ioc容器,其实是去加载了composer.lock里面的内容,
进入 Hyperf\Di\Definition\DefinitionSourceFactory.php
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
*@linkhttps://www.hyperf.io
*@documenthttps://hyperf.wiki
*@contactgroup@hyperf.io
*@licensehttps://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespaceHyperf\Di\Definition;
useHyperf\Config\ProviderConfig;
useHyperf\Di\Exception\Exception;
classDefinitionSourceFactory
{
/**
*@varbool
*/
protected$enableCache =false;
/**
*@varstring
*/
protected$baseUri;
publicfunction__construct(bool$enableCache =false)
{
$this->enableCache = $enableCache;
if(! defined('BASE_PATH')) {
thrownewException('BASE_PATH is not defined.');
}
$this->baseUri = BASE_PATH;
}
publicfunction__invoke()
{
$configDir =$this->baseUri .'/config';
$configFromProviders = [];
if(class_exists(ProviderConfig::class)) {
$configFromProviders = ProviderConfig::load();
}
$serverDependencies = $configFromProviders['dependencies'] ?? [];
if(file_exists($configDir .'/autoload/dependencies.php')) {
$definitions =include$configDir .'/autoload/dependencies.php';
$serverDependencies = array_replace($serverDependencies, $definitions ?? []);
}
returnnewDefinitionSource($serverDependencies);
}
}
__invoke() 方法也是魔术方法,会被同时调用, __invoke 方法定位到了/config 目录,
可以看到加载完composer.lock之后经过处理的 load() 返回值,处理完container.php,返回值回到hyperf.php 是一个对象,
包含了初始化好的Di和其他需要的工厂类,例如需要用到的mysql扩展类
然后会判断是否 \Psr\Container\ContainerInterface
可以在下面的完整参数里面看到第一个就是它,如果初始化后没有这个会报错
并且取用 PSR Container 加载
接下来是常用的
php bin/hyperf.phpstart
这里的start也就是启动项目
在 Hyperf/Server\Command 的 StartServer 定义了start
publicfunction__construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct('start');
$this->setDescription('Start hyperf servers.');
}
然后就是start真正执行的代码
protectedfunctionexecute(InputInterface $input, OutputInterface $output)
{
$this->checkEnvironment($output);
$serverFactory =$this->container->get(ServerFactory::class)
->setEventDispatcher($this->container->get(EventDispatcherInterface::class))
->setLogger($this->container->get(StdoutLoggerInterface::class));
$serverConfig =$this->container->get(ConfigInterface::class)->get('server', []);
if(! $serverConfig) {
thrownewInvalidArgumentException('At least one server should be defined.');
}
$serverFactory->configure($serverConfig);
Runtime::enableCoroutine(true, swoole_hook_flags());
$serverFactory->start();
return0;
}
这里会检查运行环境,同时至少需要开启一个server