zumba/swivel-cake

一个 CakePHP 插件,提供对 zumba/swivel 的便捷访问

安装次数: 68,742

依赖项: 0

建议者: 0

安全性: 0

星标: 2

关注者: 35

分支: 0

公开问题: 0

类型:cakephp-plugin

v2.0.4 2020-11-10 20:01 UTC

This package is auto-updated.

Last update: 2024-09-11 04:13:37 UTC


README

Zumba Swivel 是一个库,允许 PHP 应用程序通过桶来管理多个用户的特性。它包含 10 个桶,使得相同的代码可以具有多达 10 种不同的行为。

此插件是 CakePHP 和 Swivel 之间的桥梁。它提供助手、组件和类行为,可在您的 CakePHP 应用程序中使用。

安装

您可以使用 composer 将 Swivel Cake 安装到您的项目中。对于现有应用程序,您可以将以下内容添加到您的 composer.json 文件中

    "require": {
        "zumba/swivel-cake": "1.*"
    }

然后运行 php composer.phar update

加载插件

安装后,您应该告诉应用程序加载插件

CakePlugin::load('Swivel', ['bootstrap' => true]);

配置

插件具有默认配置并准备就绪。但是,您可以自定义一些配置。

要设置自定义配置,您需要在 APP/Config/swivel.php 中创建一个文件并设置您要覆盖的字段。以下是一个默认配置文件

<?php

$config = [
    'Swivel' => [
        'Cookie' => [
            'enabled' => true,
            'name' => 'Swivel_Bucket',
            'expire' => 0,
            'path' => '/',
            'domain' => env('HTTP_HOST'),
            'secure' => false,
            'httpOnly' => false
        ],
        'BucketIndex' => null,
        'LoaderAlias' => 'SwivelManager',
        'Logger' => null,
        'Metrics' => null,
        'ModelAlias' => 'Swivel.SwivelFeature',
    ]
];

例如,如果您想为测试保留一个桶,并将其他 9 个桶分配给您的客户,您可以这样做

<?php

// Saving bucket 1 for internal testing
$bucketIndex = isset($_COOKIE['Swivel_Bucket']) ? $_COOKIE['Swivel_Bucket'] : mt_rand(2, 10);

$config = [
    'Swivel' => [
        'BucketIndex' => $bucketIndex
    ]
];

加载特性列表

为了使 Swivel 能够工作,您需要指定每个桶启用了哪些特性。

通过数据库加载特性

swivel-cake 的默认行为是从数据库中加载特性。这是通过内置类 Swivel.SwivelFeature 实现的。然而,此类期望在您的 default 数据库配置中存在表 swivel_buckets

这是最低的表结构

CREATE TABLE `swivel_features` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `slug` varchar(255) NOT NULL,
  `buckets` varchar(20) NOT NULL DEFAULT '1,2,3,4,5,6,7,8,9,10',
  PRIMARY KEY (`id`),
  UNIQUE KEY `slug_UNIQUE` (`slug`)
);

如果您想添加更多字段,请随意添加。例如,在 Zumba 中,我们有一个名为 modified 的字段,当更改桶配置时它会自动更新。如果您重命名了预定义的字段,您将必须扩展插件模型并相应地进行更新。

请注意,桶位于字符串字段中,以逗号分隔。您不应该在数字之间添加空格。

通过自定义来源加载

您还可以从任何其他来源加载特性列表,例如,从您用于跨所有应用程序共享的 Web 服务。

为了做到这一点,创建一个模型并将此模型实现为 SwivelModelInterface 接口。例如,我将我的模型命名为 MySwivelFeature

<?php
App::uses('AppModel', 'Model');
App::uses('SwivelModelInterface', 'Swivel.Lib');

class SwivelFeature extends AppModel implements SwivelModelInterface {
    public $useTable = false;

    public function getMapData()
    {
        // @todo Load data from some source
        // @todo Format this data in a KEY/VALUE array, where KEY is the feature
        // and VALUE is the buckets in an array format, ie [1, 2, 3]
        // @todo Return the formatted data
        return [
            'FeatureA' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            'FeatureB' => [1, 2, 3],
            'FeatureC' => [2, 4, 6, 8, 10],
        ];
    }
}

使用

组件、行为和助手实现了 swivel 中的 forFeatureinvoke 方法。您可以在 Swivel 的文档 中查看这些方法的详细信息。

以 CakePHP 应用程序为例

<?php

class UsersController extends AppController
{
    public $components = ['Swivel.Swivel'];
    public $uses = ['User', 'MyCoolWidget'];

    public function index()
    {
        $this->set('users', $this->User->find(/* ... */));
        $this->Swivel->invoke('MyCoolWidget', function() {
            return $this->set('widget', $this->MyCoolWidget->find(/* ... */));
        });
    }

    public function view($id = null)
    {
        $this->Swivel->forFeature('Redesign')
            ->addBehavior('userView', [$this, 'renderNewView'], [$id])
            ->defaultBehavior([$this, 'renderOldView'], [$id])
            ->execute();
    }

    protected function renderOldView($id)
    {
        // @todo implement
        $this->render('oldView');
    }

    protected function renderNewView($id)
    {
        // @todo implement
        $this->render('newView');
    }
}

这只是如何在控制器中使用插件的一个示例,但您也可以在模型和视图中使用它。