Skip to content

Instantly share code, notes, and snippets.

@havvg
Created May 2, 2012 08:56
Show Gist options
  • Save havvg/2575286 to your computer and use it in GitHub Desktop.
Save havvg/2575286 to your computer and use it in GitHub Desktop.
Example StreamRegistry registering Gaufrette streams in Symfony2
<?php
namespace Ormigo\Bundle\OrmigoBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Ormigo\Bundle\OrmigoBundle\DependencyInjection\Compiler\RegisterStreamsPass;
/**
* This is the main application bundle.
* It contains everything that can not be further split up from the core application.
*/
class OrmigoBundle extends Bundle
{
public function boot()
{
if ($this->container->has('ormigo.gaufrette.stream_registry')) {
/* @var $registry \Ormigo\Bundle\OrmigoBundle\Gaufrette\StreamRegistry */
$registry = $this->container->get('ormigo.gaufrette.stream_registry');
$registry->register();
}
}
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new RegisterStreamsPass());
}
public function getParent()
{
return 'FrameworkBundle';
}
}
<?php
namespace Ormigo\Bundle\OrmigoBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;
class RegisterStreamsPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('ormigo.gaufrette.stream_registry')) {
return;
}
foreach ($container->findTaggedServiceIds('ormigo.stream_filesystem') as $id => $tags) {
$domain = '';
foreach ($tags as $eachTag) {
if (!empty($eachTag['alias'])) {
$domain = $eachTag['alias'];
break;
}
}
$container
->getDefinition('ormigo.gaufrette.stream_registry')
->addMethodCall('addStreamDefinition', array($domain, new Reference($id)))
;
}
}
}
services:
ormigo.gaufrette.stream_registry:
class: Ormigo\Bundle\OrmigoBundle\Gaufrette\StreamRegistry
gaufrette.filesystem.profile_photos.cropped:
class: Gaufrette\Filesystem
factory_service: 'knp_gaufrette.filesystem_map'
factory_method: 'get'
arguments:
- 'profile_photos_cropped'
tags:
- { name: 'ormigo.stream_filesystem', alias: 'profile_photos_cropped' }
ormigo.imagine.data.loader.profile_photos:
class: "%liip_imagine.data.loader.stream.class%"
arguments:
- "@liip_imagine"
- 'gaufrette://profile_photos_cropped/'
tags:
- { name: 'liip_imagine.data.loader', loader: 'profile_photos' }
<?php
namespace Ormigo\Bundle\OrmigoBundle\Gaufrette;
use Gaufrette\Filesystem;
use Gaufrette\StreamWrapper;
class StreamRegistry
{
protected $streamDefinitions = array();
/**
* Add a filesystem to be registered.
*
* @param string $domain
* @param \Gaufrette\Filesystem $filesystem
*
* @return StreamRegistry $this
*/
public function addStreamDefinition($domain, Filesystem $filesystem)
{
$this->streamDefinitions[$domain] = $filesystem;
return $this;
}
/**
* Register all added stream definitions using the Gaufrette\StreamWrapper.
*
* @param string $scheme The stream protocol scheme to use.
*
* @return StreamRegistry $this
*/
public function register($scheme = 'gaufrette')
{
$map = StreamWrapper::getFilesystemMap();
foreach ($this->streamDefinitions as $eachDomain => $eachFilesystem) {
$map->set($eachDomain, $eachFilesystem);
}
StreamWrapper::register($scheme);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment