orchestra/memory

Orchestra 平台的内存组件

v6.0.0 2021-04-17 18:24 UTC

README

Memory 组件通过“内存”运行时或数据库(使用缓存、Fluent 查询构建器或 Eloquent ORM)来处理运行时配置。它不仅允许使用静态配置,而且还通过利用多种数据存储选项(通过缓存或数据库)来持久化配置,从而在请求之间保持这些配置。

tests Latest Stable Version Total Downloads Latest Unstable Version License Coverage Status

版本兼容性

安装

要使用 composer 安装,请在终端运行以下命令

composer require "orchestra/memory"

配置

接下来,在 config/app.php 中添加服务提供者。

'providers' => [

  // ...

  Orchestra\Memory\MemoryServiceProvider::class,

],

别名

您可能希望在 config/app.php 中将 Orchestra\Support\Facades\Memory 添加到类别名。

'aliases' => [

  // ...

  'Memory' => Orchestra\Support\Facades\Memory::class,

],

迁移

在我们开始使用 Memory 组件之前,请运行以下命令

php artisan migrate

发布配置

可选地,如果您需要更改默认配置,也可以发布配置文件。

php artisan vendor:publish --provider="Orchestra\\Memory\\MemoryServiceProvider"

使用

创建实例

以下是使用 Memory 组件的可能方式列表

$runtime  = Memory::make('runtime');
$fluent   = Memory::make('fluent');
$eloquent = Memory::make('eloquent');
$cache    = Memory::make('cache');

然而,大多数时候您不需要除了默认的(使用 orchestra_options 表)之外的额外内存实例。

$memory = Memory::make();

在使用 Orchestra 平台时,应用程序中将使用 Memory::make()

存储项

在 Memory 组件中存储项非常简单。只需调用 put 方法

$memory->put('site.author', 'Taylor');

// or you can also use
Memory::put('site.author', 'Taylor');

第一个参数是配置项的 。您将使用此键从配置中检索项目。第二个参数是项的

检索项

从 Memory 组件中检索项比存储它们更简单。它是通过 get 方法完成的。只需提及您希望检索的项的键

$name = $memory->get('site.author');

// or you can also use
$name = Memory::get('site.author');

默认情况下,如果项不存在,将返回 NULL。但是,您可以将不同的默认值作为方法的第二个参数传递

$name = $memory->get('site.author', 'Fred');

现在,如果“site.author”项不存在,则将返回“Fred”。

移除项

需要删除项?没问题。只需在 forget 方法中提及项的名称

$memory->forget('site.author');

// or you can also use
Memory::forget('site.author');

扩展 Memory

可能会有这样的需求,即对于内存实例,需要使用不同类型的存储引擎,您可以通过添加自己的处理程序来扩展它。

<?php

use Orchestra\Contracts\Memory\Handler;

class AcmeMemoryHandler implements Handler
{
  // Add your implementation
}

Memory::extend('acme', function ($app, $name) {
  return new Orchestra\Memory\Provider(
    new AcmeMemoryHandler($name)
  );
});

// Now you can use it as
$acme = Memory::make('acme.default');

您还可以扩展 Orchestra\Memory\Handler,它会在您的自定义处理程序上添加一些样板代码。