Created
December 3, 2011 15:44
-
-
Save dmvk/1427409 to your computer and use it in GitHub Desktop.
autowire services into presenter
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
services: | |
presenterFactory: | |
class: Moes\Application\PresenterFactory | |
arguments: [@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 | |
namespace Moes\Application; | |
use Nette; | |
use Nette\Reflection\ClassType; | |
class PresenterFactory extends \Nette\Application\PresenterFactory | |
{ | |
private $context; | |
public function __construct(Nette\DI\IContainer $container) | |
{ | |
$this->context = $container; | |
} | |
public function createPresenter($name) | |
{ | |
$type = new ClassType($this->getPresenterClass($name)); | |
$method = $type->getMethod('__construct'); | |
$args = array(); | |
if ($method->declaringClass->name === $type->name) | |
foreach ($type->getMethod("__construct")->getParameters() as $parameter) | |
$args[] = $this->context->findByClass($parameter->class->name); | |
$presenter = $type->newInstanceArgs($args); | |
$presenter->setContext($this->context); | |
return $presenter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment