starfolksoftware/kalibrant 问题修复 & 功能扩展

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

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

starfolksoftware/kalibrant

Composer 安装命令:

composer require starfolksoftware/kalibrant

包简介

A package to manage setting of your models.

README 文档

README

Logo Kalibrant

Build Status Build Status Build Status Total Downloads Latest Stable Version License

Introduction

For your laravel 9.x applications, Kalibrant is a package that provides a simple way to manage your models settings. It is a simple way to manage your user and team models settings.

Installation

You can install the package via composer:

composer require starfolksoftware/kalibrant

You can publish and run the migrations with:

php artisan vendor:publish --tag="kalibrant-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="kalibrant-config"

This is the contents of the published config file:

return [
    /**
     * Define all the settings groups.
     */
    'groups' => [
        // 'setting-group' => SettingGroup::class,
    ],

    'middleware' => ['web'],
];

Usage

Add the HasSetting trait to your model:

<?php

namespace App\Models;

use StarfolkSoftware\Kalibrant\HasSettings;

class User extends Authenticatable
{
    use HasSetting;
}

You can create a new setting file by running the following command from the terminal:

php artisan make:setting AutopilotSettings

This is will create a new file in the /app/Settings/ directory. Here is a sample file:

<?php

namespace App\Settings;

use App\Models\User;
use StarfolkSoftware\Kalibrant\Settings;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AutopilotSettings extends Settings
{
    /**
     * The route to redirect to after update.
     * 
     * @var mixed
     */
    public $redirectRoute = 'profile.show';

    /**
     * Constructor.
     *
     * @param mixed $id
     * @return void
     */
    public function __construct(
        protected $id
    )
    {
        parent::__construct();
    }

    /**
     * Configure the settings attributes
     * 
     * @param OptionsResolver $resolver
     * 
     * @return void
     */
    public function configureAttributes(OptionsResolver $resolver)
    {
        $resolver->define('enabled')
            ->default(false)
            ->allowedTypes('boolean')
            ->info('Whether autopilot is enabled');
        
        $resolver->define('channels')
            ->default(['twitter'])
            ->allowedTypes('array')
            ->info('The channels to autopilot');

        $resolver->define('tweet_freq')
           ->default(60)
           ->allowedTypes('integer', 'string')
           ->info('Tweets count every hour.');

        $resolver->define('retweet_freq')
            ->default(15)
            ->allowedTypes('integer', 'string')
            ->info('Number of retweets in an hour');

        $resolver->define('like_freq')
            ->default(15)
            ->allowedTypes('integer', 'string')
            ->info('Number of likes in an hour');

        $resolver->define('follow_freq')
            ->default(15)
            ->allowedTypes('integer', 'string') 
            ->info('Number of follows in an hour');
            
        $resolver->define('hashtags')
            ->default(['#programming', '#dev'])
            ->allowedTypes('array')
            ->info('Relavant hashtags.');
    }

    /**
     * Returns the setable type
     * 
     * @return string
     */
    public static function setableType()
    {
        return User::class;
    }

    /**
     * Returns the setable id
     * 
     * @return int
     */
    public function setableId()
    {
        return $this->id;
    }

    /**
     * Return the settings group
     * 
     * @return string
     */
    public static function group()
    {
        return 'autopilot-settings';
    }

    /**
     * Validation rules.
     * 
     * @return array
     */
    public function rules(): array
    {
        return [
            'enabled' => ['required', 'boolean'],
            'channels' => ['required', 'array'],
            'channels.*' => ['required', 'string', 'in:twitter,facebook,instagram,linkedin'],
            'tweet_freq' => ['required', 'integer', 'max:12'],
            'retweet_freq' => ['required', 'integer', 'max:12'],
            'like_freq' => ['required', 'integer', 'max:12'],
            'follow_freq' => ['required', 'integer', 'max:12'],
            'hashtags' => ['required', 'array'],
            'hashtags.*' => ['required', 'string'],
        ];
    }
}

To update the settings, make a PUT request to route('settings.update', ['group' => '..', 'id' => '...']) with the defined attributes as data. A sample request to update the above settings:

<?php

    $response = $this->putJson(route('settings.update', ['group' => 'autopilot-settings', 'id' => $user->id]), [
        'enabled' => true,
        'channels' => ['twitter', 'facebook'],
        'tweet_freq' => '60',
        'retweet_freq' => '15',
        'like_freq' => '15',
        'follow_freq' => '15',
        'hashtags' => ['#programming', '#dev'],
    ]);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固