szabacsik/catalog

0.0.1 2021-04-10 08:41 UTC

This package is auto-updated.

Last update: 2024-09-25 21:58:19 UTC


README

目录是一个文件索引层,类似于目录。它的目的是使根据统一架构命名的文件易于快速检索,无需在文件系统中搜索,并且具有足够的性能。

它可以与例如Flysystem文件存储库一起使用。

示例用例

假设我们正在处理以下统一架构命名的文件

../path/to/files/2020
../path/to/files/2021
../path/to/files/2020/USER#1
../path/to/files/2020/USER#2
../path/to/files/2020/USER#1/BLOGPOST#1_001.png
../path/to/files/2020/USER#1/BLOGPOST#1_002.png
../path/to/files/2020/USER#1/BLOGPOST#1_003.png
../path/to/files/2020/USER#1/BLOGPOST#2_001.png
../path/to/files/2020/USER#1/BLOGPOST#2_002.png
../path/to/files/2020/USER#1/PROFILE_001.png
../path/to/files/2020/USER#2/BLOGPOST#1_001.png
../path/to/files/2020/USER#2/BLOGPOST#1_002.png
../path/to/files/2020/USER#2/PROFILE_001.png
../path/to/files/2021/USER#1
../path/to/files/2021/USER#3
../path/to/files/2021/USER#1/BLOGPOST#2_003.png
../path/to/files/2021/USER#1/PROFILE_002.png
../path/to/files/2021/USER#3/BLOGPOST#1_001.png
../path/to/files/2021/USER#3/PROFILE_001.png

您可以使用正则表达式轻松地在它们之间进行搜索

查找由"用户 1"上传的所有文件

$re = '/^(?=.*USER#1).*$/i';
$filenames = $catalog->findAll($re);

将得到以下结果

array(8) {
  [0]=>
  string(51) "/path/to/files/USER#1/BLOGPOST#1_001.png"
  [1]=>
  string(51) "/path/to/files/USER#1/BLOGPOST#1_002.png"
  [2]=>
  string(51) "/path/to/files/USER#1/BLOGPOST#1_003.png"
  [3]=>
  string(51) "/path/to/files/USER#1/BLOGPOST#2_001.png"
  [4]=>
  string(51) "/path/to/files/USER#1/BLOGPOST#2_002.png"
  [5]=>
  string(48) "/path/to/files/USER#1/PROFILE_001.png"
  [6]=>
  string(51) "/path/to/files/USER#1/BLOGPOST#2_003.png"
  [7]=>
  string(48) "/path/to/files/USER#1/PROFILE_002.png"
}

查找由"用户 1"创建的与"博客文章 2"相关的所有文件

$re = '/^(?=.*USER#1)(?=.*BLOGPOST#2).*/i';
$filenames = $catalog->findAll($re);

将得到以下结果

array(3) {
  [0]=>
  string(51) "/path/to/files/2020/USER#1/BLOGPOST#2_001.png"
  [1]=>
  string(51) "/path/to/files/2020/USER#1/BLOGPOST#2_002.png"
  [2]=>
  string(51) "/path/to/files/2021/USER#1/BLOGPOST#2_003.png"
}

要使其工作,您只需在保存时按照统一架构命名文件并将它们添加到目录中即可

$filename = '/path/to/files/2021/USER#1/BLOGPOST#2_004.png';
$filesystem->write($filename, $data);
$catalog->add($filename);

入门

composer require szabacsik/catalog
mkdir ../path/to/files/catalog -p
<?php
use Szabacsik\Catalog\CatalogFile;
require_once(__DIR__.'/vendor/autoload.php');
$catalogWorkingDirectory='D:\tmp\catalog';
$catalog = new CatalogFile($catalogWorkingDirectory);
$catalog->add('/path/to/files/2020/USER#1/BLOGPOST#1_001.png');
$catalog->add('/path/to/files/2020/USER#1/BLOGPOST#2_001.png');
$re = '/^(?=.*USER#1).*$/i';
$re = '/^(?=.*USER#1)(?=.*BLOGPOST#2).*/i';
$filenames = $catalog->findAll($re);
var_dump($filenames);