Code Monkey home page Code Monkey logo

apollo-client's Introduction

apollo-client


协程apollo php client for laravel

1、安装

composer require yuanlj-tea/apollo-client

2、使用

require_once __DIR__ . '/../vendor/autoload.php';
use ApolloClient\ApolloClient;

$base_url = 'http://xxx';
$appid = 'xxx';
$namespace = ['xxx'];

$cluster = 'default';
$envDir = __DIR__ . '/../env';
$saveDir = __DIR__ . '/../src';

$client = new ApolloClient($base_url, $appid, $namespace);
//方式1:依赖模板文件
$client->setCluster($cluster)    		//设置集群名
  ->setEnvFileName('env-2')         //设置env文件名
  // ->setTplFileName('tpl.php')  	//设置env模板文件名
  ->setEnvTemplateDir($envDir)      //设置env模板文件所在路径
  ->setEnvSavedDir($saveDir);       //设置env文件的存储路径
$client->startLongPull(1);

//方式2:不依赖模板文件,把apollo的配置文件放入env
$envFullPath = __DIR__ . '/../src/.env-3';
$client->setCluster($cluster)       //设置集群名
	->setEnvPath($envFullPath);       //设置env文件存储的完整路径
$client->startLongPull(2);

//方式3:自行传入闭包自定义env生成方式
$callBack = function($data){
  // do sth
};
$client->startLongPull(3,$callBack);

3、方法说明

setEnvFileName($envFileName)//设置要生成的env模板文件名
  
setEnvSavedDir($envSavedDir)//设置env要保存到的文件夹
  
setEnvPath($fullPath)//设置env文件要保存的全路径

setTplFileName($tplFileName)//设置env模板文件名
  
setEnvTemplateDir($envTemplateDir)//设置env模板文件
  
setEnvTplPath($fullPath)//设置要依赖的env模板文件的全路径
    
setCluster($cluster)//设置集群名
    
setClientIp()//设置client ip
    
startLongPull($type = 1, \Closure $callback_param = null)//开始从apollo拉取配置

4、在laravel中使用

4.1、创建配置文件
php artisan vendor:publish --tag=apollo --force(强制替换已有的配置文件)

会在config_path()下生成apollo.php
4.2、在 config/app.php注册 ServiceProvider 和 Facade
'providers' => [
    // ...
    ApolloClient\ApolloClientServiceProvider::class,
],
'aliases' => [
    // ...
    'Apollo' => ApolloClient\ApolloFacade::class,
],
4.3、三种方式获取服务实例

方法参数注入

public function handle(ApolloClient $apollo)
{
		$apollo->doSth();
}

通过服务名获取

public function handle()
{
    app('apollo_client')->setEnvFileName('env.prod')
        ->setEnvSavedDir($saveDir)
        ->startLongPull(2);
}

通过门面类获取

use Apollo;

public function handle()
{
    $envTplDir = app_path().'/env';
    $saveDir = realpath(app_path() . '/../');

  	Apollo::setEnvFileName('env.prod')
    		->setEnvSavedDir($saveDir)
    		->startLongPull(2);
}

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.