PHPackages                             szabacsik/catalog - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. szabacsik/catalog

ActiveLibrary

szabacsik/catalog
=================

Catalog

0.0.1(5y ago)011MITPHPPHP ^7.2 || ^8.0

Since Apr 10Pushed 4y ago1 watchersCompare

[ Source](https://github.com/szabacsik/catalog)[ Packagist](https://packagist.org/packages/szabacsik/catalog)[ RSS](/packages/szabacsik-catalog/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

Catalog
=======

[](#catalog)

**Catalog** is a file indexing layer, like a table of contents. Its purpose is to make files, named according to a uniform schema, easily and quickly retrievable without searching in the file system, and with adequate performance.

It can be used together for example with the [Flysystem](https://github.com/thephpleague/flysystem) file storage library.

Example use case
----------------

[](#example-use-case)

Suppose we are working with files named according to the following uniform schema:

```
../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
```

you can easily search between them using regular expression:

### Find all files upload by "user 1"

[](#find-all-files-upload-by-user-1)

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

will result:

```
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"
}
```

### Find all files related to "blogpost 2" created by "user 1"

[](#find-all-files-related-to-blogpost-2-created-by-user-1)

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

will result:

```
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"
}
```

To make it work, all you have to do is name the files in the uniform schema and add them to the catalog when you save them:

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

Getting Started
---------------

[](#getting-started)

```
composer require szabacsik/catalog
```

```
mkdir ../path/to/files/catalog -p
```

```
