class TestAssetStore extends FlysystemAssetStore implements TestOnly (View source)

Allows you to mock a backend store in a custom directory beneath assets.

Only to be used for mocking test fixtures

Traits

Provides extensions to this object to integrate it with standard config API methods.

Allows an object to have extensions applied to it.

Allows an object to declare a set of custom methods

Constants

GRANTS_SESSION

Session key to use for user grants

Properties

static private array $extensions

An array of extension names and parameters to be applied to this object upon construction.

from  Extensible
static private bool $legacy_filenames

Enable to use legacy filename behaviour (omits hash and uses the natural filename).

from  FlysystemAssetStore
static private bool $keep_empty_dirs

Flag if empty folders are allowed.

from  FlysystemAssetStore
static private int $denied_response_code

Enable disclosure of secure assets

static private int $missing_response_code

Set HTTP error code to use for missing secure assets

from  FlysystemAssetStore
static private int $redirect_response_code

Define the HTTP Response code for request that should be temporarily redirected to a different URL. Defaults to 302.

from  FlysystemAssetStore
static private int $permanent_redirect_response_code

Define the HTTP Response code for request that should be permanently redirected to a different URL. Defaults to 301.

from  FlysystemAssetStore
static private array $file_response_headers

Custom headers to add to all custom file responses

from  FlysystemAssetStore
static null|bool $seekable_override

Set to true|false to override all isSeekableStream calls

static string $basedir

Base dir of current file

Methods

static Config_ForClass
config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

mixed
stat(string $name) deprecated

Get inherited config value

mixed
uninherited(string $name)

Gets the uninherited value for the given config option

$this
set_stat(string $name, mixed $value) deprecated

Update the config value for a given property

mixed
__call(string $method, array $arguments)

Attempts to locate and call a method dynamically added to a class at runtime if a default cannot be located

bool
hasMethod(string $method)

Return TRUE if a method exists on this object

array
allMethodNames(bool $custom = false)

Return the names of all the methods available on this object

static bool
add_extension(string $classOrExtension, string $extension = null)

Add an extension to a specific class.

static 
remove_extension(string $extension)

Remove an extension from a class.

static array
get_extensions(string $class = null, bool $includeArgumentString = false)

No description

static array|null
get_extra_config_sources(string $class = null)

Get extra config sources for this class

static bool
has_extension(string $classOrExtension, string $requiredExtension = null, bool $strict = false)

Return TRUE if a class has a specified extension.

array
invokeWithExtensions(string $method, mixed $a1 = null, mixed $a2 = null, mixed $a3 = null, mixed $a4 = null, mixed $a5 = null, mixed $a6 = null, mixed $a7 = null)

Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge all results into an array

array
extend(string $method, mixed $a1 = null, mixed $a2 = null, mixed $a3 = null, mixed $a4 = null, mixed $a5 = null, mixed $a6 = null, mixed $a7 = null)

Run the given function on all of this object's extensions. Note that this method originally returned void, so if you wanted to return results, you're hosed

Extension|null
getExtensionInstance(string $extension)

Get an extension instance attached to this object by name.

bool
hasExtension(string $extension)

Returns TRUE if this object instance has a specific extension applied in {@link $extension_instances}. Extension instances are initialized at constructor time, meaning if you use {@link add_extension()} afterwards, the added extension will just be added to new instances of the extended class. Use the static method {@link has_extension()} to check if a class (not an instance) has a specific extension.

getExtensionInstances()

Get all extension instances for this specific object instance.

$this
setPublicFilesystem(Filesystem $filesystem)

Assign new flysystem backend

Filesystem
getPublicFilesystem()

Get the currently assigned flysystem backend

$this
setProtectedFilesystem(Filesystem $filesystem)

Assign filesystem to use for non-public files

Filesystem
getProtectedFilesystem()

Get filesystem to use for non-public files

array
getCapabilities()

Return list of feature capabilities of this backend as an array.

string
getVisibility(string $filename, string $hash)

Determine visibility of the given file

resource
getAsStream(string $filename, string $hash, string|null $variant = null)

Get a stream for this file

string
getAsString(string $filename, string $hash, string|null $variant = null)

Get contents of a given file

string
getAsURL(string $filename, string $hash, string|null $variant = null, bool $grant = true)

