PHP code example of hrb981027 / treasure-bag

1. Go to this page and download the library: Download hrb981027/treasure-bag library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hrb981027 / treasure-bag example snippets




declare(strict_types=1);

return [
    'service_center_host' => env('TREASURE_BAG_SERVICE_CENTER_HOST', 'http://localhost'),
    'service' => ['name' => '', 'description' => '', 'path' => [], 'publish' => [['topic' => '', 'description' => '']]],
    'service_hostname' => env('TREASURE_BAG_SERVICE_HOSTNAME', 'localhost'),
    'service_port' => (int)env('TREASURE_BAG_SERVICE_PORT', 9501),
];



declare(strict_types=1);

return [
    'service_center_host' => env('TREASURE_BAG_SERVICE_CENTER_HOST', 'http://localhost'),
    'service' => \Hrb981027\TreasureBag\Service\PresetService\PodsService::MEMBER_SERVICE,
    'service_hostname' => env('TREASURE_BAG_SERVICE_HOSTNAME', 'localhost'),
    'service_port' => env('TREASURE_BAG_SERVICE_PORT', 9501),
];



declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hrb981027\TreasureBag\Annotation\Secure;

/**
 * @Secure()
 * @Controller()
 */
class ApiController extends AbstractController
{
    /**
     * @RequestMapping(path="test", methods={"GET"})
     */
    public function test()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}



declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hrb981027\TreasureBag\Annotation\Secure;

/**
 * @Controller()
 */
class ApiController extends AbstractController
{
    /**
     * @Secure()
     * @RequestMapping(path="test", methods={"GET"})
     */
    public function test()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}

return [
    ...
    'service' => [
        ...
        'path' => [
            '/test1', '/test2'
        ]
        ...
    ],
    ...
];

/**
 * @Secure(path="/test2")
 */



declare(strict_types=1);

namespace App\Listener;

use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\OnStart;
use Hrb981027\TreasureBag\Service\CenterClient;

/**
 * @Listener()
 */
class OnStartListener implements ListenerInterface
{
    public function listen(): array
    {
        return [
            OnStart::class,
        ];
    }

    public function process(object $event)
    {
        if ($event instanceof OnStart) {
           make(CenterClient::class)->register();
        }
    }
}



declare(strict_types=1);

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hrb981027\TreasureBag\Service\PresetService\PodsService;
use Hrb981027\TreasureBag\Service\RouterClient;

/**
 * @Controller()
 */
class ApiController extends AbstractController
{
    /**
     * @Inject()
     * @var RouterClient
     */
    protected RouterClient $routerClient;

    /**
     * @RequestMapping(path="test", methods={"GET"})
     */
    public function test()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        $this->routerClient->get(PodsService::MEMBER_SERVICE, '/hello-world', [
            'query' => [
                'hello' => 'world'
            ]
        ]);

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}



declare(strict_types=1);

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hrb981027\TreasureBag\PresetEvent\PodsEvent;
use Hrb981027\TreasureBag\Service\EventClient;

/**
 * @Controller()
 */
class ApiController extends AbstractController
{
    /**
     * @Inject()
     * @var EventClient
     */
    protected EventClient $eventClient;

    /**
     * @RequestMapping(path="test", methods={"GET"})
     */
    public function test()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        $this->eventClient->publish(PodsEvent::MEMBER_CREATED, [
            'hello' => 'world'
        ]);

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}



declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hrb981027\TreasureBag\Annotation\Subscribe;
use Hrb981027\TreasureBag\PresetEvent\PodsEvent;

/**
 * @Controller()
 */
class ApiController extends AbstractController
{
    /**
     * @Subscribe(topic={PodsEvent::MEMBER_CREATED})
     * @RequestMapping(path="test", methods={"POST"})
     */
    public function test()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}



declare(strict_types=1);

namespace App\Controller;

use Hrb981027\TreasureBag\Lib\Enum\ResponseCode;
use Hrb981027\TreasureBag\Lib\ResponseContent\StandardResponseContent;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * @Controller()
 */
class ApiController extends AbstractController
{
    /**
     * @RequestMapping(path="test", methods={"GET"})
     */
    public function test()
    {
        $standardResponseContent = new StandardResponseContent();

        $standardResponseContent->setCode(ResponseCode::SUCCESS);
        $standardResponseContent->setMessage('成功');
        $standardResponseContent->setData([
            'hello' => 'world'
        ]);

        return $this->response->json($standardResponseContent->toArray());
    }
}



declare(strict_types=1);

return [
    'handler' => [
        'http' => [
            \Hrb981027\TreasureBag\Exception\Handler\StandardExceptionHandler::class,
            Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
            App\Exception\Handler\AppExceptionHandler::class,
        ],
    ],
];



declare(strict_types=1);

namespace App\Param\Api\Test;

use Hrb981027\TreasureBag\Annotation\Param;
use Hrb981027\TreasureBag\Annotation\ParamProperty;
use Hrb981027\TreasureBag\Lib\Param\AbstractParam;

/**
 * @Param()
 */
class Data extends AbstractParam
{
    /**
     * @ParamProperty()
     */
    public int $data1;

    /**
     * @ParamProperty()
     */
    public string $data2;
}



declare(strict_types=1);

namespace App\Controller;

use App\Param\Api\Test\Data as TestData;
use Hrb981027\TreasureBag\Lib\Enum\ResponseCode;
use Hrb981027\TreasureBag\Lib\ResponseContent\StandardResponseContent;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * @Controller()
 */
