定制 metarush/getter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

metarush/getter

最新稳定版本:v3.4.1

Composer 安装命令:

composer require metarush/getter

包简介

Generate a class with getter methods from a yaml or env file

README 文档

README

Generate a class with getter methods from a yaml or env file. This is useful if you want to access variables in yaml/env files as PHP getter methods.

Install

Install via composer as metarush/getter

Sample usage

The following will generate a class named MyNewClass from contents of foo/sample.yaml using yaml adapter. The generated class will be saved in foo/ folder.

via CLI

vendor/metarush/getter/bin/generate -a yaml -c MyNewClass -s tests/unit/samples/sample.yaml -l foo/

via PHP script

(new \MetaRush\Getter\Generator)
    ->setAdapter('yaml')
    ->setClassName('MyNewClass')
    ->setSourceFile('foo/sample.yaml')
    ->setLocation('foo/')
    ->generate();

Sample yaml content

E.g., located in foo/sample.yaml

stringVar: 'foo'
intVar: 9
floatVar: 2.1
boolVar: true
arrayVar:
  - foo
  - 1.3

Sample generated class

E.g., located in foo/MyNewClass.php

<?php

declare(strict_types=1);

namespace MyNamespace;

class MyNewClass
{
    private $stringVar = 'foo';
    private $intVar = 9;
    private $floatVar = 2.1;
    private $boolVar = true;
    private $arrayVar = [0 => 'foo', 1 => 1.3];

    public function getStringVar(): string
    {
        return $this->stringVar;
    }

    public function getIntVar(): int
    {
        return $this->intVar;
    }

    public function getFloatVar(): float
    {
        return $this->floatVar;
    }

    public function getBoolVar(): bool
    {
        return $this->boolVar;
    }

    public function getArrayVar(): array
    {
        return $this->arrayVar;
    }
}

Supported types

  • string
  • int
  • float
  • bool
  • array (only available in yaml adapter)

Adapters

  • yaml
  • env

Advanced usage

Include namespace

Include a namespace, use ->setNamespace('MyNamespace') or via CLI --namespace MyNamespace

Extend class

Extend a class, use ->setExtendedClass('MyBaseClass') or via CLI --extendClass MyBaseClass

Dummify field values

Dummify field values, use the ->setDummifyValues(true) config method or via CLI --dummify

Why dummify?

It's useful for hiding sensitive data such as credentials. Credentials should not be in the source code. The field values of the generated classes with dummy data can later be repopulated (by your custom scripts) on runtime with actual values (see data replacer below). The generated class retains the data types even if the original values have been dummified.

If dummified, the generated field values are as follows:

[
    'x', // string
    1, // int
    1.2, // float
    true, // true
    [0 => 'x'] // array
];

Data replacer for dummified values

Dynamically change the values of the dummified data on runtime, use ->setConstructorType(\MetaRush\Getter\Config::CONSTRUCTOR_DATA_REPLACER); or via CLI --dataReplacer

You can then call the generated class and inject an array of replacement values on runtime:

// $replacementValues should contain key value pair that matches the dummified data

$newClass = new MyNewClass($replacementValues);

Call parent

Call a parent class, use ->setConstructorType(\MetaRush\Getter\Config::CONSTRUCTOR_CALL_PARENT) or via CLI --callParent

Call parent and use data replacer

Call a parent class and also use data replacer, use ->setConstructorType(\MetaRush\Getter\Config::CONSTRUCTOR_BOTH) or via CLI --callParent --dataReplacer

Note: You should only call ->setConstructorType() once

Generate constants

Generate the key/value pairs as constants, use ->setGenerateAsConstants(true) or via CLI --constants.

Generate constants with key as both constant name and value

Use ->setGenerateAsConstants(true) and ->setConstantNameAsValue(true) or via CLI --constants --constantNameAsValue.

Don't generate getter methods

Use ->setNoMethods(true) or via CLI --noMethods. Usually used when you only want to generate constants.

统计信息

  • 总下载量: 154
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固