2017 © Pedro Peláez
 

project file-storage-bundle

image

sokil/file-storage-bundle

  • Saturday, January 20, 2018
  • by sokil
  • Repository
  • 1 Watchers
  • 3 Stars
  • 36 Installations
  • PHP
  • 2 Dependents
  • 1 Suggesters
  • 0 Forks
  • 2 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

FileStorageBundle

Working with different filesystems, managing file metadata in Doctrine ORM. Storing metadata allows to prevent loading same file twice by checking file hash., (*1)

Build Status, (*2)

Installation

Add composer dependency:, (*3)

composer require sokil/file-storage-bundle

Add bundle to AppKernel:, (*4)

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Knp\Bundle\GaufretteBundle\KnpGaufretteBundle(),
            new Sokil\FileStorageBundle\FileStorageBundle(),
        );
    }
}



, (*5)

Configuration

  • Read about Gaufrette at https://github.com/KnpLabs/Gaufrette.
  • Read abount configuring Gaufrette filesystems in Symfony at https://github.com/KnpLabs/KnpGaufretteBundle.

Configuration of supported filesystems

This bundle uses gaufrette as filesystem abstraction, so you need to configure filesystem in app config:, (*6)

knp_gaufrette:
    adapters:
        acme.attachments_adapter:
            local:
                directory:  "%kernel.root_dir%/attachments"
                create:     true
    filesystems:
        acme.attachments_filesystem:
            adapter: acme.attachments_adapter

You need then pass this filesystem name to your code. For example define parameter in extension of your bundle:, (*7)

<?php
namespace AcmeBundle\DependencyInjection;

class AcmeExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        if (isset($config['attachments_filesystem'])) {
            $container->setParameter(
                $this->getAlias() . '.attachments_filesystem',
                $config['attachments_filesystem']
            );
        }
    }
}

And then in application config:, (*8)


acme: attachments_filesystem: "acme.attachments_filesystem"

Now you may use configured filesystems in your controller:, (*9)

<?php

$attachmentFilesystem = $this
    ->get('knp_gaufrette.filesystem_map')
    ->get($this->getParameter('acme.attachments_filesystem'));



, (*10)

Move local file to filesystem

This bundle usefull for moving local files into some external filesystems and add record to database about file. First we need to create some file entity. File entity holds useful metadata about stored file., (*11)

<?php

$file = new File();
$file
    ->setName('some.txt')
    ->setSize(4242)
    ->setCreatedAt(new \DateTime())
    ->setMime('text/plain')
    ->setHash('some_hash_of_file_content');

$this
    ->get('file_storage')
    ->write(
        $file,
        'acme.attachments_filesystem',
        'some content of file'
    );

$fileId = $file->getId(); // now you have id of file



, (*12)

See also

  • See how to easy handle uploaded files at https://github.com/sokil/php-upload

The Versions