Flourish PHP Unframework

fFilesystem

static class, v1.0.0b16

Handles filesystem-level tasks including filesystem transactions and the reference map to keep all fFile and fDirectory objects in sync

Changes:
1.0.0b16Added a call to clearstatcache() to rollback() to prevent incorrect data from being returned by fFilegetMTime() and fFile::getSize() 11/27/10
1.0.0b15Fixed translateToWebPath() to handle Windows \s 4/9/10
1.0.0b14Added recordAppend() 3/15/10
1.0.0b13Changed the way files/directories deleted in a filesystem transaction are handled, including improvements to the exception that is thrown 3/5/10
1.0.0b12Updated convertToBytes() to properly handle integers without a suffix and sizes with fractions 11/14/09
1.0.0b11Corrected the API documentation for getPathInfo() 9/9/09
1.0.0b10Updated updateExceptionMap() to not contain the Exception class parameter hint, allowing NULL to be passed 8/20/09
1.0.0b9Added some performance tweaks to createObject() 8/6/09
1.0.0b8Changed formatFilesize() to not use decimal places for bytes, add a space before and drop the B in suffixes 7/12/09
1.0.0b7Fixed formatFilesize() to work when $bytes equals zero 7/8/09
1.0.0b6Changed replacement values in preg_replace() calls to be properly escaped 6/11/09
1.0.0b5Changed formatFilesize() to use proper uppercase letters instead of lowercase 6/2/09
1.0.0b4Added the createObject() method 1/21/09
1.0.0b3Removed some unnecessary error suppresion operators 12/11/08
1.0.0b2Fixed a bug where the filepath and exception maps weren't being updated after a rollback 12/11/08
1.0.0bThe initial implementation 3/24/08

Static Methods

::addWebPathTranslation() public

Adds a directory to the web path translation list

The web path conversion list is a list of directory paths that will be converted (from the beginning of filesystem paths) when preparing a path for output into HTML.

By default the $_SERVER['DOCUMENT_ROOT'] will be converted to a blank string, in essence stripping it from filesystem paths.

Signature

void addWebPathTranslation( string $search_path, string $replace_path )

Parameters

string $search_path The path to look for
string $replace_path The path to replace with

::begin() public

Starts a filesystem pseudo-transaction, should only be called when no transaction is in progress.

Flourish filesystem transactions are NOT full ACID-compliant transactions, but rather more of an filesystem undo buffer which can return the filesystem to the state when begin() was called. If your PHP script dies in the middle of an operation this functionality will do nothing for you and all operations will be retained, except for deletes which only occur once the transaction is committed.

Signature

void begin( )

::commit() public

Commits a filesystem transaction, should only be called when a transaction is in progress

Signature

void commit( )

::convertToBytes() public

Takes a file size including a unit of measure (i.e. kb, GB, M) and converts it to bytes

Sizes are interpreted using base 2, not base 10. Sizes above 2GB may not be accurately represented on 32 bit operating systems.

Signature

integer convertToBytes( string $size )

Parameters

string $size The size to convert to bytes

Returns

The number of bytes represented by the size

::createObject() public

Takes a filesystem path and creates either an fDirectory, fFile or fImage object from it

Signature

fDirectory|fFile|fImage createObject( string $path )

Parameters

string $path The path to the filesystem object

Throws

fValidationException
When no path was specified or the path specified does not exist

::formatFilesize() public

Takes the size of a file in bytes and returns a friendly size in B/K/M/G/T

Signature

string formatFilesize( integer $bytes, integer $decimal_places=1 )

Parameters

integer $bytes The size of the file in bytes
integer $decimal_places The number of decimal places to display

::getPathInfo() public

Returns info about a path including dirname, basename, extension and filename

Signature

array getPathInfo( string $path, string $element=NULL )

Parameters

string $path The file/directory path to retrieve information about
string $element The piece of information to return: 'dirname', 'basename', 'extension', or 'filename'

Returns

The file's dirname, basename, extension and filename

::hookDeletedMap() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Hooks a file/directory into the deleted backtrace map entry for that filename

Since the value is returned by reference, all objects that represent this file/directory always see the same backtrace.

Signature

mixed &hookDeletedMap( string $file )

Parameters

string $file The name of the file or directory

Returns

Will return NULL if no match, or the backtrace array if a match occurs

::hookFilenameMap() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Hooks a file/directory name to the filename map

Since the value is returned by reference, all objects that represent this file/directory will always be update on a rename.

Signature

mixed &hookFilenameMap( string $file )

Parameters

string $file The name of the file or directory

Returns

Will return NULL if no match, or the exception object if a match occurs

::isInsideTransaction() public

Indicates if a transaction is in progress

Signature

void isInsideTransaction( )

::makeUniqueName() public

Returns a unique name for a file

Signature

string makeUniqueName( string $file, string $new_extension=NULL )

Parameters

string $file The filename to check
string $new_extension The new extension for the filename, should not include .

Returns

The unique file name

::makeURLSafe() public

Changes a filename to be safe for URLs by making it all lower case and changing everything but letters, numers, - and . to _

Signature

string makeURLSafe( string $filename )

Parameters

string $filename The filename to clean up

Returns

The cleaned up filename

::recordAppend() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Stores what data has been added to a file so it can be removed if there is a rollback

Signature

void recordAppend( fFile $file, string $data )

Parameters

fFile $file The file that is being written to
string $data The data being appended to the file

::recordCreate() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Keeps a record of created files so they can be deleted up in case of a rollback

Signature

void recordCreate( object $object )

Parameters

object $object The new file or directory to get rid of on rollback

::recordDelete() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Keeps track of file and directory names to delete when a transaction is committed

Signature

void recordDelete( fFile|fDirectory $object )

Parameters

fFile|fDirectory $object The filesystem object to delete

::recordDuplicate() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Keeps a record of duplicated files so they can be cleaned up in case of a rollback

Signature

void recordDuplicate( fFile $file )

Parameters

fFile $file The duplicate file to get rid of on rollback

::recordRename() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Keeps a temp file in place of the old filename so the file can be restored during a rollback

Signature

void recordRename( string $old_name, string $new_name )

Parameters

string $old_name The old file or directory name
string $new_name The new file or directory name

::recordWrite() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Keeps backup copies of files so they can be restored if there is a rollback

Signature

void recordWrite( fFile $file )

Parameters

fFile $file The file that is being written to

::reset() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Resets the configuration of the class

Signature

void reset( )

::rollback() public

Rolls back a filesystem transaction, it is safe to rollback when no transaction is in progress

Signature

void rollback( )

::translateToWebPath() public

Takes a filesystem path and translates it to a web path using the rules added

Signature

string translateToWebPath( string $path )

Parameters

string $path The path to translate

Returns

The filesystem path translated to a web path

::updateDeletedMap() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Updates the deleted backtrace for a file or directory

Signature

void updateDeletedMap( string $file, array $backtrace )

Parameters

string $file A file or directory name, directories should end in / or \
array $backtrace The backtrace for this file/directory

::updateFilenameMap() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Updates the filename map, causing all objects representing a file/directory to be updated

Signature

void updateFilenameMap( string $existing_filename, string $new_filename )

Parameters

string $existing_filename The existing filename
string $new_filename The new filename

::updateFilenameMapForDirectory() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Updates the filename map recursively, causing all objects representing a directory to be updated

Also updates all files and directories in the specified directory to the new paths.

Signature

void updateFilenameMapForDirectory( string $existing_dirname, string $new_dirname )

Parameters

string $existing_dirname The existing directory name
string $new_dirname The new dirname