Get the url for the file

array
setFromLocalFile(string $path, string $filename = null, string $hash = null, string $variant = null, array $config = [])

Assign a local file to the backend.

array
setFromString(string $data, string $filename, string $hash = null, string $variant = null, array $config = [])

Assign a set of data to the backend

array
setFromStream(resource $stream, string $filename, string $hash = null, string $variant = null, array $config = [])

Assign a stream to the backend

bool
delete(string $filename, string $hash)

Delete a file (and all variants) identified by the given filename and hash

string
rename(string $filename, string $hash, string $newName)

Rename a file (and all variants) to a new filename

string|null
copy(string $filename, string $hash, string $newName)

Copy a file (and all variants) to a new filename

publish(string $filename, string $hash)

Publicly expose the file (and all variants) identified by the given filename and hash

swapPublish($filename, $hash)

Similar to publish, only any existing files that would be overriden by publishing will be moved back to the protected store.

protect(string $filename, string $hash)

Protect a file (and all variants) from public access, identified by the given filename and hash.

grant(string $filename, string $hash)

Ensures that access to the specified protected file is granted for the current user.

revoke(string $filename, string $hash)

Revoke access to the given file for the current user.

bool
canView(string $filename, string $hash)

Check if the current user can view the given file.

array|null
getMetadata(string $filename, string $hash, string|null $variant = null)

Get metadata for this file, if available

string
getMimeType(string $filename, string $hash, string|null $variant = null)

Get mime type of this file

bool
exists(string $filename, string $hash, string|null $variant = null)

Determine if a file exists with the given tuple

static 
flush()

Ensure each adapter re-generates its own server configuration files

getResponseFor(string $asset)

Generate a custom HTTP response for a request to a given asset, identified by a path.

normalisePath($fileID)

No description

normalise($filename, $hash)

No description

static 
activate(string $basedir)

Set this store as the new asset backend

static string
base_path()

Get absolute path to basedir

static 
reset()

Reset defaults for this store

static string
getLocalPath(AssetContainer $asset, bool $forceProtected = false, bool $relative = false)

Helper method to get local filesystem path for this file

cleanFilename($filename)

No description

getFileID($filename, $hash, $variant = null)

No description

parseFileID($fileID)

No description

getOriginalFilename($fileID)

No description

getFilesystemFor($fileID)

No description

removeVariant($fileID)

No description

getDefaultConflictResolution($variant)

No description

isGranted($fileID)

Allows testing of grant status

Details

static Config_ForClass config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

Return Value

Config_ForClass

mixed stat(string $name) deprecated

deprecated 5.0 Use ->config()->get() instead

Get inherited config value

Parameters

string $name

Return Value

mixed

mixed uninherited(string $name)

Gets the uninherited value for the given config option

Parameters

string $name

Return Value

mixed

$this set_stat(string $name, mixed $value) deprecated

deprecated 5.0 Use ->config()->set() instead

Update the config value for a given property

Parameters

string $name
mixed $value

Return Value

$this

mixed __call(string $method, array $arguments)

Attempts to locate and call a method dynamically added to a class at runtime if a default cannot be located

You can add extra methods to a class using {@link Extensions}, {@link Object::createMethod()} or {@link Object::addWrapperMethod()}

Parameters

string $method
array $arguments

Return Value

mixed

Exceptions

BadMethodCallException

bool hasMethod(string $method)

Return TRUE if a method exists on this object

This should be used rather than PHP's inbuild method_exists() as it takes into account methods added via extensions

Parameters

string $method

Return Value

bool

array allMethodNames(bool $custom = false)

Return the names of all the methods available on this object

Parameters

bool $custom

include methods added dynamically at runtime

Return Value

array

static bool add_extension(string $classOrExtension, string $extension = null)

Add an extension to a specific class.

The preferred method for adding extensions is through YAML config, since it avoids autoloading the class, and is easier to override in more specific configurations.

As an alternative, extensions can be added to a specific class directly in the {@link Object::$extensions} array. See {@link SiteTree::$extensions} for examples. Keep in mind that the extension will only be applied to new instances, not existing ones (including all instances created through {@link singleton()}).

Parameters

string $classOrExtension

Class that should be extended - has to be a subclass of {@link Object}

string $extension

Subclass of {@link Extension} with optional parameters as a string, e.g. "Versioned" or "Translatable('Param')"

Return Value

bool

Flag if the extension was added

See also

http://doc.silverstripe.org/framework/en/trunk/reference/dataextension

static remove_extension(string $extension)

Remove an extension from a class.

Note: This will not remove extensions from parent classes, and must be called directly on the class assigned the extension.

Keep in mind that this won't revert any datamodel additions of the extension at runtime, unless its used before the schema building kicks in (in your _config.php). Doesn't remove the extension from any {@link Object} instances which are already created, but will have an effect on new extensions. Clears any previously created singletons through {@link singleton()} to avoid side-effects from stale extension information.

Parameters

string $extension

class name of an {@link Extension} subclass, without parameters

static array get_extensions(string $class = null, bool $includeArgumentString = false)

Parameters

string $class

If omitted, will get extensions for the current class

bool $includeArgumentString

Include the argument string in the return array, FALSE would return array("Versioned"), TRUE returns array("Versioned('Stage','Live')").

Return Value

array

Numeric array of either {@link DataExtension} class names, or eval'ed class name strings with constructor arguments.

static array|null get_extra_config_sources(string $class = null)

Get extra config sources for this class

Parameters

string $class

Name of class. If left null will return for the current class

Return Value

array|null

static bool has_extension(string $classOrExtension, string $requiredExtension = null, bool $strict = false)

Return TRUE if a class has a specified extension.

This supports backwards-compatible format (static Object::has_extension($requiredExtension)) and new format ($object->has_extension($class, $requiredExtension))

Parameters

string $classOrExtension

Class to check extension for, or the extension name to check if the second argument is null.

string $requiredExtension

If the first argument is the parent class, this is the extension to check. If left null, the first parameter will be treated as the extension.

bool $strict

if the extension has to match the required extension and not be a subclass

Return Value

bool

Flag if the extension exists

array invokeWithExtensions(string $method, mixed $a1 = null, mixed $a2 = null, mixed $a3 = null, mixed $a4 = null, mixed $a5 = null, mixed $a6 = null, mixed $a7 = null)

Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge all results into an array

Parameters

string $method

the method name to call

mixed $a1
mixed $a2
mixed $a3
mixed $a4
mixed $a5
mixed $a6
mixed $a7

Return Value

array

List of results with nulls filtered out

array extend(string $method, mixed $a1 = null, mixed $a2 = null, mixed $a3 = null, mixed $a4 = null, mixed $a5 = null, mixed $a6 = null, mixed $a7 = null)

Run the given function on all of this object's extensions. Note that this method originally returned void, so if you wanted to return results, you're hosed

Currently returns an array, with an index resulting every time the function is called. Only adds returns if they're not NULL, to avoid bogus results from methods just defined on the parent extension. This is important for permission-checks through extend, as they use min() to determine if any of the returns is FALSE. As min() doesn't do type checking, an included NULL return would fail the permission checks.

The extension methods are defined during {@link __construct()} in {@link defineMethods()}.

Parameters

string $method

the name of the method to call on each extension

mixed $a1
mixed $a2
mixed $a3
mixed $a4
mixed $a5
mixed $a6
mixed $a7

Return Value

array

Extension|null getExtensionInstance(string $extension)

Get an extension instance attached to this object by name.

Parameters

string $extension

Return Value

Extension|null

bool hasExtension(string $extension)

Returns TRUE if this object instance has a specific extension applied in {@link $extension_instances}. Extension instances are initialized at constructor time, meaning if you use {@link add_extension()} afterwards, the added extension will just be added to new instances of the extended class. Use the static method {@link has_extension()} to check if a class (not an instance) has a specific extension.

Caution: Don't use singleton()->hasExtension() as it will give you inconsistent results based on when the singleton was first accessed.

Parameters

string $extension

Classname of an {@link Extension} subclass without parameters

Return Value

bool

Extension[] getExtensionInstances()

Get all extension instances for this specific object instance.

See {@link get_extensions()} to get all applied extension classes for this class (not the instance).

This method also provides lazy-population of the extension_instances property.

Return Value

Extension[]

Map of {@link DataExtension} instances, keyed by classname.