class ApiController extends AbstractController
{
    /**
     * @RequestMapping(path="test", methods={"POST"})
     */
    public function test(): ResponseInterface
    {
        $data = new TestData([
            'data1' => 1,
            'data2' => "1"
        ]);

        $standardResponseContent = new StandardResponseContent();

        $standardResponseContent->setCode(ResponseCode::SUCCESS);
        $standardResponseContent->setMessage('成功');
        $standardResponseContent->setData($data->toArray());

        return $this->response->json($standardResponseContent->toArray());
    }
}



declare(strict_types=1);

namespace App\Param\Api\Test;

use Hrb981027\TreasureBag\Annotation\Param;
use Hrb981027\TreasureBag\Annotation\ParamProperty;
use Hrb981027\TreasureBag\Lib\Param\AbstractParam;

/**
 * @Param()
 */
class Data extends AbstractParam
{
    /**
     * @ParamProperty()
     * @var int[]
     */
    public array $data1;

    /**
     * @ParamProperty()
     */
    public string $data2;
}



declare(strict_types=1);

namespace App\Param\Api\Test;

use Hrb981027\TreasureBag\Annotation\Param;
use Hrb981027\TreasureBag\Annotation\ParamProperty;
use Hrb981027\TreasureBag\Lib\Param\AbstractParam;

/**
 * @Param(inHandle="camelize")
 */
class Data extends AbstractParam
{
    /**
     * @ParamProperty()
     */
    public array $data1;

    /**
     * @ParamProperty()
     */
    public string $data2;
}



declare(strict_types=1);

namespace App\Param\Api\Test;

use Hrb981027\TreasureBag\Annotation\Param;
use Hrb981027\TreasureBag\Annotation\ParamProperty;
use Hrb981027\TreasureBag\Lib\Param\AbstractParam;

/**
 * @Param()
 */
class Data extends AbstractParam
{
    /**
     * @ParamProperty(in="test1", out="test2")
     */
    public int $data1;

    /**
     * @ParamProperty()
     */
    public string $data2;
}



declare(strict_types=1);

namespace App\Controller;

use App\Param\Api\Test\Data as TestData;
use Hrb981027\TreasureBag\Lib\Enum\ResponseCode;
use Hrb981027\TreasureBag\Lib\ResponseContent\StandardResponseContent;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * @Controller()
 */
class ApiController extends AbstractController
{
    /**
     * @RequestMapping(path="test", methods={"POST"})
     */
    public function test(): ResponseInterface
    {
        $data = new TestData([
            'test1' => 1,
            'data2' => "1"
        ]);

        $standardResponseContent = new StandardResponseContent();

        $standardResponseContent->setCode(ResponseCode::SUCCESS);
        $standardResponseContent->setMessage('成功');
        $standardResponseContent->setData($data->toArray());

        return $this->response->json($standardResponseContent->toArray());
    }
}



declare(strict_types=1);

namespace App\Param\Api\Test;

use Hrb981027\TreasureBag\Annotation\Param;
use Hrb981027\TreasureBag\Annotation\ParamProperty;
use Hrb981027\TreasureBag\Lib\Param\AbstractParam;

/**
 * @Param()
 */
class Data extends AbstractParam
{
    /**
     * @ParamProperty()
     */
    public array $data1;

    /**
     * @ParamProperty(



declare(strict_types=1);

namespace App\Param\Api\Test;

use App\Param\User\Add\Def;
use Hrb981027\TreasureBag\Annotation\Param;
use Hrb981027\TreasureBag\Annotation\ParamProperty;
use Hrb981027\TreasureBag\Lib\Param\AbstractParam;

/**
 * @Param()
 */
class Data extends AbstractParam
{
    /**
     * @ParamProperty()
     * @var \App\Param\User\Add\Def[]
     */
    public array $data1;

    /**
     * @ParamProperty()
     */
    public Def $data2;
}



declare(strict_types=1);

namespace App\Param\User\Add;

use Hrb981027\TreasureBag\Annotation\Param;
use Hrb981027\TreasureBag\Annotation\ParamProperty;
use Hrb981027\TreasureBag\Lib\Param\AbstractParam;

/**
 * @Param()
 */
class Def extends AbstractParam
{
    /**
     * @ParamProperty()
     */
    public float $def1;
}



declare(strict_types=1);

namespace App\Controller;

use App\Param\Api\Test\Data as TestData;
use Hrb981027\TreasureBag\Lib\Enum\ResponseCode;
use Hrb981027\TreasureBag\Lib\ResponseContent\StandardResponseContent;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * @Controller()
 */
class ApiController extends AbstractController
{
    /**
     * @RequestMapping(path="test", methods={"POST"})
     */
    public function test(): ResponseInterface
    {
        $data = new TestData([
            'data1' => [
                [
                    'def1' => 2.1
                ],
                [
                    'def1' => 6.6
                ]
            ],
            'data2' => [
                'def1' => 1.5
            ]
        ]);

        $standardResponseContent = new StandardResponseContent();

        $standardResponseContent->setCode(ResponseCode::SUCCESS);
        $standardResponseContent->setMessage('成功');
        $standardResponseContent->setData($data->toArray());

        return $this->response->json($standardResponseContent->toArray());
    }
}



declare(strict_types=1);

namespace App\Param\Api\Test;

use Hrb981027\TreasureBag\Annotation\Param;
use Hrb981027\TreasureBag\Annotation\ParamProperty;
use Hrb981027\TreasureBag\Lib\Param\AbstractParam;

/**
 * @Param()
 */
class Data extends AbstractParam
{
    /**
     * @ParamProperty()
     */
    public array $data1;

    /**
     * @ParamProperty(allowIn=false)
     */
    public int $data2 = 66;

    /**
     * @ParamProperty(allowOut=false)
     */
    public string $data3;
}
shell
php bin/hyperf.php vendor:publish hrb981027/treasure-bag