Download the PHP package josbeir/cakephp-filesystem without Composer
On this page you can find all versions of the php package josbeir/cakephp-filesystem. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download josbeir/cakephp-filesystem
More information about josbeir/cakephp-filesystem
Files in josbeir/cakephp-filesystem
Package cakephp-filesystem
Short Description Filesystem plugin for CakePHP 4.0+
License MIT
Informations about the package cakephp-filesystem
Filesystem plugin for CakePHP
CakePHP filesystem plugin using Flysystem as it's backend.
- Filesystem plugin for CakePHP
- Why
- Requirements
- Installation
- Configuration
- Simple upload example
- Result
- Entity properties
- Recreating entities
- Using your own entities
- Example on using Cake ORM entities instead of the built entity class
- Formatters
- Setting up formatters
- Creating a custom formatter class
- Example custom formatter
- Using the custom formatter class in your application
- Methods
- Events
- Extras
- Changing the hashing algorithm used in entities
- Accessing the Flysytem object
- Contribute
Why
- Easy access to Flysystem filesystems in your application
- Upload normalization, accepts $_FILES, Zend\Diactoros\UploadedFile or just a path on the local FS
- Files are represented by customisable and json serialisable entities, Multiple files are returned in a custom Collection instance.
- A trait is available, use it everywhere in your app
- Customizable path/filename formatting during upload, custom formatters are possible, ships with a Default and EntityFormatter.
Requirements
- CakePHP 4.x
- PHP 7.2
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
Configuration
A filesystem configuration array should be available in your Configure instance. You can create a config/filesystems.php file with following content Make sure to load the file in your bootstrap.php using .
The configuration options defined for each 'filestem' are passed directly to the Filesystem.php class. A default
configuration must be set when using FilesystemAwareTrait / FilesystemRegistry classes
Simple upload example
Filesystem instances can be accessed from everywhere where you either use the FilesystemAwareTrait and calling or the
In this example we are using a fictive 'myfs' filesystem definition, if you leave that empty the default FS will be used when calling getFilesystem()
.
Upload data submitted in POST:
Example controller:
Result
The result from the above example will output a file entity class
Entity properties
A JsonSerializable FileEntity ArrayObject is returned when the file was successfully uploaded. Properties can be accessed, checked and manipulated using get and set and has**
Calling json_encode on the entity
Recreating entities
If you for instance saved a file entity somwhere as a json object you could recreate the entity using Filemanager::newEntity
Recreating a Collection of entities.
Using your own entities
Creating your own entities is possible by implementing the FileEntityInterface class and setting the entity class FQCN in your configuration's entityClass
key.
Example on using Cake ORM entities instead of the built entity class
If you want to store your entities in the ORM you can easily swap the entity class with an ORM one. The only requirement is that the entity implements the FileEntityInterface
class.
Then make sure your ORM entity implements the FileEntityInterface and its required method 'getPath':
Now when uploading and using files you can work with ORM entities.
Formatters
During upload a formatter is used to construct a path and filename. For instance, if you use the EntityFormatter you can use variables available in an entity to build the filename.
The default EntityFormatter pattern is {entity-source}/{file-name}.{file-ext}
which results in posts/myfile.png
Setting up formatters
Formatters are simple classes used to name and clean file paths during upload, this plugin currently comes with two formatters.
- DefaultFormatter, this just returns the 'cleaned' filename
- EntityFormatter, extends the default formatter, expects an EntityInterface as data and used to format filenames based on data from an entity.
Should result in something like posts/2018-05-26-myfile-key.png
.
Creating a custom formatter class
Creating your own formatter class is pretty straightforward. The class should implement FormatterInterface
Check the DefaultFormatter
or EntityFormatter
classes for more information.
Example custom formatter
Using the custom formatter class in your application
The formatter FQCN can be set in the filesystem config or whenever you call setFormatter.
Methods
The Filesystem class itself implements a few convenience methods around the Flysystem filesystem class.
Other methods are proxied over. If you wish to use the Flysystem instance directly then please use getDisk().
Events
Events are dispatched when performing an operation on a file entity. Currently the following events are implemented:
Name | Passed params | Stoppable? |
---|---|---|
Filesystem.beforeUpload | FileSource, Formatter | No |
Filesystem.afterUpload | FileEntity, FileSource | No |
Filesystem.beforeDelete | FileEntity | Yes |
Filesystem.afterDelete | FileEntity | No |
Filesystem.beforeRename | FileEntity, new path | Yes |
Filesystem.afterRename | FileEntity | No |
Filesystem.beforeCopy | FileEntity, destination path | Yes |
Filesystem.afterCopy | (new) FileEntity, (old) FileEntity | No |
Extras
Changing the hashing algorithm used in entities
Options can be passed to the FileSourceNormalizer using the 'normalizer' parameter in the filesystem instance config:
Accessing the Flysytem object
Because this plugin is using flysystem at its core one could easily integrate with other flysystem compatible code.
Accessing the flysystem directly can be done using Filesystem::getDisk()
.
As an example we can work with Admad's glide plugin and use configured filesystems as source and cache:
First set up your default and cache configurations:
Then set up the Glide middleware using the configured filesystems mentioned above:
Contribute
Before submitting a PR make sure:
- PHPUnit and CakePHP Code Sniffer tests pass
- Codecov Code Coverage does not drop