FastSitePHP\FileSystem\Sync

File System Sync

This class provides the ability for syncing of all files and directories from one directory to another directory. This class compares files using a hash (defaults to 'sha256') and updates the files if different. Additionally new files, deleted files, new empty directories, and deleted directories are handled.

Source Code

GitHub

Exemple de Code

File System Sync

// Create a FileSystem Sync Object
$sync = new FastSitePHP\FileSystem\Sync();

// Sync files and directories (folders) from [dirFrom(path)] to [dirTo(path)].
// The sync is recursive so all files and directories are synced in all
// sub-directories. Required functions are [dirFrom, dirTo, and sync].
// To view the results call [printResults()] after calling [sync()].
// All options with defaults are shown below.
$sync
    ->dirFrom($dir_from)
    ->dirTo($dir_to)
    ->excludeNames(['package-lock.json'])
    ->excludeRegExPaths(['/node_modules/'])
    ->summaryTitle('File System Sync Results')
    ->hashAlgo('sha256')
    ->dryRun(false) // Set to [true] for testing
    ->sync()
    ->printResults();

Methods

dirFrom($new_value = null)

Getter / Setter Property

Get or set the directory to sync from (source directory).

Returns: string | $this

dirTo($new_value = null)

Getter / Setter Property

Get or set the directory to sync to (destination directory).

Returns: string | $this

excludeNames(array $new_value = null)

Getter / Setter Property

Get or set an array of files/dir names to exclude. If a file/dir matches any names in the list then it will be excluded from the result. This property does not handle files in nested directories. For nested files use [excludeRegExPaths()].

Returns: array | $this

excludeRegExPaths(array $new_value = null)

Getter / Setter Property

Get or set an array of files/dir regex path expressions to exclude. If part of the full path matches any regex in the list then it will be excluded from the result.

Example usage:
    $sync->excludeRegExPaths(['/node_modules/']);

Returns: array | $this

summaryTitle($new_value = null)

Getter / Setter Property

Get or set the summary title used for report output when calling [printResults()]. Defaults to 'File System Sync Results'.

Returns: string | $this

dryRun($new_value = null)

Getter / Setter Property

Get or set a dry run boolean value for testing. When set to [true] no changes will be made when calling [sync()]. Defaults to [false].

Returns: bool | $this

hashAlgo($new_value = null)

Getter / Setter Property

Get or set the hashing algorithm for comparing files when syncing. Defaults to 'sha256'.

Returns: string | $this

sync()

Sync files and directories (folders) from [dirFrom(path)] to [dirTo(path)]. The sync is recursive so all files and directories are synced in all sub-directories.

To view the results of the sync call [printResults()] after calling this function.

Returns: $this

printResults()

Output the result of [sync()] as a text summary. This includes a list of all affected files and directories and summary counts. This function will typically be used for CLI output, however if used on a web server then <br> will be used for line breaks.