seanstewart/plan-config 问题修复 & 功能扩展

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

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

seanstewart/plan-config

Composer 安装命令:

composer require seanstewart/plan-config

包简介

Plan config allows you to easily define attributes and limits for your SaaS application subscription plans.

README 文档

README

This Laravel 5 package makes it easy to manage the rules/limits of your SaaS app subscription plans.

How to install

Require the package with composer

composer require seanstewart/plan-config

Laravel 5.5 uses Package Auto-Discovery and it is not necessary to manually add the ServiceProvider.

Laravel 5.5+:

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php

'providers' => [
    Seanstewart\PlanConfig\PlanConfigServiceProvider::class
];

Include the facade (optional) in app/config/app.php.

'aliases' => [
    'Plan'       => Seanstewart\PlanConfig\Plan::class
];

Then you will need to generate your config by running the command

php artisan vendor:publish --provider="Seanstewart\PlanConfig\PlanConfigServiceProvider"

How to Use

Let's say your app has subscription plans that limit the number of widgets a user can add. You would have some sort of logic that checks the number of widgets a user is allowed to have in their account. With Plan Config you can do that by calling the helper function plan().

if($this->getCurrentNumberOfWidgets < plan('limits.widgets'))
{
    // Allow the user to add a new widget
}

The plan() helper function knows what plan the current user is subscribed to and grabs the limits you defined in your plans.php config file. You can use the helper function anywhere in your application (views, controllers, models, middleware, etc.). Using the previous example, your plan config file would look like this:

'plans' => [

    'bronze'   => [
        'limits' => [
            'widgets' => 5
        ]
    ],

    'silver'   => [
        'limits' => [
            'widgets' => 10
        ]
    ],

    //...and so on

]

If your user is subscribed to the silver plan, they could only add 10 widgets. You can even adapt it to use other attributes, like a title, description, or pricing for your plans.

'plans' => [

    'bronze' => [
        'title'       => 'Bronze Plan',
        'description' => 'This is some description for a Bronze Plan',
        'price'       => '19.00',
        'currency'    => 'USD',
        'limits'      => [
            'widgets' => 5
        ]
    ],

    'silver' => [
        'title'       => 'Silver Plan',
        'description' => 'This is some description for the Silver Plan',
        'price'       => '29.00',
        'currency'    => 'USD',
        'limits'      => [
            'widgets' => 10
        ]
    ],

    //...and so on

]

Get a Plan's Config Without the User

In the case where a specific plan config is needed, you can pass in the plan's code as a string for the 2nd argument:

plan('limits.widgets', 'bronze'); // Returns 5
plan('limits.widgets', 'silver'); // Returns 10

Configuring Your Plans

To configure your plans, open up app/plans.php and start adding your plan details. By default the package assumes that you're using laravel's built in Auth, and that the user's plan is stored in the User model. You can set the field used to determine the user's plan in the config...

'plan_field' => 'stripe_plan'

To configure your plans, add your plan data in the 'plans' array.

'plans' => [

    'bronze'   => [
        'limits' => [
            'widgets' => 5
        ]
    ],

    'silver'   => [
        'limits' => [
            'widgets' => 10
        ]
    ],

    //...and so on

]

If you have rules that apply to all plans, you can define a default or fallback plan. In the config file, set your fallback plan...

'fallback_plan' => '_default',

And then define the _default plan in your plans array.

'plans' => [

    '_default' => [
        'limits' => [
            'purple_widgets' => 20
        ]
    ]

    'bronze'   => [
        'limits' => [
            'widgets' => 5
        ]
    ],

    'silver'   => [
        'limits' => [
            'widgets' => 10
        ]
    ],

]

In the above example, calling plan('limits.purple_widgets') will give you the value from the fallback plan.

Alternatively you can use the facade and call Plan::get('limits.purple_widgets')

Overrides

Plan config data can be overridden on the user level by setting an override attribute in the config.

'overrides' => [

        // The user model attribute that stores the attributes that can be changed
        'user_model_attribute' => 'plan_overrides',

        // The keys that are allowed to be changed. Set to all by default (['*']).
        'allowed' => ['*'],

    ]

In the above example, you would create the plan_overrides attribute on the user model. This field should be casted as an array and should include the list of keys and values that should be overridden for the given user.

Example

Plan Config with Overrides:

'overrides' => [

        'user_model_attribute' => 'plan_overrides',

        'allowed' => ['limits.apples', 'limits.bananas'],

    ],


'plans' => [

    '_default' => [
        'limits' => [
            'purple_widgets' => 20
        ]
    ]

    'bronze'   => [
        'limits' => [
            'widgets' => 5,
            'apples' => 10,
            'bananas' => 15
        ]
    ],

    'silver'   => [
        'limits' => [
            'widgets' => 10,
            'apples' => 15,
            'bananas' => 20
        ],
    ],

]

User Model plan_overrides attribute:

['limits.widgets' => 1, 'limits.apples' => 50, 'limits.bananas' => 100]

Would result in the following...

User's Plan Key Call Result Overridden?
silver limits.widgets plan('limits.widgets'); 10 No
silver limits.apples plan('limits.apples'); 50 Yes
silver limits.bananas plan('limits.bananas'); 100 Yes
silver limits.bananas plan('limits.bananas', 'silver') 20 No
bronze limits.bananas plan('limits.bananas', 'silver') 20 No
bronze limits.bananas plan('limits.bananas', $user) 100 Yes

If you want to return a user's entire plan config (along with overrides), you can pass in * as the first argument.

plan('*');

Why I created this

I've always found that managing subscriptions and plans for a SaaS app can be complicated. I felt like storing these values in a database isn't the best approach considering a lot of your values and limits will not change frequently. When building Election Runner, a web application that allows schools & organizations to run elections, we needed something to accomplish exactly this. Hopefully others will find this as useful as we do!

seanstewart/plan-config 适用场景与选型建议

seanstewart/plan-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.03k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2015 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「config」 「laravel」 「saas」 「plans」 「laravel5」 「subscription plans」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 seanstewart/plan-config 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4.03k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 31
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 31
  • Watchers: 4
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-15