FileDatasource

File based datasource.

 class Winter\Storm\Halcyon\Datasource\FileDatasource
extends Winter\Storm\Halcyon\Datasource\Datasource
implements 
    Winter\Storm\Halcyon\Datasource\DatasourceInterface

Traits

Trait Description
Emitter

Adds event related features to any class.

Properties

protected $basePath : string

The local path where the datasource can be found.

protected $files : Winter\Storm\Filesystem\Filesystem

The filesystem instance.

protected $maxDepth : integer

The maximum depth of the filesystem to scan, defaulting to 1 subdirectory.

protected $resolvedBasePaths : array

Resolved path map.

protected $emitterEventCollection : array

Inherited from Datasource

Collection of registered events.

protected $emitterEventSorted : array

Inherited from Datasource

Sorted collection of events.

protected $emitterSingleEventCollection : array

Inherited from Datasource

Collection of registered events to be fired once only.

protected $forceDeleting : bool

Inherited from Datasource

Indicates if the record is currently being force deleted.

protected $postProcessor : Winter\Storm\Halcyon\Processors\Processor

Inherited from Datasource

The query post processor implementation.

Methods

public __construct (string $basePath, Winter\Storm\Filesystem\Filesystem $files) : void

Create a new datasource instance.

Parameters
Property Description
$basePath
string
$files
Winter\Storm\Filesystem\Filesystem
Returns
void

public delete (string $dirName, string $fileName, string $extension) : bool

Runs a delete statement against the datasource.

Parameters
Property Description
$dirName
string

The directory in which the model is stored.

$fileName
string

The filename of the model.

$extension
string

The file extension of the model.

Returns
bool

If the delete operation completed successfully.

public getAvailablePaths () : array

Get all available paths within this datasource.

This method returns an array, with all available paths as the key, and a boolean that represents whether the path can be handled or modified.

Example:

[
    'path/to/file.md' => true, // (this path is available, and can be handled)
    'path/to/file2.md' => false // (this path is available, but cannot be handled)
]
Returns
array

An array of available paths alongside whether they can be handled.

public getBasePath () : string

Returns the base path for this datasource.

Returns
string

public getPathsCacheKey () : string

Gets the prefix of the cache keys.

This is based off a prefix including the base path for the model.

Returns
string

The cache key prefix.

public insert (string $dirName, string $fileName, string $extension, string $content) : integer

Creates a new Halcyon model (template).

Parameters
Property Description
$dirName
string

The directory in which the model is stored.

$fileName
string

The filename of the model.

$extension
string

The file extension of the model.

$content
string

The content to store for the model.

Returns
integer

The filesize of the created model.

public lastModified (string $dirName, string $fileName, string $extension) : integer | null

Returns the last modified date of a model (template).

Parameters
Property Description
$dirName
string

The directory in which the model is stored.

$fileName
string

The filename of the model.

$extension
string

The file extension of the model.

Returns
integer | null

The last modified time as a timestamp, or null if the object doesn't exist.

public select (string $dirName, array $options = []) : array

Returns all Halcyon models (templates) within a given directory.

You can provide multiple options with the $options property, in order to filter the retrieved records:

  • columns: Only retrieve certain columns. Must be an array with any combination of fileName, mtime and content.
  • extensions: Defines the accepted extensions as an array. Eg: ['htm', 'md', 'twig']
  • fileMatch: Defines a glob string to match filenames against. Eg: '*gr[ae]y'
  • orders: Not implemented
  • limit: Not implemented
  • offset: Not implemented
Parameters
Property Description
$dirName
string

The directory in which the model is stored.

$options
array

Defines the options for this query.

Returns
array

An array of models found, with the columns defined as per the columns parameter for $options.

public selectOne (string $dirName, string $fileName, string $extension) : array | null

Returns a single Halcyon model (template).

Parameters
Property Description
$dirName
string

The directory in which the model is stored.

$fileName
string

The filename of the model.

$extension
string

The file extension of the model.

Returns
array | null

An array of template data (fileName, mtime and content), or null if the model does not exist.

