承接 alwaysblank/brief 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

alwaysblank/brief

Composer 安装命令:

composer require alwaysblank/brief

包简介

Present your arguments.

README 文档

README

Present your arguments.

Build Status

What Is This?

It’s a simple tool for passing around collections of variable structured data.

90% of the reason I built it was because I got very tired of writing isset(), etc, conditionals when the nonexistence of a key was equivalent to a falsey value in terms of program flow. It grew from there to provide some other functionality related to moving collections of structure data around. I find it particularly useful when templating with Laravel Blade.

(Brief is only one way to solve the issues I might use here as examples: It’s not intended to be an authoritative or "best" solution, merely a convenient one, if you find it convenient.)

How Does It Work?

$Brief = Brief::make([
    'key' => 'value',
]);

$Brief->key;
// 'value'

$Brief->doesnt_exist;
// null

Instead of throwing an error or exception, Brief will return null if a key does not have an associated value.

ℹ️

You can also create a Brief with new, make() is just slightly more concise:

$Brief = new Brief([
    'key' => 'value',
]);

Arguments

A new Brief, whether created with new Brief() or Brief::make(), takes two arguments:

$items

The values to be stored, usually as an array.

$settings

Some settings for things like callables, aliases, etc.

Both arguments are optional, although if you wish to create an empty Brief it is recommended to use new EmptyBrief() or Brief::empty() to make your code clearer.

These arguments are treated the same whichever method of instantiation you use, with one major difference. While $items will accept an existing Brief through either method, the behavior will be different:

Brief::make($Brief)

This will return the exact same Brief that you passed to it.

new Brief($Brief)

This will attempt to ingest all of the data and settings stored on the passed Brief and apply them to a new instance of Brief.

In many situations the distinction will be academic—​you can still get at your data whichever one you use—​but the distinction should be kept in mind, especially if you feel you might be using strict comparison on Briefs. My recommendation is to simply pick an instantiation method and use that for an entire project, to minimize possible confusion.

Features

Storing Data

This is the primary feature of Brief.

Brief expects data in an array, but whether that array is keyed or numeric is up to you. (Or, use a language where arrays aren’t as confusing.)

$keyed = Brief::make([
    'key' => 'value1',
    'key2' => 'value2',
]);

$numeric = Brief::make([
    'number1',
    'number2',
]);

Aliasing

You may find yourself in a situation where you want to have multiple keys that point to the same data. With aliases, you can accomplish this easily:

$brief = Brief::make([
    'a_rose' => 'sweet smell',
], [
    'aliases' => [
        'a_rose' => 'any_other_name',
    ]
]);

$brief->a_rose;
// "sweet smell"

$brief->any_other_name;
// "sweet smell"

Aliases are passed through the "settings" array (the optional second argument to a new Brief). Use the key aliases or alias to set any aliases you would like.

You can define multiple aliases for a single key at once by passing an array of strings instead of a single string:

$brief = Brief::make([
    'original_key' => 'value',
], [
    'aliases' => [
        // "Array" style
        'original_key' => ['another_key', 'another_another_key'],
        // "String" style
        'original_key' => 'yet_another_key',
    ]
]);

$brief->original_key === $brief->another_key === $brief->another_another_key === $brief->yet_another_key;
// true

Aliases can also be chained to one another, if that’s something you feel like doing. Brief will make a relatively naive attempt to not get sucked into infinite alias loops, and will simply stop trying to resolve an alias chain if it detects such a loop.

$brief = Brief::make([
    'a_rose' => 'sweet smell',
], [
    'aliases' => [
        'a_rose' => ['any_other_name'],
        'any_other_name' => ['montague'],
    ]
]);

$brief->a_rose === $brief->montague;
// true

Custom Empty Test

Brief comes with isEmpty() and isNotEmpty() which somewhat naively test if the Brief is empty (they examine only whether top-level items in the array are not equal to null). If your use case requires a more robust test, you can pass that test to the isEmpty parameter at instantiation. It accepts anything PHP considers callable.

$brief = Brief::make([
    ['key' => 'value'],
    ['isEmpty' => function($brief) {
        // some logic
    }]
);

$brief->isEmpty(); // false (hopefully)

Logging

Since the basic concept for Brief is about how either your data exists or doesn’t, Brief will not complain loudly if you do something it doesn’t like. If it’s recoverable, it will simply recover and move on, with your data likely lost. In most cases, this should be fine; Your logic will have something to do if Brief gives you null for a piece of data you thought you’d added.

In some situations, though, you don’t want this—​you want to know what’s happened. Fortunately, Brief includes a very simple logging feature. To use it, just do the following:

$brief = Brief::make(
    ['key' => 'value'],
    ['logger' => function($name, $description, $clone, $data) {
        // Do something with this data
    }]
);

In this example, whenever Brief encounters errors that it has some understanding of, an error message will be passed to the callable you’ve defined here. If instead of a callable you pass boolean true to the logger setting, then it will just dispatch an canned message to PHP’s error_log() and your system will handle that however it’s configured to.

If, for some reason, you need to manually log something to a Brief, you can do so:

$brief = Brief::make(
    ['key' => 'value'],
    ['logger' => function($name, $description, $clone, $data) {
        // Do something with this data
    }]
);

$brief->log('ExampleError', 'This is to prove a point', ['a_key' => 'some_value']);

It will be sent to whatever logger you have defined (or, if you haven’t defined one, nothing will happen). This is exactly the same mechanism Brief uses to log errors internally.

⚠️

This method is only fired on problems Brief is equipped to understand and expect; it will not, for instance, catch an exception you throw during transform().

ℹ️

If you used v1 of Brief, then these logger calls happen in the same places where Brief used to throw Exceptions, and replace that functionality; Brief will not longer throw Exceptions of its own volition.

alwaysblank/brief 适用场景与选型建议

alwaysblank/brief 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 909 次下载、GitHub Stars 达 8, 最近一次更新时间为 2019 年 06 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 alwaysblank/brief 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alwaysblank/brief 我们能提供哪些服务?
定制开发 / 二次开发

基于 alwaysblank/brief 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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