partnermarketing / file-system-bundle
A file system component supporting different file storage adapters.
Installs: 9 030
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 16
Forks: 6
Open Issues: 6
Requires
- php: >=5.4.0
- aws/aws-sdk-php: ~3.0
- symfony/filesystem: >2.3.0
Requires (Dev)
- johnkary/phpunit-speedtrap: ~1.0.0
- partnermarketing/pm-git-hooks-php: ^1.0
- phpunit/phpunit: ~4.0 | ~5.0
README
FileSystemBundle is a file system component supporting different file storage adapters.
Adapters:
Local storage
This adapter was made to be used when you want to interact with your local file system.
Config example:
partnermarketing_file_system.default_file_system: local_storage partnermarketing_file_system.config: local_storage: path: /path/to/test/directory url: 'http://your-project-url.dev/test'
Amazon S3
This adapter was made to be used when you want to interactive with Amazon S3 file system.
Config example:
partnermarketing_file_system.default_file_system: amazon_s3 partnermarketing_file_system.config: amazon_s3: key: your-amazon-key secret: your-amazon-secret bucket: your-bucket-name region: eu-west-1 acl: public-read # Optional parameter.
How to use
Configuration
First step is to pass the factory into where you need to use it.
# In your services.yml YourServiceName: class: Your\Namespace\Path\ServiceName arguments: fileSystemFactory: @partnermarketing_file_system.factory
Then in your ServiceName.php file you can use the factory as you need.
namespace Your\Namespace\Path; use Partnermarketing\FileSystemBundle\Factory\FileSystemFactory; class ServiceName { private $fileSystem; public function __construct(FileSystemFactory $fileSystemFactory) { // This will build a fileSystem based on configs specified. $this->filesystem = $fileSystemFactory->build(); } }
Read a file content
$this->filesystem->read($varWithFilePath);
Write content from a file to other
// Writes the content of the $source into the $path returns the URL. $url = $this->filesystem->write($path, $source);
Write content into a file
// Writes the $content into the $path returns the URL: $url = $this->filesystem->writeContent($path, $content);
Delete a file
// Deletes the file $path: $isDeleted = $this->filesystem->delete($path);
Rename a file
$isRenamed = $this->filesystem->rename($sourcePath, $targetPath);
Get files from directory
// Returns an array of files under given directory. $filesArray = $this->filesystem->getFiles($directory = '');
Copy files from one directory to another
// Copies all files under given source directory to given target directory. $filesArray = $this->filesystem->copyFiles($sourceDir, $targetDir);
Check if a file exist
$fileExists = $this->filesystem->exists($varWithFilePath);
Check if path is a directory
$isDirectory = $this->filesystem->isDirectory($varWithFilePath);
Gets the Absolute URL to a file
$absoluteFileUrl = $this->filesystem->getURL($path);
Copy file to temporary directory
// Copy a file to the local temporary directory, and return the full path. $temporaryFilePath = $this->filesystem->copyToLocalTemporaryFile($path);
How to contribute
You can add more adapters or improve the existing ones.
Create a pull request and please add tests if you fix a bug or added new functionality.
Report founded issues here:
https://github.com/partnermarketing/PartnermarketingFileSystemBundle/issues