$this setPublicFilesystem(Filesystem $filesystem)

Assign new flysystem backend

Parameters

Filesystem $filesystem

Return Value

$this

Exceptions

InvalidArgumentException

Filesystem getPublicFilesystem()

Get the currently assigned flysystem backend

Return Value

Filesystem

Exceptions

LogicException

$this setProtectedFilesystem(Filesystem $filesystem)

Assign filesystem to use for non-public files

Parameters

Filesystem $filesystem

Return Value

$this

Exceptions

InvalidArgumentException

Filesystem getProtectedFilesystem()

Get filesystem to use for non-public files

Return Value

Filesystem

Exceptions

LogicException

array getCapabilities()

Return list of feature capabilities of this backend as an array.

Array keys will be the options supported by $config, and the values will be the list of accepted values for each option (or true if any value is allowed).

Return Value

array

string getVisibility(string $filename, string $hash)

Determine visibility of the given file

Parameters

string $filename
string $hash

Return Value

string

one of values defined by the constants VISIBILITY_PROTECTED or VISIBILITY_PUBLIC, or null if the file does not exist

resource getAsStream(string $filename, string $hash, string|null $variant = null)

Get a stream for this file

Parameters

string $filename

Filename (not including assets)

string $hash

sha1 hash of the file content. If a variant is requested, this is the hash of the file before it was modified.

string|null $variant

Optional variant string for this file

Return Value

resource

Data stream

string getAsString(string $filename, string $hash, string|null $variant = null)

Get contents of a given file

Parameters

string $filename

Filename (not including assets)

string $hash

sha1 hash of the file content. If a variant is requested, this is the hash of the file before it was modified.

string|null $variant

Optional variant string for this file

Return Value

string

Data from the file.

string getAsURL(string $filename, string $hash, string|null $variant = null, bool $grant = true)

Get the url for the file

Parameters

string $filename

Filename (not including assets)

string $hash

sha1 hash of the file content. If a variant is requested, this is the hash of the file before it was modified.

string|null $variant

Optional variant string for this file

bool $grant

Ensures that the url for any protected assets is granted for the current user. If set to true, and the file is currently in protected mode, the asset store will ensure the returned URL is accessible for the duration of the current session / user. This will have no effect if the file is in published mode. This will not grant access to users other than the owner of the current session.

Return Value

string

public url to this resource

array setFromLocalFile(string $path, string $filename = null, string $hash = null, string $variant = null, array $config = [])

Assign a local file to the backend.

Parameters

string $path

Absolute filesystem path to file

string $filename

Optional path to ask the backend to name as. Will default to the filename of the $path, excluding directories.

string $hash

Hash of original file, if storing a variant.

string $variant

Name of variant, if storing a variant.

array $config

Write options. {see \SilverStripe\Assets\Storage\AssetStore}

Return Value

array

Tuple associative array (Filename, Hash, Variant) Unless storing a variant, the hash will be calculated from the local file content.

array setFromString(string $data, string $filename, string $hash = null, string $variant = null, array $config = [])

Assign a set of data to the backend

Parameters

string $data

Raw binary/text content

string $filename

Name for the resulting file

string $hash

Hash of original file, if storing a variant.

string $variant

Name of variant, if storing a variant.

array $config

Write options. {see \SilverStripe\Assets\Storage\AssetStore}

Return Value

array

Tuple associative array (Filename, Hash, Variant) Unless storing a variant, the hash will be calculated from the given data.

array setFromStream(resource $stream, string $filename, string $hash = null, string $variant = null, array $config = [])

Assign a stream to the backend

Parameters

resource $stream

Streamable resource

string $filename

Name for the resulting file

string $hash

Hash of original file, if storing a variant.

string $variant

Name of variant, if storing a variant.

array $config

Write options. {see \SilverStripe\Assets\Storage\AssetStore}

Return Value

array

Tuple associative array (Filename, Hash, Variant) Unless storing a variant, the hash will be calculated from the raw stream.

bool delete(string $filename, string $hash)

Delete a file (and all variants) identified by the given filename and hash

Parameters

string $filename
string $hash

Return Value

bool

Flag if a file was deleted

string rename(string $filename, string $hash, string $newName)

Rename a file (and all variants) to a new filename

Parameters

string $filename
string $hash
string $newName

Return Value

string

Updated Filename, or null if rename failed

string|null copy(string $filename, string $hash, string $newName)

Copy a file (and all variants) to a new filename

Parameters

string $filename
string $hash
string $newName

Return Value

string|null

Updated Filename, or null if copy failed

publish(string $filename, string $hash)

Publicly expose the file (and all variants) identified by the given filename and hash

Parameters

string $filename

Filename (not including assets)

string $hash

sha1 hash of the file content.

swapPublish($filename, $hash)

Similar to publish, only any existing files that would be overriden by publishing will be moved back to the protected store.

Parameters

$filename
$hash

protect(string $filename, string $hash)

Protect a file (and all variants) from public access, identified by the given filename and hash.

A protected file can be granted access to users on a per-session or per-user basis as response to any future invocations of {see \SilverStripe\Assets\Storage\grant()} or {see \SilverStripe\Assets\Storage\getAsURL()} with $grant = true

Parameters

string $filename

Filename (not including assets)

string $hash

sha1 hash of the file content.

grant(string $filename, string $hash)

Ensures that access to the specified protected file is granted for the current user.

If this file is currently in protected mode, the asset store will ensure the returned asset for the duration of the current session / user. This will have no effect if the file is in published mode. This will not grant access to users other than the owner of the current session. Does not require a member to be logged in.

Parameters

string $filename
string $hash

revoke(string $filename, string $hash)

Revoke access to the given file for the current user.

Note: This will have no effect if the given file is public

Parameters

string $filename
string $hash

bool canView(string $filename, string $hash)

Check if the current user can view the given file.

Parameters

string $filename
string $hash

Return Value

bool

True if the file is verified and grants access to the current session / user.

array|null getMetadata(string $filename, string $hash, string|null $variant = null)

Get metadata for this file, if available

Parameters

string $filename

Filename (not including assets)

string $hash

sha1 hash of the file content. If a variant is requested, this is the hash of the file before it was modified.

string|null $variant

Optional variant string for this file

Return Value

array|null

File information, or null if no metadata available

string getMimeType(string $filename, string $hash, string|null $variant = null)

Get mime type of this file

Parameters

string $filename

Filename (not including assets)

string $hash

sha1 hash of the file content. If a variant is requested, this is the hash of the file before it was modified.

string|null $variant

Optional variant string for this file

Return Value

string

Mime type for this file

bool exists(string $filename, string $hash, string|null $variant = null)

Determine if a file exists with the given tuple

Parameters

string $filename

Filename (not including assets)

string $hash

sha1 hash of the file content. If a variant is requested, this is the hash of the file before it was modified.

string|null $variant

Optional variant string for this file

Return Value

bool

Flag as to whether the file exists

static flush()

Ensure each adapter re-generates its own server configuration files

HTTPResponse getResponseFor(string $asset)

Generate a custom HTTP response for a request to a given asset, identified by a path.

Parameters

string $asset

Asset path name, omitting any leading 'assets'

Return Value

HTTPResponse

Exceptions

HTTPResponse_Exception

normalisePath($fileID)

Parameters

$fileID

normalise($filename, $hash)

Parameters

$filename
$hash

static activate(string $basedir)

Set this store as the new asset backend

Parameters

string $basedir

Basedir to store assets, which will be placed beneath 'assets' folder

static string base_path()

Get absolute path to basedir

Return Value

string

static reset()

Reset defaults for this store

static string getLocalPath(AssetContainer $asset, bool $forceProtected = false, bool $relative = false)

Helper method to get local filesystem path for this file

Parameters

AssetContainer $asset
bool $forceProtected
bool $relative

Return path relative to asset store root.

Return Value

string

cleanFilename($filename)

Parameters

$filename

getFileID($filename, $hash, $variant = null)

Parameters

$filename
$hash
$variant

parseFileID($fileID)

Parameters

$fileID

getOriginalFilename($fileID)

Parameters

$fileID

getFilesystemFor($fileID)

Parameters

$fileID

removeVariant($fileID)

Parameters

$fileID

getDefaultConflictResolution($variant)

Parameters

$variant

isGranted($fileID)

Allows testing of grant status

Parameters

$fileID