hyperf源码分析

本分析基于hyperf2.0

具体详情参考  https://hyperf.wiki/2.0/#/

# 1 下载image
docker pull hyperf/hyperf:latest
# 加载image成为容器
docker run -it -v D:\wwwroot\code\demo:/hyperf-skeleton -p 2804:9501 --name=hyperfdemo2804 hyperf/hyperf:latest
composer安装
# 镜像容器运行后,在容器内安装 Composer
wget https://mirrors.aliyun.com/composer/composer.phar
chmod u+x composer.phar
mv composer.phar /usr/local/bin/composer

# 通过 Composer 安装 hyperf/hyperf-skeleton 项目
composer create-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);

require BASE_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 = require BASE_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 = new Container((new DefinitionSourceFactory(true))());

这里是实例化ioc容器,其实是去加载了composer.lock里面的内容,

进入  Hyperf\Di\Definition\DefinitionSourceFactory.php

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace Hyperf\Di\Definition;

use Hyperf\Config\ProviderConfig;
use Hyperf\Di\Exception\Exception;

class DefinitionSourceFactory
{
    /**
     * @var bool
     */
    protected $enableCache = false;

    /**
     * @var string
     */
    protected $baseUri;

    public function __construct(bool $enableCache = false)
    {
        $this->enableCache = $enableCache;

        if (! defined('BASE_PATH')) {
            throw new Exception('BASE_PATH is not defined.');
        }

        $this->baseUri = BASE_PATH;
    }

    public function __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 ?? []);
        }

        return new DefinitionSource($serverDependencies);
    }
}

__invoke() 方法也是魔术方法,会被同时调用, __invoke 方法定位到了/config 目录,

可以看到加载完composer.lock之后经过处理的 load() 返回值,处理完container.php,返回值回到hyperf.php 是一个对象,

包含了初始化好的Di和其他需要的工厂类,例如需要用到的mysql扩展类

然后会判断是否 \Psr\Container\ContainerInterface

可以在下面的完整参数里面看到第一个就是它,如果初始化后没有这个会报错

并且取用 PSR Container 加载

 

接下来是常用的

php bin/hyperf.php start

这里的start也就是启动项目

在 Hyperf/Server\Command 的 StartServer 定义了start

public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        parent::__construct('start');
        $this->setDescription('Start hyperf servers.');
    }

然后就是start真正执行的代码

protected function execute(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) {
            throw new InvalidArgumentException('At least one server should be defined.');
        }

        $serverFactory->configure($serverConfig);

        Runtime::enableCoroutine(true, swoole_hook_flags());

        $serverFactory->start();

        return 0;
    }

这里会检查运行环境,同时至少需要开启一个server

 

菜鸟一枚,欢迎吐槽和大佬莅临指导

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值