Skip to content

Instantly share code, notes, and snippets.

@eddy8
Created April 1, 2017 07:30
Show Gist options
  • Save eddy8/2aeca84c4553e4ede1fab1558ce45902 to your computer and use it in GitHub Desktop.
Save eddy8/2aeca84c4553e4ede1fab1558ce45902 to your computer and use it in GitHub Desktop.
Symfony DependencyInjection Autowire
<?php
namespace Eddy;
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
require 'vendor/autoload.php';
$autowire = new AutowirePass();
$container = new ContainerBuilder();
$definition = new Definition(TwitterClient::class);
$definition->setAutowired(true);
$container->setDefinition('twitter_client', $definition);
$autowire->process($container);
$client = $container->get('twitter_client');
$client->tweet('user', 'key', 'rr');
class Rot13Transformer
{
public function transform($value)
{
return str_rot13($value);
}
}
class TwitterClient
{
private $transformer;
public function __construct(Rot13Transformer $transformer)
{
//$transformer = new Rot13Transformer();
$this->transformer = $transformer;
}
public function tweet($user, $key, $status)
{
$transformedStatus = $this->transformer->transform($status);
var_dump($transformedStatus);
// ... connect to Twitter and send the encoded status
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment