taeluf/resource

v0.1.x-dev 2021-10-14 19:39 UTC

This package is auto-updated.

Last update: 2024-09-04 08:17:55 UTC


README

通过键存储值,稍后可以通过调用 R('the.key'); 简单地检索它们

注意 - 2021年9月30日

它正在运行,但我还没有更新文档。现在可以通过composer获取。

检索

  • 检索一个实例: R()
  • 检索一个存储的值: R('stored.key')
  • 检索一个存储的值的列表: R('stored.key.')
  • 使用默认备份值检索: R('stored.key','defaultValue')(否则,将抛出异常)

设置

检索一个实例,然后调用适当的函数

  • R()->set('key','value');
  • R()->set('key.subkey.another','value');
  • R()->put(['key'=>'value','key.sub'=>'value 2']);
  • R()->put(['key'=>$subArray,'another'=>$anArray],'',TRUE); 也可以将子数组的键存储到 R 中
  • R()->put([...], 'prefix.'); 将所有设置的值前缀为 prefix.。`.` 是可选的,适合用于命名空间
    • loadput 将前缀作为第二个参数。 set 不接受前缀
  • R()->load('/file-path.json', 'prefix.');
  • 加载目录: R()->loadDir($dirPath,$prefix='',$prefixWithFileNames=FALSE,$putSubArrays=TRUE,$recurse=TRUE)

处理任何文件类型

  • R()->setFileHandler($callable,'ext');
    • 例如: $callable($content,$ext)

安装

  • src/R.php 复制到您的项目中,并使用 require_once
  • git clone git@github.com:ReedOverflow/PHP-Resources.git; 然后 require_once src/R.php
  • 待办事项 composer require rbear/resource

问题

  • 测试并不完全彻底。
  • README 可以改进。

旧,更详细的说明

检索一个值

有三种方法可以检索一个值

- `$theValue = R("the.key")` - returns the stored value or `null` if nothing found  
- `$theValue = R("the.key","default_value")` - returns the stored value or the supplied default value if nothing found  
- `$theValue = R("the.key.")` - Returns an array of children (and the value at `the.key`).   
    For the input array `['the.key'=>'Primary','the.key.one'=>'child','the.key.two'=>'another child']`,  
    `R("the.key.)` will return:  
    ```php
        [    '=' => 'Primary',
            'one'=> 'child',
            'two'=> 'another child'
        ]
    ```  

处理文件扩展名

传递一个可调用的函数和一个要处理的扩展名或扩展名数组。
调用 R()->setFileHandler( $function, 'ext')

  • $function 返回一个数组
  • $function 将第一个参数解析为字符串并将其作为数组解析
  • $function 将文件扩展名作为第二个参数,扩展名前有一个点。
    示例
    $json_decoder = function($content,$ext){
     return json_decode($content,true);
    };
    R()->setFileHandler($json_decoder,'json');
    

创建自己的实例

R() 使用一个 static 变量,它是 R 类的一个实例。您可以使用 $z = new R() 然后使用 $z(...) 就像使用 R(...) 一样。或者,您可以编写自己的函数

function Z(...$args){
    static $theObject = null;
    $theObject = $theObject ?? new R();
    return $theObject(...$args);
};