FastSitePHP\FileSystem\Search

File System Search

This Class has functions for searching the local file system for files and directories. Additionally URL Lists can be built from a list of files.

This class works by setting the root search directory/folder [dir()], setting various search options, and then calling one of [files(), dirs(), or urlFiles($url_root)] functions.

Código Fonte

GitHub

Código de Exemplo

Busque por Arquivos e Diretórios (Pastas)

// Crie um Objeto Search do Sistema de Arquivos
$search = new \FastSitePHP\FileSystem\Search();

// Para utilização básica, especifique um diretório raiz com o comando
// [dir()] e então chame [files()] ou [dirs()]. Um array the nomes correspondentes
// será retornado.
$files = $search->dir($dir_path)->files();

// Funções são encadeáveis então quebrá-las em uma por linha pode tornar o
// código mais fácil de ler.
$dirs = $search
    ->dir($dir_path)
    ->dirs();

// [all()] can be used to return both directories and files
list($dirs, $files) = $search->dir($dir_path)->all();

// URL lists can also be generated from matching files.
$url_root = 'http://www.example.com/';
$urls = $search
    ->dir($dir_path)
    ->urlFiles($url_root);

// Existem várias funções com critérios diferentes e podem ser utilizadas
// para filtrar os resultados. Neste exemplo uma busca recursiva é utilizada
// para encontrar arquivos PHP que contenham o texto 'FileSystem'. Quando
// uma busca recursiva é utilizada, o caminho completo dos arquivos é
// retornado a não ser que [includeRoot(false)] esteja definida.
// Veja a documentação e exemplos para todas as funções.
$files = $search
    ->dir($dir_path)
    ->recursive(true)
    ->fileTypes(['php'])
    ->includeText(['FileSystem'])
    ->files();

Métodos

dir($new_value = null)

Propriedade Getter / Setter

Get or set the root directory for searching.

Retorna: null | string | $this

reset()

Reset all options other than the root search directory.

Retorna: $this

recursive($new_value = null)

Propriedade Getter / Setter

If true then sub-directories/folders will be searched when either [dirs() or files()] are called and the full path will be returned.

Defaults to false.

Retorna: bool | $this

includeRoot($new_value = null)

Propriedade Getter / Setter

Applies only when using [recursive(true)]. If set to false then the root search directory will be excluded from the returned file/dir list.

Defaults to true.

Retorna: bool | $this

fullPath($full_path = null)

Propriedade Getter / Setter

If true then then the full file paths will be returned when [dirs() or files()] are called. Defaults to false, however when [recursive(true)] is used then the value will always be true.

Retorna: bool | $this

fileTypes(array $new_value = null)

Propriedade Getter / Setter

Specify an array of files types to filter on when calling [files() or urlFiles()].

Example:
    $search->fileTypes(['png', 'jpg', 'svg'])

Retorna: null | array | $this

includeNames(array $new_value = null)

Propriedade Getter / Setter

Specify an array of files/dir names to include on when calling [dirs(), files(), or urlFiles()]. If a file/dir matches any names in the list then it will be included in the result.

Example:
    $search->includeNames(['index.php', 'app.php'])

Retorna: null | array | $this

includeRegExNames(array $new_value = null)

Propriedade Getter / Setter

Specify an array of regex patterns to include on when calling [dirs(), files(), or urlFiles()]. If a file/dir name matches any regex in the list then it will be included in the result.

Example:
    $search->includeRegExNames(['/^app/', '/.htm$/'])

Retorna: null | array | $this

includeRegExPaths(array $new_value = null)

Propriedade Getter / Setter

Specify an array of regex patterns to include on when calling [dirs(), files(), or urlFiles()]. If part of the full path matches any regex in the list then it will be included in the result.

Retorna: null | array | $this

excludeNames(array $new_value = null)

Propriedade Getter / Setter

Specify an array of files/dir names to exclude on when calling [dirs(), files(), or urlFiles()]. If a file/dir matches any names in the list then it will be excluded from the result.

Example:
    $search->excludeNames(['.DS_Store', 'desktop.ini'])

Retorna: null | array | $this

excludeRegExNames(array $new_value = null)

Propriedade Getter / Setter

Specify an array of regex patterns to exclude on when calling [dirs(), files(), or urlFiles()]. If a file/dir name matches any regex in the list then it will be excluded from the result.

Example:
    $search->excludeRegExName(['/^[.]/', '/^testing-/'])

Retorna: null | array | $this

excludeRegExPaths(array $new_value = null)

Propriedade Getter / Setter

Specify an array of regex patterns to exclude on when calling [dirs(), files(), or urlFiles()]. If part of the full path matches any regex in the list then it will be excluded from the result.

Retorna: null | array | $this

includeText(array $new_value = null)

Propriedade Getter / Setter

Specify an array of search text that matching files must contain to be included in the result. If running from a web page or web service then this option should only be used against known files because it does not exclude large files from be opened.

Example:
    $search->fileTypes(['php'])->includeText(['X-API-Key'])

By default text searches are case-insensitive which is controlled by the [caseInsensitiveText()] function.

Retorna: null | array | $this

caseInsensitiveText($new_value = null)

Propriedade Getter / Setter

Specify if content searches defined from [includeText()] should be case-insensitive or not.

Defaults to [true] which means that ('ABC' === 'abc').

Retorna: bool | $this

hideExtensions($new_value = null)

Propriedade Getter / Setter

If set to [true] then file extensions will be hidden on the result. This only applies to [files()] and requires [fullPath()] to be false.

Retorna: bool | $this

files()

Returns an array of file names in a directory matching the specified criteria.

Retorna: array

dirs()

Returns an array of directory names in a directory matching the specified criteria and excluding the dot directories '.' and '..'.

Retorna: array

urlFiles($url_root)

Returns an array of url names for files in directory matching the specified criteria.

Currently this option doesn't work with recursive directories [option: recursive(true)].

Retorna: array