Skip to content

Instantly share code, notes, and snippets.

@dmvk
Created December 3, 2011 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmvk/1427409 to your computer and use it in GitHub Desktop.
Save dmvk/1427409 to your computer and use it in GitHub Desktop.
autowire services into presenter
services:
presenterFactory:
class: Moes\Application\PresenterFactory
arguments: [@container]
<?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