public update (string $dirName, string $fileName, string $extension, string $content, $oldFileName = null, $oldExtension = null) : integer

Updates an existing Halcyon model (template).

Parameters
Property Description
$dirName
string

The directory in which the model is stored.

$fileName
string

The filename of the model.

$extension
string

The file extension of the model.

$content
string

The content to store for the model.

$oldFileName
mixed

Used for renaming templates. If specified, this will delete the "old" path.

$oldExtension
mixed

Used for renaming templates. If specified, this will delete the "old" path.

Returns
integer

The filesize of the updated model.

protected makeDirectoryPath (string $dirName, string $relativePath = "") : string

Helper to generate the absolute path to the provided relative path within the provided directory

Parameters
Property Description
$dirName
string
$relativePath
string

Optional, if not provided the absolute path to the provided directory will be returned

Returns
string

protected makeFilePath (string $dirName, string $fileName, string $extension) : string

Helper method to make the full file path to the model.

Parameters
Property Description
$dirName
string

The directory in which the model is stored.

$fileName
string

The filename of the model.

$extension
string

The file extension of the model.

Returns
string

The full file path.

protected validateDirectoryForSave (string $dirName, string $fileName, string $extension) : void

Ensure the requested file can be created in the requested directory.

Parameters
Property Description
$dirName
string

The directory in which the model is stored.

$fileName
string

The filename of the model.

$extension
string

The file extension of the model.

Returns
void

public bindEvent (string | Closure | QueuedClosure $event, mixed $callback = null, integer $priority) : self

Inherited from Datasource

Create a new event binding.

Parameters
Property Description
$event
string | Closure | QueuedClosure
$callback
mixed

when the third parameter is omitted and a Closure or QueuedClosure is provided this parameter is used as an integer this is used as priority variable

$priority
integer
Returns
self

public bindEventOnce (string | Closure | QueuedClosure $event, QueuedClosure | Closure | null $callback = null) : self

Inherited from Datasource

Create a new event binding that fires once only

Parameters
Property Description
$event
string | Closure | QueuedClosure
$callback
QueuedClosure | Closure | null

When a Closure or QueuedClosure is provided as the first parameter this parameter can be omitted

Returns
self

public fireEvent (string $event, array $params = [], boolean $halt = false) : array | mixed | null

Inherited from Datasource

Fire an event and call the listeners.

Parameters
Property Description
$event
string

Event name

$params
array

Event parameters

$halt
boolean

Halt after first non-null result

Returns
array | mixed | null

If halted, the first non-null result. If not halted, an array of event results. Returns null if no listeners returned a result.

public forceDelete (string $dirName, string $fileName, string $extension) : bool

Inherited from Datasource

Runs a delete statement against the datasource, forcing the complete removal of the model (template).

Parameters
Property Description
$dirName
string

The directory in which the model is stored.

$fileName
string

The filename of the model.

$extension
string

The file extension of the model.

Returns
bool

If the delete operation completed successfully.

public getPostProcessor () : Winter\Storm\Halcyon\Processors\Processor

Inherited from Datasource

Get the query post processor used by the connection.

Returns
Winter\Storm\Halcyon\Processors\Processor

public makeCacheKey (string $name = "") : string

Inherited from Datasource

Generate a cache key unique to this datasource.

Parameters
Property Description
$name
string

The name of the key.

Returns
string

The hashed key.

public unbindEvent (string | array | object $event = null) : self

Inherited from Datasource

Destroys an event binding.

Parameters
Property Description
$event
string | array | object

Event to destroy

Returns
self

protected emitterEventSortEvents (string $eventName) : void

Inherited from Datasource

Sort the listeners for a given event by priority.

Parameters
Property Description
$eventName
string
Returns
void

protected parseEventAndPayload (mixed $event, mixed $payload = null) : array

Inherited from Datasource

Parse the given event and payload and prepare them for dispatching.

Parameters
Property Description
$event
mixed
$payload
mixed
Returns
array
Copyright © 2024 Winter CMS