secret
Created

Wiring up Symfony2 services in the DIC using configuration parameters

  • Download Gist
StorageFactory.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?php
 
namespace Vich\UploaderBundle\Storage;
 
use Symfony\Component\DependencyInjection\ContainerInterface;
 
/**
* StorageFactory
*
** @author Francesco Tassi <tassi.francesco@gmail.com>
*/
class StorageFactory
{
 
/**
*
* @var ContainerInterface
*/
protected $container;
 
/**
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
 
public function createStorage()
{
return $this->container->get($this->container->getParameter('vich_uploader.storage_service'));
}
 
}
factory.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?xml version="1.0" encoding="UTF-8" ?>
 
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
 
<services>
 
<service id="vich_uploader.property_mapping_factory" class="Vich\UploaderBundle\Mapping\PropertyMappingFactory" public="false">
<argument type="service" id="service_container" />
<argument type="service" id="vich_uploader.annotation_driver" />
<argument type="service" id="vich_uploader.adapter" />
<argument>%vich_uploader.mappings%</argument>
</service>
 
<service id="vich_uploader.storage_factory" class="Vich\UploaderBundle\Storage\StorageFactory">
<argument type="service" id="service_container" />
</service>
 
 
</services>
 
</container>
storage.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?xml version="1.0" encoding="UTF-8" ?>
 
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
 
<services>
<service id="vich_uploader.storage.adapter.rackspace_cloud_files" class="Vich\UploaderBundle\Storage\Adapter\RackspaceCloudFilesAdapter">
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
<call method="setMediaContainer">
<argument type="string">%vich_uploader.storage.adapter.rackspace.media_container%</argument>
</call>
</service>
 
<service id="vich_uploader.storage.file_system" class="Vich\UploaderBundle\Storage\FileSystemStorage">
<argument type="service" id="vich_uploader.property_mapping_factory" />
</service>
 
<service id="vich_uploader.storage.cdn" class="Vich\UploaderBundle\Storage\CDNStorage">
<argument type="service" id="vich_uploader.property_mapping_factory" />
<argument type="service" id="vich_uploader.storage.adapter.rackspace_cloud_files" />
</service>
 
<service
id="vich_uploader.storage"
class="Vich\UploaderBundle\Storage\StorageInterface"
factory-service="vich_uploader.storage_factory"
factory-method ="createStorage">
 
</service>
</services>
 
</container>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.