Skip to content

Instantly share code, notes, and snippets.

@ak4n
Forked from havvg/AcmeController.php
Last active August 29, 2015 14:10
Show Gist options
  • Save ak4n/1958babafd66e6ae2477 to your computer and use it in GitHub Desktop.
Save ak4n/1958babafd66e6ae2477 to your computer and use it in GitHub Desktop.
<?php
class AcmeController implements ContainerAwareInterface
{
use ContainerAwareTrait;
use RedirectTrait;
public function indexAction()
{
return $this->redirect('some_route', [
// ..
]);
}
}
<?php
class AcmeRedirector implements RedirectorInterface
{
protected $urlGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
public function redirect($routeName, $parameters)
{
return new RedirectResponse(
$this->urlGenerator->generate($routeName, $parameters)
);
}
}
<?php
class Container implements ContainerInterface
{
protected $services = [];
public function set($id, $service)
{
$this->services[$id] = $service;
}
public function get($id)
{
return $this->services[$id];
}
}
<?php
interface ContainerAwareInterface
{
public function setContainer(ContainerInterface $container = null);
}
<?php
trait ContainerAwareTrait
{
/**
* @var ContainerInterface
*/
protected $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}
<?php
interface ContainerInterface
{
/**
* Save a service providing object under a named id.
*
* @param string $id
* @param object $service
*/
public function set($id, $service);
/**
* Retrieve a service by its id.
*
* @param string $id
*
* @return object
*/
public function get($id);
}
<?php
class ExampleUrlGenerator implements UrlGeneratorInterface
{
// Generates a URL in any way,
// requires to inject dependencies accordingly.
public function generate($routeName, array $parameters = array())
{
return 'http://example.com';
}
}
<?php
interface RedirectorInterface
{
public function redirect($routeName, $parameters);
}
<?php
class RedirectResponse
{
protected $url;
public function __construct($url)
{
$this->url = $url;
}
public function __toString()
{
return $this->url;
}
}
<?php
trait RedirectTrait
{
/**
* @var RedirectorInterface
*/
protected $redirector;
public function setRedirector(RedirectorInterface $redirector)
{
$this->redirector = $redirector;
}
public function redirect($routeName, $parameters)
{
return $this->redirector->redirect($routeName, $parameters);
}
}
<?php
foreach ([
__DIR__ . '/ContainerInterface.php',
__DIR__ . '/ContainerAwareInterface.php',
__DIR__ . '/UrlGeneratorInterface.php',
__DIR__ . '/RedirectorInterface.php',
__DIR__ . '/ContainerAwareTrait.php',
__DIR__ . '/RedirectTrait.php',
__DIR__ . '/Container.php',
__DIR__ . '/RedirectResponse.php',
__DIR__ . '/ExampleUrlGenerator.php',
__DIR__ . '/AcmeRedirector.php',
__DIR__ . '/AcmeController.php',
] as $eachFile) { require_once $eachFile; }
$container = new Container();
$container->set('controller', new AcmeController());
// meanwhile in some generated code ..
$controller = $container->get('controller');
$controller->setRedirector(new AcmeRedirector(new ExampleUrlGenerator()));
if ($controller instanceof ContainerAwareInterface) {
$controller->setContainer($container);
}
// .. let's run that
echo $controller->indexAction(), PHP_EOL;
<?php
interface UrlGeneratorInterface
{
/**
* Generate the Url for a given route name and its parameters.
*
* @return string
*/
public function generate($name, array $parameters = array());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment