Skip to content

Latest commit

 

History

History
176 lines (121 loc) · 3.69 KB

2-service-configuration.md

File metadata and controls

176 lines (121 loc) · 3.69 KB

Chapter 2 - Service Configuration

If basic configuration is not enough for you and you need more control, then use the following service configuration:

you only need one option:

service The service that will return configuration

Option Type Required
service String True

Example:

artgris_file_manager:
    conf:
        perso:
            service: "custom_service"            

This service need to implement CustomConfServiceInterface

<?php

namespace AppBundle\Service;

use Artgris\Bundle\FileManagerBundle\Service\CustomConfServiceInterface;

class CustomService implements CustomConfServiceInterface
{
   public function getConf($extra = []) {
   
    ... 
   
   }
}

getconf($extra) must return an Array of the configuration:

   public function getConf($extra = []) {
   
     return [
     'dir' => '%kernel.project_dir%/public'
     ... 
     ];
   
   }

Do not forget to configure your services.yml

services:
    custom_service:
      class: AppBundle\Service\CustomService

Browse the /manager/?conf=perso URL to get access to this File Manager

Extra URL parameters injections

You can inject extra parameters in your service via URL:

Example:

path('file_manager', {module:'tiny', type:'image', conf:'perso', extra: {'user':'miamolex', 'allow': true}})

Here, I add 2 extra parameters, which I recover in my Service:

public function getConf($extra = []) {     

 $user = $extra['user'] # miamolex
 $allow = $extra['allow'] # true
 
 ...

With this service configuration, you can define (for example) a folder for each user

<?php


namespace AppBundle\Service;


use Artgris\Bundle\FileManagerBundle\Service\CustomConfServiceInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;

class CustomService implements CustomConfServiceInterface
{
    /**
     * @var TokenStorage
     */
    private $tokenStorage;


    /**
     * CustomService constructor.
     * @param TokenStorage $tokenStorage
     */
    public function __construct(TokenStorage $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }


    public function getConf($extra = [])
    {
        $folder = 'user/' . $this->tokenStorage->getToken()->getUser();
        $fs = new Filesystem();
        if (!$fs->exists($folder)) {
            $fs->mkdir($folder);
        }
        return ['dir' => $folder];

    }

}

with

custom_service:
      public: true
      class: AppBundle\Service\CustomService
      arguments: ['@security.token_storage']

upload Exhaustive options (file upload widget)

You can include all the options of jQuery File Upload in return (to make it easier than .yml):

Exhaustive options

Example

<?php

namespace AppBundle\Service;

use Artgris\Bundle\FileManagerBundle\Service\CustomConfServiceInterface;

class CustomService implements CustomConfServiceInterface
{
      public function getConf($extra = [])
      {
          return [
              'dir' => '%kernel.project_dir%/public/perso',
              'upload' => [
                  'image_versions' => [
                      'medium' => [
                          'auto_orient' => true,
                          'max_width' => 10
                      ]
                  ],
              ]
          ];
      }
}

Chapter 3 - Access to the File Manager