taylornetwork/variable-replacer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

taylornetwork/variable-replacer

Composer 安装命令:

composer require taylornetwork/variable-replacer

包简介

Replaces variables in strings at defined stages

README 文档

README

This package will replace variables in a string. It includes functionality to use a stage based replace, meaning you can replace multiple variables in the same string at different times.

Install

Install using Composer

$ composer require taylornetwork/variable-replacer

Default Options

By default the syntax for a replaced variable is

'@stage{var}'

Note no $ on the variable.

Usage

Let's say you have a logger class that logs recent activity and you want to define the same message whenever any model is created but want information from the model in the description.

In this example my base model class is defined as:

// App\Models\Model.php

use Illuminate\Database\Eloquent\Model;

abstract class Model
{

    /**
     * All models will define what the model's name is
     *
     * @return string
     */
    abstract public funtion getNameAttribute();

    /**
     * Returns the model name
     *
     * @return string
     */
    public function getModelNameAttribute()
    {
        return class_basename(get_class($this));
    }
}

In an observer class you could do the following.

// App\Observers\BaseObserver.php

use TaylorNetwork\VariableReplacer\VariableReplacer;
use App\Models\Model;
use Some\Logger\Package\Logger;

class BaseObserver
{
    protected $descriptions = [
        'created' => 'A @entry{modelName} named @runtime{name} was created.',
    ];
    
    public function created(Model $model) 
    {
        $description = (new VariableReplacer)->stage('entry')
                                             ->replaceWith($model)
                                             ->parse($this->descriptions['created']);
      
        return (new Logger)->log($description);
    }
}

The VariableReplacer gets the entire App\Models\Model $model object and as such, all methods are available to be used.

If a model App\Models\Customer with name 'John Smith' was created the $description that would be saved to the database is

'A Customer name @runtime{name} was created.'

When accessing the log you could run the description through the replacer with the runtime stage.

// App\Models\Log.php

use TaylorNetwork\VariableReplacer\VariableReplacer;
use Illuminate\Database\Eloquent\Model;

class Log extends Model
{
    public function getDescriptionAttribute()
    {
        return (new VariableReplacer)->stage('runtime')
                                     ->replaceWith($this->relatedModel)
                                     ->parse($this->attributes['description']);
    }
}

Assuming 'A Customer named @runtime{name} was created.' was passed the returned value would be

'A Customer named John Smith was created.'

Note

Because the VariableReplacer gets the entire object you pass to the replaceWith method, you can chain methods onto it if you need to get relations, etc.

Example:

(new VariableReplacer)->stage('runtime')
                      ->replaceWith($someModel)
                      ->parse('A @runtime{relatedModel->someRelation->someOtherMethod()} did something');

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固