-
-
Save ak4n/1958babafd66e6ae2477 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AcmeController implements ContainerAwareInterface | |
{ | |
use ContainerAwareTrait; | |
use RedirectTrait; | |
public function indexAction() | |
{ | |
return $this->redirect('some_route', [ | |
// .. | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Container implements ContainerInterface | |
{ | |
protected $services = []; | |
public function set($id, $service) | |
{ | |
$this->services[$id] = $service; | |
} | |
public function get($id) | |
{ | |
return $this->services[$id]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface ContainerAwareInterface | |
{ | |
public function setContainer(ContainerInterface $container = null); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait ContainerAwareTrait | |
{ | |
/** | |
* @var ContainerInterface | |
*/ | |
protected $container; | |
public function setContainer(ContainerInterface $container = null) | |
{ | |
$this->container = $container; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface RedirectorInterface | |
{ | |
public function redirect($routeName, $parameters); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RedirectResponse | |
{ | |
protected $url; | |
public function __construct($url) | |
{ | |
$this->url = $url; | |
} | |
public function __toString() | |
{ | |
return $this->url; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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