class ResourceManager (View source)

The ResourceManager

Constants

DEFAULT_STATIC_COLLECTION_NAME

Names of the default collections for static and persistent resources.

DEFAULT_PERSISTENT_COLLECTION_NAME

PUBLIC_RESSOURCE_REGEXP

Properties

protected ObjectManagerInterface $objectManager
protected LoggerInterface $logger
protected ResourceRepository $resourceRepository
protected PersistenceManagerInterface $persistenceManager
protected array $settings
protected Environment $environment
protected StorageInterface[] $storages
protected TargetInterface[] $targets
protected CollectionInterface[] $collections
protected bool $initialized

Methods

void
injectSettings(array $settings)

Injects the settings of this package

void
injectLogger(LoggerInterface $logger)

Injects the (system) logger based on PSR-3.

void
initialize()

Initializes the ResourceManager by parsing the related configuration and registering the resource stream wrapper.

importResource(string|resource $source, string $collectionName = ResourceManager::DEFAULT_PERSISTENT_COLLECTION_NAME, string $forcedPersistenceObjectIdentifier = null)

Imports a resource (file) from the given location as a persistent resource.

importResourceFromContent(string $content, string $filename, string $collectionName = ResourceManager::DEFAULT_PERSISTENT_COLLECTION_NAME, string $forcedPersistenceObjectIdentifier = null)

Imports the given content passed as a string as a new persistent resource.

importUploadedResource(array $uploadInfo, string $collectionName = self::DEFAULT_PERSISTENT_COLLECTION_NAME)

Imports a resource (file) from the given upload info array as a persistent resource.

getResourceBySha1(string $sha1Hash)

Returns the resource object identified by the given SHA1 hash over the content, or NULL if no such PersistentResource object is known yet.

resource|false
getStreamByResource(PersistentResource $resource)

Returns a stream handle of the given persistent resource which allows for opening / copying the resource's data. Note that this stream handle may only be used read-only.

getImportedResources()

Returns an object storage with all resource objects which have been imported by the ResourceManager during this script call. Each resource comes with an array of additional information about its import.

bool
deleteResource(PersistentResource $resource, bool $unpublishResource = true)

Deletes the given PersistentResource from the ResourceRepository and, if the storage data is no longer used in another PersistentResource object, also deletes the data from the storage.

string|false
getPublicPersistentResourceUri(PersistentResource $resource)

Returns the web accessible URI for the given resource object

string
getPublicPersistentResourceUriByHash(string $resourceHash, string $collectionName = self::DEFAULT_PERSISTENT_COLLECTION_NAME)

Returns the web accessible URI for the resource object specified by the given SHA1 hash.

string
getPublicPackageResourceUri(string $packageKey, string $relativePathAndFilename)

Returns the public URI for a static resource provided by the specified package and in the given path below the package's resources directory.

string
getPublicPackageResourceUriByPath(string $path)

Returns the public URI for a static resource provided by the public package

array
getPackageAndPathByPublicPath(string $path)

Return the package key and the relative path and filename from the given resource path

getStorage(string $storageName)

Returns a Storage instance by the given name

getCollection(string $collectionName)

Returns a Collection instance by the given name

getCollections()

Returns an array of currently known Collection instances

getCollectionsByStorage(StorageInterface $storage)

Returns an array of Collection instances which use the given storage

void
shutdownObject()

Checks if recently imported resources really have been persisted - and if not, removes its data from the respective storage.

void
initializeStorages()

Initializes the Storage objects according to the current settings

void
initializeTargets()

Initializes the Target objects according to the current settings

void
initializeCollections()

Initializes the Collection objects according to the current settings

array
prepareUploadedFileForImport(array $uploadInfo)

Prepare an uploaded file to be imported as resource object. Will check the validity of the file, move it outside of upload folder if open_basedir is enabled and check the filename.

Details

void injectSettings(array $settings)

Injects the settings of this package

Parameters

array $settings

Return Value

void

void injectLogger(LoggerInterface $logger)

Injects the (system) logger based on PSR-3.

Parameters

LoggerInterface $logger

Return Value

void

protected void initialize()

Initializes the ResourceManager by parsing the related configuration and registering the resource stream wrapper.

Return Value

void

PersistentResource importResource(string|resource $source, string $collectionName = ResourceManager::DEFAULT_PERSISTENT_COLLECTION_NAME, string $forcedPersistenceObjectIdentifier = null)

Imports a resource (file) from the given location as a persistent resource.

On a successful import this method returns a PersistentResource object representing the newly imported persistent resource and automatically publishes it to the configured publication target.

Parameters

string|resource $source

A URI (can therefore also be a path and filename) or a PHP resource stream(!) pointing to the PersistentResource to import

string $collectionName

Name of the collection this new resource should be added to. By default the standard collection for persistent resources is used.

string $forcedPersistenceObjectIdentifier

INTERNAL: Force the object identifier for this resource to the given UUID

Return Value

PersistentResource

A resource object representing the imported resource

Exceptions

Exception

PersistentResource importResourceFromContent(string $content, string $filename, string $collectionName = ResourceManager::DEFAULT_PERSISTENT_COLLECTION_NAME, string $forcedPersistenceObjectIdentifier = null)

Imports the given content passed as a string as a new persistent resource.

The given content typically is binary data or a text format. On a successful import this method returns a PersistentResource object representing the imported content and automatically publishes it to the configured publication target.

The specified filename will be used when presenting the resource to a user. Its file extension is important because the resource management will derive the IANA Media Type from it.

Parameters

string $content

The binary content to import

string $filename

The filename to use for the newly generated resource

string $collectionName

Name of the collection this new resource should be added to. By default the standard collection for persistent resources is used.

string $forcedPersistenceObjectIdentifier

INTERNAL: Force the object identifier for this resource to the given UUID

Return Value

PersistentResource

A resource object representing the imported resource

Exceptions

Exception

PersistentResource importUploadedResource(array $uploadInfo, string $collectionName = self::DEFAULT_PERSISTENT_COLLECTION_NAME)

Imports a resource (file) from the given upload info array as a persistent resource.

On a successful import this method returns a PersistentResource object representing the newly imported persistent resource.

Parameters

array $uploadInfo

An array detailing the resource to import (expected keys: name, tmp_name)

string $collectionName

Name of the collection this uploaded resource should be added to

Return Value

PersistentResource

A resource object representing the imported resource

Exceptions

Exception

PersistentResource|null getResourceBySha1(string $sha1Hash)

Returns the resource object identified by the given SHA1 hash over the content, or NULL if no such PersistentResource object is known yet.

Parameters

string $sha1Hash

The SHA1 identifying the data the PersistentResource stands for

Return Value

PersistentResource|null

resource|false getStreamByResource(PersistentResource $resource)

Returns a stream handle of the given persistent resource which allows for opening / copying the resource's data. Note that this stream handle may only be used read-only.

Parameters

PersistentResource $resource

The resource to retrieve the stream for

Return Value

resource|false

The resource stream or false if the stream could not be obtained

SplObjectStorage getImportedResources()

Returns an object storage with all resource objects which have been imported by the ResourceManager during this script call. Each resource comes with an array of additional information about its import.

Example for a returned object storage:

$resource1 => array('originalFilename' => 'Foo.txt'), $resource2 => array('originalFilename' => 'Bar.txt'), ...

Return Value

SplObjectStorage

bool deleteResource(PersistentResource $resource, bool $unpublishResource = true)

Deletes the given PersistentResource from the ResourceRepository and, if the storage data is no longer used in another PersistentResource object, also deletes the data from the storage.

This method will also remove the PersistentResource object from the (internal) ResourceRepository.

Parameters

PersistentResource $resource

The resource to delete

bool $unpublishResource

If the resource should be unpublished before deleting it from the storage

Return Value

bool

true if the resource was deleted, otherwise false

string|false getPublicPersistentResourceUri(PersistentResource $resource)

Returns the web accessible URI for the given resource object

Parameters

PersistentResource $resource

The resource object

Return Value

string|false

A URI as a string or false if the collection of the resource is not found

string getPublicPersistentResourceUriByHash(string $resourceHash, string $collectionName = self::DEFAULT_PERSISTENT_COLLECTION_NAME)

Returns the web accessible URI for the resource object specified by the given SHA1 hash.

Parameters

string $resourceHash

The SHA1 hash identifying the resource content

string $collectionName

Name of the collection the resource is part of

Return Value

string

A URI as a string

Exceptions

Exception

string getPublicPackageResourceUri(string $packageKey, string $relativePathAndFilename)

Returns the public URI for a static resource provided by the specified package and in the given path below the package's resources directory.

Parameters

string $packageKey

Package key

string $relativePathAndFilename

A relative path below the "Resources" directory of the package

Return Value

string

string getPublicPackageResourceUriByPath(string $path)

Returns the public URI for a static resource provided by the public package

Parameters

string $path

The ressource path, like resource://Your.Package/Public/Image/Dummy.png

Return Value

string

array getPackageAndPathByPublicPath(string $path)

Return the package key and the relative path and filename from the given resource path

Parameters

string $path

The ressource path, like resource://Your.Package/Public/Image/Dummy.png

Return Value

array

The array contains two value, first the packageKey followed by the relativePathAndFilename

Exceptions

Exception

StorageInterface|null getStorage(string $storageName)

Returns a Storage instance by the given name

Parameters

string $storageName

Name of the storage as defined in the settings

Return Value

StorageInterface|null

CollectionInterface|null getCollection(string $collectionName)

Returns a Collection instance by the given name

Parameters

string $collectionName

Name of the collection as defined in the settings

Return Value

CollectionInterface|null

CollectionInterface[] getCollections()

Returns an array of currently known Collection instances

Return Value

CollectionInterface[]

CollectionInterface[] getCollectionsByStorage(StorageInterface $storage)

Returns an array of Collection instances which use the given storage

Parameters

StorageInterface $storage

Return Value

CollectionInterface[]

void shutdownObject()

Checks if recently imported resources really have been persisted - and if not, removes its data from the respective storage.

Return Value

void

protected void initializeStorages()

Initializes the Storage objects according to the current settings

Return Value

void

Exceptions

Exception

protected void initializeTargets()

Initializes the Target objects according to the current settings

Return Value

void

Exceptions

Exception

protected void initializeCollections()

Initializes the Collection objects according to the current settings

Return Value

void

Exceptions

Exception

protected array prepareUploadedFileForImport(array $uploadInfo)

Prepare an uploaded file to be imported as resource object. Will check the validity of the file, move it outside of upload folder if open_basedir is enabled and check the filename.

Parameters

array $uploadInfo

Return Value

array

Array of string with the two keys "filepath" (the path to get the filecontent from) and "filename" the filename of the originally uploaded file.

Exceptions

Exception