728x90
.env
위치: /.env
.env 파일은 공개해서는 안되는 private 한 정보를 정의해 둔다.
그러므로 gitignore 에 등록하여 git 에 업로드되지 않도록 유심히 신경써야하고 .env 대신 config에서 값을 찾아 사용하는 것 장려한다.
.env.example
.env.example 파일은 깃에 업로드가 되며, .env에 어떠한 정보가 필요한지 기입해둔다.
(해당 패키지를 사용할 다른 사용자가 .env.example 파일을 보고 필요한 값을 관리자에게 요청)
주의: .env파일에는 ;를 사용하면 안됌
config
위치: /config/grip-config.php
다른 파일에서 config를 통해 .env 파일에 있는 데이터를 사용 가능하도록 정의한다.
주의: ServiceProvider 에 config 파일 정의 필요
ServiceProvider
위치: /src/GripServiceProvider.php
1) register() 메서드 내에, config 파일의 경로($configPath)를 설정값('grip-config')으로 참조 선언
2) boot() 메서드 내에, 패키지의 config 파일을 프로젝트의 config 디렉토리에 게시
- register(): 주로 서비스 등록에 사용되며, 다른 것들에 의존하지 않는 기본적인 설정을 제공합니다.
- boot(): 서비스 등록 후에 실행되며, 애플리케이션의 다른 부분에 의존하는 복잡한 초기화나 설정 작업에 사용됩니다.
<?php
namespace -\GripSellerClient;
use Illuminate\Support\ServiceProvider;
class GripServiceProvider extends ServiceProvider
{
public function register(): void
{
$configPath = __DIR__ . '/../config/grip-config.php';
$this->mergeConfigFrom($configPath, 'grip-config');
}
public function boot(): void
{
#publishes(): 패키지의 리소스를 프로젝트의 특정 위치에 게시할 때 사용
#publishes(패키지의 grip-config.php 파일 => 프로젝트의 config 디렉토리에 'grip-config.php' 로 게시)
$this->publishes(
[__DIR__.'/../config/grip-config.php' => $this->app->configPath('grip-config.php')]
);
}
}
TestCase
위치: /tests/TestCase.php
Test 환경에서 config 파일 내용 필요시 직접 설정
<?php
namespace Tests;
use -\GripSellerClient\GripServiceProvider;
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
class TestCase extends \Orchestra\Testbench\TestCase
{
protected function getPackageProviders($app): array //init
{
return [
GripServiceProvider::class
];
}
#getEnvironmentSetUp(): 주로 테스트 환경에서 사용될 설정을 직접 지정
protected function getEnvironmentSetUp($app): void
{
$app->useEnvironmentPath(__DIR__ . '/..');
$app->bootstrapWith([LoadEnvironmentVariables::class]);
parent::getEnvironmentSetUp($app);
#서비스 컨테이너 config (라라벨 설정 관리자)의 grip-config 라는 설정 키에 대한 값을 정의
$app['config']->set(
'grip-config',
[
#GRIP_SERVICE_ID 환경 변수의 값이 있으면 그 값을 사용하고, 그렇지 않으면 빈 문자열('')을 기본값으로 사용
'service_id' => env('GRIP_SERVICE_ID', ''),
'access_key' => env('GRIP_ACCESS_KEY', ''),
'secret_key' => env('GRIP_SECRET_KEY', ''),
'environment' => env('GRIP_ENVIRONMENT', 'development'),
]
);
}
}
Base
위치: /src/Common/Base.php
코드 내 사용 방법: config('grip-config.key');
<?php
namespace -\GripSellerClient\Common;
use GuzzleHttp\Client;
use JsonMapper;
class Base
{
protected string $serviceId;
protected string $accessKey;
protected string $secretKey;
protected float $timestamp;
protected string $url = "";
public function __construct(
protected Client $client,
protected JsonMapper $jsonMapper,
)
{
if(config('grip-config.environment') === 'development') {
$this->url = 'https://seller.grip.show';
} elseif(config('grip-config.environment') === 'production') {
$this->url = 'https://seller.grip.show';
}
$this->serviceId = config('grip-config.service_id');
$this->accessKey = config('grip-config.access_key');
$this->secretKey = config('grip-config.secret_key');
$this->timestamp = floor(microtime(true) * 1000);
}
public function getSignature(string $subUrl): string #'/api/product/Query' (uri는 QueryString을 포함)
{
$hashString = "GET {$subUrl}\n{$this->timestamp}\n{$this->accessKey}";
return base64_encode(hash_hmac('sha256', $hashString, $this->secretKey, true));
}
}
'Project > Grip Api Package' 카테고리의 다른 글
*참고 패키지 정리 (0) | 2023.09.30 |
---|---|
자동 의존성 주입(automatic dependency injection) (0) | 2023.09.30 |
Java Date <-> Laravel Date (0) | 2023.09.25 |
Json encode, Json decode (0) | 2023.09.21 |
[2] 패키지 테스팅 정리 (0) | 2023.09.17 |